gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

parameter_status.go (1431B)


      1 package pgproto3
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/json"
      6 
      7 	"github.com/jackc/pgio"
      8 )
      9 
     10 type ParameterStatus struct {
     11 	Name  string
     12 	Value string
     13 }
     14 
     15 // Backend identifies this message as sendable by the PostgreSQL backend.
     16 func (*ParameterStatus) Backend() {}
     17 
     18 // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
     19 // type identifier and 4 byte message length.
     20 func (dst *ParameterStatus) Decode(src []byte) error {
     21 	buf := bytes.NewBuffer(src)
     22 
     23 	b, err := buf.ReadBytes(0)
     24 	if err != nil {
     25 		return err
     26 	}
     27 	name := string(b[:len(b)-1])
     28 
     29 	b, err = buf.ReadBytes(0)
     30 	if err != nil {
     31 		return err
     32 	}
     33 	value := string(b[:len(b)-1])
     34 
     35 	*dst = ParameterStatus{Name: name, Value: value}
     36 	return nil
     37 }
     38 
     39 // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
     40 func (src *ParameterStatus) Encode(dst []byte) []byte {
     41 	dst = append(dst, 'S')
     42 	sp := len(dst)
     43 	dst = pgio.AppendInt32(dst, -1)
     44 
     45 	dst = append(dst, src.Name...)
     46 	dst = append(dst, 0)
     47 	dst = append(dst, src.Value...)
     48 	dst = append(dst, 0)
     49 
     50 	pgio.SetInt32(dst[sp:], int32(len(dst[sp:])))
     51 
     52 	return dst
     53 }
     54 
     55 // MarshalJSON implements encoding/json.Marshaler.
     56 func (ps ParameterStatus) MarshalJSON() ([]byte, error) {
     57 	return json.Marshal(struct {
     58 		Type  string
     59 		Name  string
     60 		Value string
     61 	}{
     62 		Type:  "ParameterStatus",
     63 		Name:  ps.Name,
     64 		Value: ps.Value,
     65 	})
     66 }