package pgproto3

import (
	
	
	
	

	
)

type SASLInitialResponse struct {
	AuthMechanism string
	Data          []byte
}

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

// 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 ( *SASLInitialResponse) ( []byte) error {
	* = SASLInitialResponse{}

	 := 0

	 := bytes.IndexByte(, 0)
	if  < 0 {
		return errors.New("invalid SASLInitialResponse")
	}

	.AuthMechanism = string([:])
	 =  + 1

	 += 4 // The rest of the message is data so we can just skip the size
	.Data = [:]

	return nil
}

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

	 = append(, []byte(.AuthMechanism)...)
	 = append(, 0)

	 = pgio.AppendInt32(, int32(len(.Data)))
	 = append(, .Data...)

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

	return 
}

// MarshalJSON implements encoding/json.Marshaler.
func ( SASLInitialResponse) () ([]byte, error) {
	return json.Marshal(struct {
		          string
		 string
		          string
	}{
		:          "SASLInitialResponse",
		: .AuthMechanism,
		:          string(.Data),
	})
}

// UnmarshalJSON implements encoding/json.Unmarshaler.
func ( *SASLInitialResponse) ( []byte) error {
	// Ignore null, like in the main JSON package.
	if string() == "null" {
		return nil
	}

	var  struct {
		 string
		          string
	}
	if  := json.Unmarshal(, &);  != nil {
		return 
	}
	.AuthMechanism = .
	if . != "" {
		,  := hex.DecodeString(.)
		if  != nil {
			return 
		}
		.Data = 
	}
	return nil
}