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