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
+22
View File
@@ -2,6 +2,7 @@ package pgproto3
import (
"encoding/binary"
"encoding/json"
"errors"
"github.com/jackc/pgio"
@@ -41,3 +42,24 @@ func (src *AuthenticationMD5Password) Encode(dst []byte) []byte {
dst = append(dst, src.Salt[:]...)
return dst
}
// UnmarshalJSON implements encoding/json.Unmarshaler.
func (dst *AuthenticationMD5Password) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" {
return nil
}
var msg struct {
Salt string
}
if err := json.Unmarshal(data, &msg); err != nil {
return err
}
if len(msg.Salt) != 4 {
return errors.New("invalid salt size")
}
copy(dst.Salt[:], []byte(msg.Salt)[:4])
return nil
}