...

Source file src/github.com/jackc/pgproto3/v2/command_complete.go

Documentation: github.com/jackc/pgproto3/v2

     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 != len(src)-1 {
    20  		return &invalidMessageFormatErr{messageType: "CommandComplete"}
    21  	}
    22  
    23  	dst.CommandTag = src[:idx]
    24  
    25  	return nil
    26  }
    27  
    28  // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
    29  func (src *CommandComplete) Encode(dst []byte) ([]byte, error) {
    30  	dst, sp := beginMessage(dst, 'C')
    31  	dst = append(dst, src.CommandTag...)
    32  	dst = append(dst, 0)
    33  	return finishMessage(dst, sp)
    34  }
    35  
    36  // MarshalJSON implements encoding/json.Marshaler.
    37  func (src CommandComplete) MarshalJSON() ([]byte, error) {
    38  	return json.Marshal(struct {
    39  		Type       string
    40  		CommandTag string
    41  	}{
    42  		Type:       "CommandComplete",
    43  		CommandTag: string(src.CommandTag),
    44  	})
    45  }
    46  
    47  // UnmarshalJSON implements encoding/json.Unmarshaler.
    48  func (dst *CommandComplete) UnmarshalJSON(data []byte) error {
    49  	// Ignore null, like in the main JSON package.
    50  	if string(data) == "null" {
    51  		return nil
    52  	}
    53  
    54  	var msg struct {
    55  		CommandTag string
    56  	}
    57  	if err := json.Unmarshal(data, &msg); err != nil {
    58  		return err
    59  	}
    60  
    61  	dst.CommandTag = []byte(msg.CommandTag)
    62  	return nil
    63  }
    64  

View as plain text