package pgproto3

import (
	
	
	
	
	

	
)

const ProtocolVersionNumber = 196608 // 3.0

type StartupMessage struct {
	ProtocolVersion uint32
	Parameters      map[string]string
}

// Frontend identifies this message as sendable by a PostgreSQL frontend.
func (*StartupMessage) () {}

// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
// type identifier and 4 byte message length.
func ( *StartupMessage) ( []byte) error {
	if len() < 4 {
		return errors.New("startup message too short")
	}

	.ProtocolVersion = binary.BigEndian.Uint32()
	 := 4

	if .ProtocolVersion != ProtocolVersionNumber {
		return fmt.Errorf("Bad startup message version number. Expected %d, got %d", ProtocolVersionNumber, .ProtocolVersion)
	}

	.Parameters = make(map[string]string)
	for {
		 := bytes.IndexByte([:], 0)
		if  < 0 {
			return &invalidMessageFormatErr{messageType: "StartupMesage"}
		}
		 := string([ : +])
		 +=  + 1

		 = bytes.IndexByte([:], 0)
		if  < 0 {
			return &invalidMessageFormatErr{messageType: "StartupMesage"}
		}
		 := string([ : +])
		 +=  + 1

		.Parameters[] = 

		if len([:]) == 1 {
			if [] != 0 {
				return fmt.Errorf("Bad startup message last byte. Expected 0, got %d", [])
			}
			break
		}
	}

	return nil
}

// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
func ( *StartupMessage) ( []byte) []byte {
	 := len()
	 = pgio.AppendInt32(, -1)

	 = pgio.AppendUint32(, .ProtocolVersion)
	for ,  := range .Parameters {
		 = append(, ...)
		 = append(, 0)
		 = append(, ...)
		 = append(, 0)
	}
	 = append(, 0)

	pgio.SetInt32([:], int32(len([:])))

	return 
}

// MarshalJSON implements encoding/json.Marshaler.
func ( StartupMessage) () ([]byte, error) {
	return json.Marshal(struct {
		            string
		 uint32
		      map[string]string
	}{
		:            "StartupMessage",
		: .ProtocolVersion,
		:      .Parameters,
	})
}