package pgproto3

import (
	
	

	
)

type NegotiateProtocolVersion struct {
	NewestMinorProtocol uint32
	UnrecognizedOptions []string
}

// Backend identifies this message as sendable by the PostgreSQL backend.
func (*NegotiateProtocolVersion) () {}

// 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 ( *NegotiateProtocolVersion) ( []byte) error {
	if len() < 8 {
		return &invalidMessageLenErr{messageType: "NegotiateProtocolVersion", expectedLen: 8, actualLen: len()}
	}

	.NewestMinorProtocol = binary.BigEndian.Uint32([:4])
	 := int(binary.BigEndian.Uint32([4:8]))

	 := 8

	// Use the remaining message size as an upper bound for capacity to prevent
	// malicious optionCount values from causing excessive memory allocation.
	 := 
	if  := len() - ;  >  {
		 = 
	}
	.UnrecognizedOptions = make([]string, 0, )
	for  := 0;  < ; ++ {
		if  >= len() {
			return &invalidMessageFormatErr{messageType: "NegotiateProtocolVersion"}
		}
		 := 
		for  < len() && [] != 0 {
			++
		}
		if  >= len() {
			return &invalidMessageFormatErr{messageType: "NegotiateProtocolVersion"}
		}
		.UnrecognizedOptions = append(.UnrecognizedOptions, string([:]))
		 =  + 1
	}

	return nil
}

// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
func ( *NegotiateProtocolVersion) ( []byte) ([]byte, error) {
	,  := beginMessage(, 'v')
	 = pgio.AppendUint32(, .NewestMinorProtocol)
	 = pgio.AppendUint32(, uint32(len(.UnrecognizedOptions)))
	for ,  := range .UnrecognizedOptions {
		 = append(, ...)
		 = append(, 0)
	}
	return finishMessage(, )
}

// MarshalJSON implements encoding/json.Marshaler.
func ( NegotiateProtocolVersion) () ([]byte, error) {
	return json.Marshal(struct {
		                string
		 uint32
		 []string
	}{
		:                "NegotiateProtocolVersion",
		: .NewestMinorProtocol,
		: .UnrecognizedOptions,
	})
}

// UnmarshalJSON implements encoding/json.Unmarshaler.
func ( *NegotiateProtocolVersion) ( []byte) error {
	var  struct {
		 uint32
		 []string
	}
	if  := json.Unmarshal(, &);  != nil {
		return 
	}

	.NewestMinorProtocol = .
	.UnrecognizedOptions = .
	return nil
}