copy_fail.go (1194B)
1 package pgproto3 2 3 import ( 4 "bytes" 5 "encoding/json" 6 7 "github.com/jackc/pgio" 8 ) 9 10 type CopyFail struct { 11 Message string 12 } 13 14 // Frontend identifies this message as sendable by a PostgreSQL frontend. 15 func (*CopyFail) 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 *CopyFail) Decode(src []byte) error { 20 idx := bytes.IndexByte(src, 0) 21 if idx != len(src)-1 { 22 return &invalidMessageFormatErr{messageType: "CopyFail"} 23 } 24 25 dst.Message = string(src[:idx]) 26 27 return nil 28 } 29 30 // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. 31 func (src *CopyFail) Encode(dst []byte) []byte { 32 dst = append(dst, 'f') 33 sp := len(dst) 34 dst = pgio.AppendInt32(dst, -1) 35 36 dst = append(dst, src.Message...) 37 dst = append(dst, 0) 38 39 pgio.SetInt32(dst[sp:], int32(len(dst[sp:]))) 40 41 return dst 42 } 43 44 // MarshalJSON implements encoding/json.Marshaler. 45 func (src CopyFail) MarshalJSON() ([]byte, error) { 46 return json.Marshal(struct { 47 Type string 48 Message string 49 }{ 50 Type: "CopyFail", 51 Message: src.Message, 52 }) 53 }