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