sasl_response.go (1400B)
1 package pgproto3 2 3 import ( 4 "encoding/hex" 5 "encoding/json" 6 7 "github.com/jackc/pgx/v5/internal/pgio" 8 ) 9 10 type SASLResponse struct { 11 Data []byte 12 } 13 14 // Frontend identifies this message as sendable by a PostgreSQL frontend. 15 func (*SASLResponse) Frontend() {} 16 17 // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message 18 // type identifier and 4 byte message length. 19 func (dst *SASLResponse) Decode(src []byte) error { 20 *dst = SASLResponse{Data: src} 21 return nil 22 } 23 24 // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. 25 func (src *SASLResponse) Encode(dst []byte) []byte { 26 dst = append(dst, 'p') 27 dst = pgio.AppendInt32(dst, int32(4+len(src.Data))) 28 29 dst = append(dst, src.Data...) 30 31 return dst 32 } 33 34 // MarshalJSON implements encoding/json.Marshaler. 35 func (src SASLResponse) MarshalJSON() ([]byte, error) { 36 return json.Marshal(struct { 37 Type string 38 Data string 39 }{ 40 Type: "SASLResponse", 41 Data: string(src.Data), 42 }) 43 } 44 45 // UnmarshalJSON implements encoding/json.Unmarshaler. 46 func (dst *SASLResponse) UnmarshalJSON(data []byte) error { 47 var msg struct { 48 Data string 49 } 50 if err := json.Unmarshal(data, &msg); err != nil { 51 return err 52 } 53 if msg.Data != "" { 54 decoded, err := hex.DecodeString(msg.Data) 55 if err != nil { 56 return err 57 } 58 dst.Data = decoded 59 } 60 return nil 61 }