diff --git a/authentication.go b/authentication.go index 2078c87c..bc654c4f 100644 --- a/authentication.go +++ b/authentication.go @@ -8,6 +8,7 @@ import ( "github.com/pkg/errors" ) +// Authentication message type constants. const ( AuthTypeOk = 0 AuthTypeCleartextPassword = 3 @@ -17,6 +18,10 @@ const ( AuthTypeSASLFinal = 12 ) +// Authentication is a message sent from the backend during the authentication process. +// +// There are multiple authentication messages that each begin with 'R'. This structure represents all such +// authentication messages. type Authentication struct { Type uint32 @@ -30,8 +35,11 @@ type Authentication struct { SASLData []byte } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*Authentication) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *Authentication) Decode(src []byte) error { *dst = Authentication{Type: binary.BigEndian.Uint32(src[:4])} @@ -58,6 +66,7 @@ func (dst *Authentication) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *Authentication) Encode(dst []byte) []byte { dst = append(dst, 'R') sp := len(dst) diff --git a/backend.go b/backend.go index 7f11bc7f..2e2f5eea 100644 --- a/backend.go +++ b/backend.go @@ -7,6 +7,7 @@ import ( "github.com/pkg/errors" ) +// Backend acts as a server for the PostgreSQL wire protocol version 3. type Backend struct { cr ChunkReader w io.Writer @@ -30,15 +31,19 @@ type Backend struct { partialMsg bool } +// NewBackend creates a new Backend. func NewBackend(cr ChunkReader, w io.Writer) (*Backend, error) { return &Backend{cr: cr, w: w}, nil } +// Send sends a message to the frontend. func (b *Backend) Send(msg BackendMessage) error { _, err := b.w.Write(msg.Encode(nil)) return err } +// ReceiveStartupMessage receives the initial startup message. This method is used of the normal Receive method +// because StartupMessage and SSLRequest are "special" and do not include the message type as the first byte. func (b *Backend) ReceiveStartupMessage() (*StartupMessage, error) { buf, err := b.cr.Next(4) if err != nil { @@ -59,6 +64,7 @@ func (b *Backend) ReceiveStartupMessage() (*StartupMessage, error) { return &b.startupMessage, nil } +// Receive receives a message from the frontend. func (b *Backend) Receive() (FrontendMessage, error) { if !b.partialMsg { header, err := b.cr.Next(5) diff --git a/backend_key_data.go b/backend_key_data.go index 0396379b..b775d689 100644 --- a/backend_key_data.go +++ b/backend_key_data.go @@ -12,8 +12,11 @@ type BackendKeyData struct { SecretKey uint32 } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*BackendKeyData) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *BackendKeyData) Decode(src []byte) error { if len(src) != 8 { return &invalidMessageLenErr{messageType: "BackendKeyData", expectedLen: 8, actualLen: len(src)} @@ -25,6 +28,7 @@ func (dst *BackendKeyData) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *BackendKeyData) Encode(dst []byte) []byte { dst = append(dst, 'K') dst = pgio.AppendUint32(dst, 12) @@ -33,6 +37,7 @@ func (src *BackendKeyData) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *BackendKeyData) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/bind.go b/bind.go index 459e5ff2..67d20b5d 100644 --- a/bind.go +++ b/bind.go @@ -17,8 +17,11 @@ type Bind struct { ResultFormatCodes []int16 } +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*Bind) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *Bind) Decode(src []byte) error { *dst = Bind{} @@ -103,6 +106,7 @@ func (dst *Bind) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *Bind) Encode(dst []byte) []byte { dst = append(dst, 'B') sp := len(dst) @@ -139,6 +143,7 @@ func (src *Bind) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *Bind) MarshalJSON() ([]byte, error) { formattedParameters := make([]map[string]string, len(src.Parameters)) for i, p := range src.Parameters { diff --git a/bind_complete.go b/bind_complete.go index 60360519..fc9d317a 100644 --- a/bind_complete.go +++ b/bind_complete.go @@ -6,8 +6,11 @@ import ( type BindComplete struct{} +// Backend identifies this message as sendable by the PostgreSQL backend. func (*BindComplete) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *BindComplete) Decode(src []byte) error { if len(src) != 0 { return &invalidMessageLenErr{messageType: "BindComplete", expectedLen: 0, actualLen: len(src)} @@ -16,10 +19,12 @@ func (dst *BindComplete) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *BindComplete) Encode(dst []byte) []byte { return append(dst, '2', 0, 0, 0, 4) } +// MarshalJSON implements encoding/json.Marshaler. func (src *BindComplete) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/chunkreader.go b/chunkreader.go index 2eb278ea..92206f35 100644 --- a/chunkreader.go +++ b/chunkreader.go @@ -13,6 +13,7 @@ type ChunkReader interface { Next(n int) (buf []byte, err error) } +// NewChunkReader creates and returns a new default ChunkReader. func NewChunkReader(r io.Reader) ChunkReader { return chunkreader.New(r) } diff --git a/close.go b/close.go index 4e497549..349a319d 100644 --- a/close.go +++ b/close.go @@ -12,8 +12,11 @@ type Close struct { Name string } +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*Close) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *Close) Decode(src []byte) error { if len(src) < 2 { return &invalidMessageFormatErr{messageType: "Close"} @@ -32,6 +35,7 @@ func (dst *Close) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *Close) Encode(dst []byte) []byte { dst = append(dst, 'C') sp := len(dst) @@ -46,6 +50,7 @@ func (src *Close) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *Close) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/close_complete.go b/close_complete.go index db793c94..b4982207 100644 --- a/close_complete.go +++ b/close_complete.go @@ -6,8 +6,11 @@ import ( type CloseComplete struct{} +// Backend identifies this message as sendable by the PostgreSQL backend. func (*CloseComplete) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *CloseComplete) Decode(src []byte) error { if len(src) != 0 { return &invalidMessageLenErr{messageType: "CloseComplete", expectedLen: 0, actualLen: len(src)} @@ -16,10 +19,12 @@ func (dst *CloseComplete) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *CloseComplete) Encode(dst []byte) []byte { return append(dst, '3', 0, 0, 0, 4) } +// MarshalJSON implements encoding/json.Marshaler. func (src *CloseComplete) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/command_complete.go b/command_complete.go index adcc56c8..87fcddf6 100644 --- a/command_complete.go +++ b/command_complete.go @@ -11,8 +11,11 @@ type CommandComplete struct { CommandTag []byte } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*CommandComplete) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *CommandComplete) Decode(src []byte) error { idx := bytes.IndexByte(src, 0) if idx != len(src)-1 { @@ -24,6 +27,7 @@ func (dst *CommandComplete) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *CommandComplete) Encode(dst []byte) []byte { dst = append(dst, 'C') sp := len(dst) @@ -37,6 +41,7 @@ func (src *CommandComplete) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *CommandComplete) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/copy_both_response.go b/copy_both_response.go index aa59d52a..b037a197 100644 --- a/copy_both_response.go +++ b/copy_both_response.go @@ -13,8 +13,11 @@ type CopyBothResponse struct { ColumnFormatCodes []uint16 } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*CopyBothResponse) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *CopyBothResponse) Decode(src []byte) error { buf := bytes.NewBuffer(src) @@ -39,6 +42,7 @@ func (dst *CopyBothResponse) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *CopyBothResponse) Encode(dst []byte) []byte { dst = append(dst, 'W') sp := len(dst) @@ -54,6 +58,7 @@ func (src *CopyBothResponse) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *CopyBothResponse) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/copy_data.go b/copy_data.go index 490d3d80..317710ac 100644 --- a/copy_data.go +++ b/copy_data.go @@ -11,14 +11,20 @@ type CopyData struct { Data []byte } -func (*CopyData) Backend() {} +// Backend identifies this message as sendable by the PostgreSQL backend. +func (*CopyData) Backend() {} + +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*CopyData) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *CopyData) Decode(src []byte) error { dst.Data = src return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *CopyData) Encode(dst []byte) []byte { dst = append(dst, 'd') dst = pgio.AppendInt32(dst, int32(4+len(src.Data))) @@ -26,6 +32,7 @@ func (src *CopyData) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *CopyData) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/copy_done.go b/copy_done.go index 92481908..7612350a 100644 --- a/copy_done.go +++ b/copy_done.go @@ -7,8 +7,11 @@ import ( type CopyDone struct { } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*CopyDone) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *CopyDone) Decode(src []byte) error { if len(src) != 0 { return &invalidMessageLenErr{messageType: "CopyDone", expectedLen: 0, actualLen: len(src)} @@ -17,10 +20,12 @@ func (dst *CopyDone) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *CopyDone) Encode(dst []byte) []byte { return append(dst, 'c', 0, 0, 0, 4) } +// MarshalJSON implements encoding/json.Marshaler. func (src *CopyDone) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/copy_fail.go b/copy_fail.go index 2f228a82..b12d7ba0 100644 --- a/copy_fail.go +++ b/copy_fail.go @@ -11,8 +11,11 @@ type CopyFail struct { Message string } +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*CopyFail) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *CopyFail) Decode(src []byte) error { idx := bytes.IndexByte(src, 0) if idx != len(src)-1 { @@ -24,6 +27,7 @@ func (dst *CopyFail) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *CopyFail) Encode(dst []byte) []byte { dst = append(dst, 'f') sp := len(dst) @@ -37,6 +41,7 @@ func (src *CopyFail) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *CopyFail) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/copy_in_response.go b/copy_in_response.go index 3ddeeb40..d28baa33 100644 --- a/copy_in_response.go +++ b/copy_in_response.go @@ -13,8 +13,11 @@ type CopyInResponse struct { ColumnFormatCodes []uint16 } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*CopyInResponse) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *CopyInResponse) Decode(src []byte) error { buf := bytes.NewBuffer(src) @@ -39,6 +42,7 @@ func (dst *CopyInResponse) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *CopyInResponse) Encode(dst []byte) []byte { dst = append(dst, 'G') sp := len(dst) @@ -54,6 +58,7 @@ func (src *CopyInResponse) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *CopyInResponse) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/copy_out_response.go b/copy_out_response.go index eb6fb50e..1d3c2364 100644 --- a/copy_out_response.go +++ b/copy_out_response.go @@ -15,6 +15,8 @@ type CopyOutResponse struct { func (*CopyOutResponse) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *CopyOutResponse) Decode(src []byte) error { buf := bytes.NewBuffer(src) @@ -39,6 +41,7 @@ func (dst *CopyOutResponse) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *CopyOutResponse) Encode(dst []byte) []byte { dst = append(dst, 'H') sp := len(dst) @@ -56,6 +59,7 @@ func (src *CopyOutResponse) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *CopyOutResponse) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/data_row.go b/data_row.go index 0da18b06..9d6a9f1f 100644 --- a/data_row.go +++ b/data_row.go @@ -12,8 +12,11 @@ type DataRow struct { Values [][]byte } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*DataRow) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *DataRow) Decode(src []byte) error { if len(src) < 2 { return &invalidMessageFormatErr{messageType: "DataRow"} @@ -59,6 +62,7 @@ func (dst *DataRow) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *DataRow) Encode(dst []byte) []byte { dst = append(dst, 'D') sp := len(dst) @@ -80,6 +84,7 @@ func (src *DataRow) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *DataRow) MarshalJSON() ([]byte, error) { formattedValues := make([]map[string]string, len(src.Values)) for i, v := range src.Values { diff --git a/describe.go b/describe.go index 86016ebc..d3fb5b09 100644 --- a/describe.go +++ b/describe.go @@ -12,8 +12,11 @@ type Describe struct { Name string } +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*Describe) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *Describe) Decode(src []byte) error { if len(src) < 2 { return &invalidMessageFormatErr{messageType: "Describe"} @@ -32,6 +35,7 @@ func (dst *Describe) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *Describe) Encode(dst []byte) []byte { dst = append(dst, 'D') sp := len(dst) @@ -46,6 +50,7 @@ func (src *Describe) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *Describe) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/doc.go b/doc.go index 75340210..8226dc98 100644 --- a/doc.go +++ b/doc.go @@ -1,2 +1,4 @@ // Package pgproto3 is a encoder and decoder of the PostgreSQL wire protocol version 3. +// +// See https://www.postgresql.org/docs/current/protocol-message-formats.html for meanings of the different messages. package pgproto3 diff --git a/empty_query_response.go b/empty_query_response.go index d283b06d..1bec52e2 100644 --- a/empty_query_response.go +++ b/empty_query_response.go @@ -6,8 +6,11 @@ import ( type EmptyQueryResponse struct{} +// Backend identifies this message as sendable by the PostgreSQL backend. func (*EmptyQueryResponse) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *EmptyQueryResponse) Decode(src []byte) error { if len(src) != 0 { return &invalidMessageLenErr{messageType: "EmptyQueryResponse", expectedLen: 0, actualLen: len(src)} @@ -16,10 +19,12 @@ func (dst *EmptyQueryResponse) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *EmptyQueryResponse) Encode(dst []byte) []byte { return append(dst, 'I', 0, 0, 0, 4) } +// MarshalJSON implements encoding/json.Marshaler. func (src *EmptyQueryResponse) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/error_response.go b/error_response.go index 987fe38a..d444798b 100644 --- a/error_response.go +++ b/error_response.go @@ -28,8 +28,11 @@ type ErrorResponse struct { UnknownFields map[byte]string } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*ErrorResponse) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *ErrorResponse) Decode(src []byte) error { *dst = ErrorResponse{} @@ -103,6 +106,7 @@ func (dst *ErrorResponse) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *ErrorResponse) Encode(dst []byte) []byte { return append(dst, src.marshalBinary('E')...) } diff --git a/execute.go b/execute.go index 71713f49..32269857 100644 --- a/execute.go +++ b/execute.go @@ -13,8 +13,11 @@ type Execute struct { MaxRows uint32 } +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*Execute) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *Execute) Decode(src []byte) error { buf := bytes.NewBuffer(src) @@ -32,6 +35,7 @@ func (dst *Execute) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *Execute) Encode(dst []byte) []byte { dst = append(dst, 'E') sp := len(dst) @@ -47,6 +51,7 @@ func (src *Execute) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *Execute) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/flush.go b/flush.go index 7fd5e987..e7bc7e43 100644 --- a/flush.go +++ b/flush.go @@ -6,8 +6,11 @@ import ( type Flush struct{} +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*Flush) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *Flush) Decode(src []byte) error { if len(src) != 0 { return &invalidMessageLenErr{messageType: "Flush", expectedLen: 0, actualLen: len(src)} @@ -16,10 +19,12 @@ func (dst *Flush) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *Flush) Encode(dst []byte) []byte { return append(dst, 'H', 0, 0, 0, 4) } +// MarshalJSON implements encoding/json.Marshaler. func (src *Flush) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/frontend.go b/frontend.go index a0a5b493..bcf796e1 100644 --- a/frontend.go +++ b/frontend.go @@ -7,6 +7,7 @@ import ( "github.com/pkg/errors" ) +// Frontend acts as a client for the PostgreSQL wire protocol version 3. type Frontend struct { cr ChunkReader w io.Writer @@ -41,15 +42,18 @@ type Frontend struct { partialMsg bool } +// NewFrontend creates a new Frontend. func NewFrontend(cr ChunkReader, w io.Writer) (*Frontend, error) { return &Frontend{cr: cr, w: w}, nil } +// Send sends a message to the backend. func (b *Frontend) Send(msg FrontendMessage) error { _, err := b.w.Write(msg.Encode(nil)) return err } +// Receive receives a message from the backend. func (b *Frontend) Receive() (BackendMessage, error) { if !b.partialMsg { header, err := b.cr.Next(5) diff --git a/function_call_response.go b/function_call_response.go index f14f8452..72bb907c 100644 --- a/function_call_response.go +++ b/function_call_response.go @@ -12,8 +12,11 @@ type FunctionCallResponse struct { Result []byte } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*FunctionCallResponse) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *FunctionCallResponse) Decode(src []byte) error { if len(src) < 4 { return &invalidMessageFormatErr{messageType: "FunctionCallResponse"} @@ -35,6 +38,7 @@ func (dst *FunctionCallResponse) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *FunctionCallResponse) Encode(dst []byte) []byte { dst = append(dst, 'V') sp := len(dst) @@ -52,6 +56,7 @@ func (src *FunctionCallResponse) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *FunctionCallResponse) MarshalJSON() ([]byte, error) { var formattedValue map[string]string var hasNonPrintable bool diff --git a/no_data.go b/no_data.go index 1fb47c2a..172d0dc1 100644 --- a/no_data.go +++ b/no_data.go @@ -6,8 +6,11 @@ import ( type NoData struct{} +// Backend identifies this message as sendable by the PostgreSQL backend. func (*NoData) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *NoData) Decode(src []byte) error { if len(src) != 0 { return &invalidMessageLenErr{messageType: "NoData", expectedLen: 0, actualLen: len(src)} @@ -16,10 +19,12 @@ func (dst *NoData) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *NoData) Encode(dst []byte) []byte { return append(dst, 'n', 0, 0, 0, 4) } +// MarshalJSON implements encoding/json.Marshaler. func (src *NoData) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/notice_response.go b/notice_response.go index e4595aa5..4ac28a79 100644 --- a/notice_response.go +++ b/notice_response.go @@ -2,12 +2,16 @@ package pgproto3 type NoticeResponse ErrorResponse +// Backend identifies this message as sendable by the PostgreSQL backend. func (*NoticeResponse) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *NoticeResponse) Decode(src []byte) error { return (*ErrorResponse)(dst).Decode(src) } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *NoticeResponse) Encode(dst []byte) []byte { return append(dst, (*ErrorResponse)(src).marshalBinary('N')...) } diff --git a/notification_response.go b/notification_response.go index 2b32b10c..33170f66 100644 --- a/notification_response.go +++ b/notification_response.go @@ -14,8 +14,11 @@ type NotificationResponse struct { Payload string } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*NotificationResponse) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *NotificationResponse) Decode(src []byte) error { buf := bytes.NewBuffer(src) @@ -37,6 +40,7 @@ func (dst *NotificationResponse) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *NotificationResponse) Encode(dst []byte) []byte { dst = append(dst, 'A') sp := len(dst) @@ -52,6 +56,7 @@ func (src *NotificationResponse) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *NotificationResponse) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/parameter_description.go b/parameter_description.go index 9d964129..a43e802e 100644 --- a/parameter_description.go +++ b/parameter_description.go @@ -12,8 +12,11 @@ type ParameterDescription struct { ParameterOIDs []uint32 } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*ParameterDescription) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *ParameterDescription) Decode(src []byte) error { buf := bytes.NewBuffer(src) @@ -35,6 +38,7 @@ func (dst *ParameterDescription) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *ParameterDescription) Encode(dst []byte) []byte { dst = append(dst, 't') sp := len(dst) @@ -50,6 +54,7 @@ func (src *ParameterDescription) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *ParameterDescription) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/parameter_status.go b/parameter_status.go index d370a4c1..4385fe99 100644 --- a/parameter_status.go +++ b/parameter_status.go @@ -12,8 +12,11 @@ type ParameterStatus struct { Value string } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*ParameterStatus) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *ParameterStatus) Decode(src []byte) error { buf := bytes.NewBuffer(src) @@ -33,6 +36,7 @@ func (dst *ParameterStatus) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *ParameterStatus) Encode(dst []byte) []byte { dst = append(dst, 'S') sp := len(dst) @@ -48,6 +52,7 @@ func (src *ParameterStatus) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (ps *ParameterStatus) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/parse.go b/parse.go index 6f17175b..d0bbf865 100644 --- a/parse.go +++ b/parse.go @@ -14,8 +14,11 @@ type Parse struct { ParameterOIDs []uint32 } +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*Parse) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *Parse) Decode(src []byte) error { *dst = Parse{} @@ -48,6 +51,7 @@ func (dst *Parse) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *Parse) Encode(dst []byte) []byte { dst = append(dst, 'P') sp := len(dst) @@ -68,6 +72,7 @@ func (src *Parse) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *Parse) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/parse_complete.go b/parse_complete.go index 462a89ba..c2d3a34d 100644 --- a/parse_complete.go +++ b/parse_complete.go @@ -6,8 +6,11 @@ import ( type ParseComplete struct{} +// Backend identifies this message as sendable by the PostgreSQL backend. func (*ParseComplete) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *ParseComplete) Decode(src []byte) error { if len(src) != 0 { return &invalidMessageLenErr{messageType: "ParseComplete", expectedLen: 0, actualLen: len(src)} @@ -16,10 +19,12 @@ func (dst *ParseComplete) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *ParseComplete) Encode(dst []byte) []byte { return append(dst, '1', 0, 0, 0, 4) } +// MarshalJSON implements encoding/json.Marshaler. func (src *ParseComplete) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/password_message.go b/password_message.go index 30377cbe..b01316e9 100644 --- a/password_message.go +++ b/password_message.go @@ -11,8 +11,11 @@ type PasswordMessage struct { Password string } +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*PasswordMessage) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *PasswordMessage) Decode(src []byte) error { buf := bytes.NewBuffer(src) @@ -25,6 +28,7 @@ func (dst *PasswordMessage) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *PasswordMessage) Encode(dst []byte) []byte { dst = append(dst, 'p') dst = pgio.AppendInt32(dst, int32(4+len(src.Password)+1)) @@ -35,6 +39,7 @@ func (src *PasswordMessage) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *PasswordMessage) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/portal_suspended.go b/portal_suspended.go index dc81b027..5603d95e 100644 --- a/portal_suspended.go +++ b/portal_suspended.go @@ -6,8 +6,11 @@ import ( type PortalSuspended struct{} +// Backend identifies this message as sendable by the PostgreSQL backend. func (*PortalSuspended) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *PortalSuspended) Decode(src []byte) error { if len(src) != 0 { return &invalidMessageLenErr{messageType: "PortalSuspended", expectedLen: 0, actualLen: len(src)} @@ -16,10 +19,12 @@ func (dst *PortalSuspended) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *PortalSuspended) Encode(dst []byte) []byte { return append(dst, 's', 0, 0, 0, 4) } +// MarshalJSON implements encoding/json.Marshaler. func (src *PortalSuspended) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/query.go b/query.go index 16228cb4..17377dfb 100644 --- a/query.go +++ b/query.go @@ -11,8 +11,11 @@ type Query struct { String string } +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*Query) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *Query) Decode(src []byte) error { i := bytes.IndexByte(src, 0) if i != len(src)-1 { @@ -24,6 +27,7 @@ func (dst *Query) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *Query) Encode(dst []byte) []byte { dst = append(dst, 'Q') dst = pgio.AppendInt32(dst, int32(4+len(src.String)+1)) @@ -34,6 +38,7 @@ func (src *Query) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *Query) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/ready_for_query.go b/ready_for_query.go index 63b902bd..65f7d8c1 100644 --- a/ready_for_query.go +++ b/ready_for_query.go @@ -8,8 +8,11 @@ type ReadyForQuery struct { TxStatus byte } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*ReadyForQuery) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *ReadyForQuery) Decode(src []byte) error { if len(src) != 1 { return &invalidMessageLenErr{messageType: "ReadyForQuery", expectedLen: 1, actualLen: len(src)} @@ -20,10 +23,12 @@ func (dst *ReadyForQuery) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *ReadyForQuery) Encode(dst []byte) []byte { return append(dst, 'Z', 0, 0, 0, 5, src.TxStatus) } +// MarshalJSON implements encoding/json.Marshaler. func (src *ReadyForQuery) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/row_description.go b/row_description.go index 1b1734dc..87479188 100644 --- a/row_description.go +++ b/row_description.go @@ -27,8 +27,11 @@ type RowDescription struct { Fields []FieldDescription } +// Backend identifies this message as sendable by the PostgreSQL backend. func (*RowDescription) Backend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *RowDescription) Decode(src []byte) error { if len(src) < 2 { @@ -74,6 +77,7 @@ func (dst *RowDescription) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *RowDescription) Encode(dst []byte) []byte { dst = append(dst, 'T') sp := len(dst) @@ -97,6 +101,7 @@ func (src *RowDescription) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *RowDescription) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/sasl_initial_response.go b/sasl_initial_response.go index 63766131..b9459e16 100644 --- a/sasl_initial_response.go +++ b/sasl_initial_response.go @@ -14,8 +14,11 @@ type SASLInitialResponse struct { Data []byte } +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*SASLInitialResponse) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *SASLInitialResponse) Decode(src []byte) error { *dst = SASLInitialResponse{} @@ -35,6 +38,7 @@ func (dst *SASLInitialResponse) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *SASLInitialResponse) Encode(dst []byte) []byte { dst = append(dst, 'p') sp := len(dst) @@ -51,6 +55,7 @@ func (src *SASLInitialResponse) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *SASLInitialResponse) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/sasl_response.go b/sasl_response.go index 1e8d3bd3..ef893437 100644 --- a/sasl_response.go +++ b/sasl_response.go @@ -11,13 +11,17 @@ type SASLResponse struct { Data []byte } +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*SASLResponse) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *SASLResponse) Decode(src []byte) error { *dst = SASLResponse{Data: src} return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *SASLResponse) Encode(dst []byte) []byte { dst = append(dst, 'p') dst = pgio.AppendInt32(dst, int32(4+len(src.Data))) @@ -27,6 +31,7 @@ func (src *SASLResponse) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *SASLResponse) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/startup_message.go b/startup_message.go index 93a3d992..d9f04d17 100644 --- a/startup_message.go +++ b/startup_message.go @@ -19,8 +19,11 @@ type StartupMessage struct { Parameters map[string]string } +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*StartupMessage) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *StartupMessage) Decode(src []byte) error { if len(src) < 4 { return errors.Errorf("startup message too short") @@ -66,6 +69,7 @@ func (dst *StartupMessage) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *StartupMessage) Encode(dst []byte) []byte { sp := len(dst) dst = pgio.AppendInt32(dst, -1) @@ -84,6 +88,7 @@ func (src *StartupMessage) Encode(dst []byte) []byte { return dst } +// MarshalJSON implements encoding/json.Marshaler. func (src *StartupMessage) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/sync.go b/sync.go index 85f4749a..a058e8c9 100644 --- a/sync.go +++ b/sync.go @@ -6,8 +6,11 @@ import ( type Sync struct{} +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*Sync) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *Sync) Decode(src []byte) error { if len(src) != 0 { return &invalidMessageLenErr{messageType: "Sync", expectedLen: 0, actualLen: len(src)} @@ -16,10 +19,12 @@ func (dst *Sync) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *Sync) Encode(dst []byte) []byte { return append(dst, 'S', 0, 0, 0, 4) } +// MarshalJSON implements encoding/json.Marshaler. func (src *Sync) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string diff --git a/terminate.go b/terminate.go index 0a3310da..6c9d5b1a 100644 --- a/terminate.go +++ b/terminate.go @@ -6,8 +6,11 @@ import ( type Terminate struct{} +// Frontend identifies this message as sendable by a PostgreSQL frontend. func (*Terminate) Frontend() {} +// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message +// type identifier and 4 byte message length. func (dst *Terminate) Decode(src []byte) error { if len(src) != 0 { return &invalidMessageLenErr{messageType: "Terminate", expectedLen: 0, actualLen: len(src)} @@ -16,10 +19,12 @@ func (dst *Terminate) Decode(src []byte) error { return nil } +// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. func (src *Terminate) Encode(dst []byte) []byte { return append(dst, 'X', 0, 0, 0, 4) } +// MarshalJSON implements encoding/json.Marshaler. func (src *Terminate) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type string