2
0

json: Implement json.Unmarshaler for messages.

This will allow using pgmockproxy output as ingestion data for pgmock.
This commit is contained in:
Henrique Vicente
2021-05-16 02:05:24 +02:00
committed by Jack Christensen
parent 1213b69774
commit ba924e5715
19 changed files with 472 additions and 1 deletions
+23
View File
@@ -67,3 +67,26 @@ func (src SASLInitialResponse) MarshalJSON() ([]byte, error) {
Data: hex.EncodeToString(src.Data),
})
}
// UnmarshalJSON implements encoding/json.Unmarshaler.
func (dst *SASLInitialResponse) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" {
return nil
}
var msg struct {
AuthMechanism string
Data string
}
if err := json.Unmarshal(data, &msg); err != nil {
return err
}
decodedData, err := hex.DecodeString(msg.Data)
if err != nil {
return err
}
dst.AuthMechanism = msg.AuthMechanism
dst.Data = decodedData
return nil
}