package pgproto3
import (
)
const (
ProtocolVersion30 = 196608
ProtocolVersion32 = 196610
ProtocolVersionNumber = ProtocolVersion30
)
type StartupMessage struct {
ProtocolVersion uint32
Parameters map[string]string
}
func (*StartupMessage) () {}
func ( *StartupMessage) ( []byte) error {
if len() < 4 {
return errors.New("startup message too short")
}
.ProtocolVersion = binary.BigEndian.Uint32()
:= 4
if .ProtocolVersion != ProtocolVersion30 && .ProtocolVersion != ProtocolVersion32 {
return fmt.Errorf("Bad startup message version number. Expected %d or %d, got %d", ProtocolVersion30, ProtocolVersion32, .ProtocolVersion)
}
.Parameters = make(map[string]string)
for {
:= bytes.IndexByte([:], 0)
if < 0 {
return &invalidMessageFormatErr{messageType: "StartupMessage"}
}
:= string([ : +])
+= + 1
= bytes.IndexByte([:], 0)
if < 0 {
return &invalidMessageFormatErr{messageType: "StartupMessage"}
}
:= string([ : +])
+= + 1
.Parameters[] =
if len([:]) == 1 {
if [] != 0 {
return fmt.Errorf("Bad startup message last byte. Expected 0, got %d", [])
}
break
}
}
return nil
}
func ( *StartupMessage) ( []byte) ([]byte, error) {
:= len()
= pgio.AppendInt32(, -1)
= pgio.AppendUint32(, .ProtocolVersion)
for , := range .Parameters {
= append(, ...)
= append(, 0)
= append(, ...)
= append(, 0)
}
= append(, 0)
return finishMessage(, )
}
func ( StartupMessage) () ([]byte, error) {
return json.Marshal(struct {
string
uint32
map[string]string
}{
: "StartupMessage",
: .ProtocolVersion,
: .Parameters,
})
}