...

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

Documentation: github.com/jackc/pgproto3/v2

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

View as plain text