package pgproto3

import (
	
	
	
	

	
)

// AuthenticationSASL is a message sent from the backend indicating that SASL authentication is required.
type AuthenticationSASL struct {
	AuthMechanisms []string
}

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

// Backend identifies this message as an authentication response.
func (*AuthenticationSASL) () {}

// 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 ( *AuthenticationSASL) ( []byte) error {
	if len() < 4 {
		return errors.New("authentication message too short")
	}

	 := binary.BigEndian.Uint32()

	if  != AuthTypeSASL {
		return errors.New("bad auth type")
	}

	 := [4:]
	for len() > 1 {
		 := bytes.IndexByte(, 0)
		if  == -1 {
			return &invalidMessageFormatErr{messageType: "AuthenticationSASL", details: "unterminated string"}
		}
		.AuthMechanisms = append(.AuthMechanisms, 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 ( *AuthenticationSASL) ( []byte) []byte {
	 = append(, 'R')
	 := len()
	 = pgio.AppendInt32(, -1)
	 = pgio.AppendUint32(, AuthTypeSASL)

	for ,  := range .AuthMechanisms {
		 = append(, []byte()...)
		 = append(, 0)
	}
	 = append(, 0)

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

	return 
}

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