github.com/jackc/pgx/v5@v5.5.5/pgproto3/command_complete.go (about) 1 package pgproto3 2 3 import ( 4 "bytes" 5 "encoding/json" 6 ) 7 8 type CommandComplete struct { 9 CommandTag []byte 10 } 11 12 // Backend identifies this message as sendable by the PostgreSQL backend. 13 func (*CommandComplete) Backend() {} 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 *CommandComplete) Decode(src []byte) error { 18 idx := bytes.IndexByte(src, 0) 19 if idx == -1 { 20 return &invalidMessageFormatErr{messageType: "CommandComplete", details: "unterminated string"} 21 } 22 if idx != len(src)-1 { 23 return &invalidMessageFormatErr{messageType: "CommandComplete", details: "string terminated too early"} 24 } 25 26 dst.CommandTag = src[:idx] 27 28 return nil 29 } 30 31 // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. 32 func (src *CommandComplete) Encode(dst []byte) ([]byte, error) { 33 dst, sp := beginMessage(dst, 'C') 34 dst = append(dst, src.CommandTag...) 35 dst = append(dst, 0) 36 return finishMessage(dst, sp) 37 } 38 39 // MarshalJSON implements encoding/json.Marshaler. 40 func (src CommandComplete) MarshalJSON() ([]byte, error) { 41 return json.Marshal(struct { 42 Type string 43 CommandTag string 44 }{ 45 Type: "CommandComplete", 46 CommandTag: string(src.CommandTag), 47 }) 48 } 49 50 // UnmarshalJSON implements encoding/json.Unmarshaler. 51 func (dst *CommandComplete) UnmarshalJSON(data []byte) error { 52 // Ignore null, like in the main JSON package. 53 if string(data) == "null" { 54 return nil 55 } 56 57 var msg struct { 58 CommandTag string 59 } 60 if err := json.Unmarshal(data, &msg); err != nil { 61 return err 62 } 63 64 dst.CommandTag = []byte(msg.CommandTag) 65 return nil 66 }