github.com/jackc/pgx/v5@v5.5.5/pgproto3/parameter_status.go (about) 1 package pgproto3 2 3 import ( 4 "bytes" 5 "encoding/json" 6 ) 7 8 type ParameterStatus struct { 9 Name string 10 Value string 11 } 12 13 // Backend identifies this message as sendable by the PostgreSQL backend. 14 func (*ParameterStatus) Backend() {} 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 *ParameterStatus) Decode(src []byte) error { 19 buf := bytes.NewBuffer(src) 20 21 b, err := buf.ReadBytes(0) 22 if err != nil { 23 return err 24 } 25 name := string(b[:len(b)-1]) 26 27 b, err = buf.ReadBytes(0) 28 if err != nil { 29 return err 30 } 31 value := string(b[:len(b)-1]) 32 33 *dst = ParameterStatus{Name: name, Value: value} 34 return nil 35 } 36 37 // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. 38 func (src *ParameterStatus) Encode(dst []byte) ([]byte, error) { 39 dst, sp := beginMessage(dst, 'S') 40 dst = append(dst, src.Name...) 41 dst = append(dst, 0) 42 dst = append(dst, src.Value...) 43 dst = append(dst, 0) 44 return finishMessage(dst, sp) 45 } 46 47 // MarshalJSON implements encoding/json.Marshaler. 48 func (ps ParameterStatus) MarshalJSON() ([]byte, error) { 49 return json.Marshal(struct { 50 Type string 51 Name string 52 Value string 53 }{ 54 Type: "ParameterStatus", 55 Name: ps.Name, 56 Value: ps.Value, 57 }) 58 }