...

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

Documentation: github.com/jackc/pgproto3/v2

     1  package pgproto3
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"errors"
     7  )
     8  
     9  type Describe struct {
    10  	ObjectType byte // 'S' = prepared statement, 'P' = portal
    11  	Name       string
    12  }
    13  
    14  // Frontend identifies this message as sendable by a PostgreSQL frontend.
    15  func (*Describe) 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 *Describe) Decode(src []byte) error {
    20  	if len(src) < 2 {
    21  		return &invalidMessageFormatErr{messageType: "Describe"}
    22  	}
    23  
    24  	dst.ObjectType = src[0]
    25  	rp := 1
    26  
    27  	idx := bytes.IndexByte(src[rp:], 0)
    28  	if idx != len(src[rp:])-1 {
    29  		return &invalidMessageFormatErr{messageType: "Describe"}
    30  	}
    31  
    32  	dst.Name = string(src[rp : len(src)-1])
    33  
    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 *Describe) Encode(dst []byte) ([]byte, error) {
    39  	dst, sp := beginMessage(dst, 'D')
    40  	dst = append(dst, src.ObjectType)
    41  	dst = append(dst, src.Name...)
    42  	dst = append(dst, 0)
    43  	return finishMessage(dst, sp)
    44  }
    45  
    46  // MarshalJSON implements encoding/json.Marshaler.
    47  func (src Describe) MarshalJSON() ([]byte, error) {
    48  	return json.Marshal(struct {
    49  		Type       string
    50  		ObjectType string
    51  		Name       string
    52  	}{
    53  		Type:       "Describe",
    54  		ObjectType: string(src.ObjectType),
    55  		Name:       src.Name,
    56  	})
    57  }
    58  
    59  // UnmarshalJSON implements encoding/json.Unmarshaler.
    60  func (dst *Describe) UnmarshalJSON(data []byte) error {
    61  	// Ignore null, like in the main JSON package.
    62  	if string(data) == "null" {
    63  		return nil
    64  	}
    65  
    66  	var msg struct {
    67  		ObjectType string
    68  		Name       string
    69  	}
    70  	if err := json.Unmarshal(data, &msg); err != nil {
    71  		return err
    72  	}
    73  	if len(msg.ObjectType) != 1 {
    74  		return errors.New("invalid length for Describe.ObjectType")
    75  	}
    76  
    77  	dst.ObjectType = byte(msg.ObjectType[0])
    78  	dst.Name = msg.Name
    79  	return nil
    80  }
    81  

View as plain text