package pgproto3

import (
	
	
	

	
)

type FunctionCallResponse struct {
	Result []byte
}

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

// 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 ( *FunctionCallResponse) ( []byte) error {
	if len() < 4 {
		return &invalidMessageFormatErr{messageType: "FunctionCallResponse"}
	}
	 := 0
	 := int(binary.BigEndian.Uint32([:]))
	 += 4

	if  == -1 {
		.Result = nil
		return nil
	}

	if len([:]) !=  {
		return &invalidMessageFormatErr{messageType: "FunctionCallResponse"}
	}

	.Result = [:]
	return nil
}

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

	if .Result == nil {
		 = pgio.AppendInt32(, -1)
	} else {
		 = pgio.AppendInt32(, int32(len(.Result)))
		 = append(, .Result...)
	}

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

	return 
}

// MarshalJSON implements encoding/json.Marshaler.
func ( FunctionCallResponse) () ([]byte, error) {
	var  map[string]string
	var  bool
	for ,  := range .Result {
		if  < 32 {
			 = true
			break
		}
	}

	if  {
		 = map[string]string{"binary": hex.EncodeToString(.Result)}
	} else {
		 = map[string]string{"text": string(.Result)}
	}

	return json.Marshal(struct {
		   string
		 map[string]string
	}{
		:   "FunctionCallResponse",
		: ,
	})
}

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

	var  struct {
		 map[string]string
	}
	 := json.Unmarshal(, &)
	if  != nil {
		return 
	}
	.Result,  = getValueFromJSON(.)
	return 
}