// Code generated by github.com/actgardner/gogen-avro/v10. DO NOT EDIT.
/*
 * SOURCE:
 *     schema.avsc
 */
package avro

import (
	"encoding/json"
	"fmt"
	"io"

	"github.com/actgardner/gogen-avro/v10/compiler"
	"github.com/actgardner/gogen-avro/v10/vm"
	"github.com/actgardner/gogen-avro/v10/vm/types"
)

type UnionNullHeaderworksDatatypeUUIDTypeEnum int

const (
	UnionNullHeaderworksDatatypeUUIDTypeEnumHeaderworksDatatypeUUID UnionNullHeaderworksDatatypeUUIDTypeEnum = 1
)

type UnionNullHeaderworksDatatypeUUID struct {
	Null                    *types.NullVal
	HeaderworksDatatypeUUID HeaderworksDatatypeUUID
	UnionType               UnionNullHeaderworksDatatypeUUIDTypeEnum
}

func writeUnionNullHeaderworksDatatypeUUID(r *UnionNullHeaderworksDatatypeUUID, w io.Writer) error {

	if r == nil {
		err := vm.WriteLong(0, w)
		return err
	}

	err := vm.WriteLong(int64(r.UnionType), w)
	if err != nil {
		return err
	}
	switch r.UnionType {
	case UnionNullHeaderworksDatatypeUUIDTypeEnumHeaderworksDatatypeUUID:
		return writeHeaderworksDatatypeUUID(r.HeaderworksDatatypeUUID, w)
	}
	return fmt.Errorf("invalid value for *UnionNullHeaderworksDatatypeUUID")
}

func NewUnionNullHeaderworksDatatypeUUID() *UnionNullHeaderworksDatatypeUUID {
	return &UnionNullHeaderworksDatatypeUUID{}
}

func (r *UnionNullHeaderworksDatatypeUUID) Serialize(w io.Writer) error {
	return writeUnionNullHeaderworksDatatypeUUID(r, w)
}

func DeserializeUnionNullHeaderworksDatatypeUUID(r io.Reader) (*UnionNullHeaderworksDatatypeUUID, error) {
	t := NewUnionNullHeaderworksDatatypeUUID()
	deser, err := compiler.CompileSchemaBytes([]byte(t.Schema()), []byte(t.Schema()))
	if err != nil {
		return t, err
	}

	err = vm.Eval(r, deser, t)

	if err != nil {
		return t, err
	}
	return t, err
}

func DeserializeUnionNullHeaderworksDatatypeUUIDFromSchema(r io.Reader, schema string) (*UnionNullHeaderworksDatatypeUUID, error) {
	t := NewUnionNullHeaderworksDatatypeUUID()
	deser, err := compiler.CompileSchemaBytes([]byte(schema), []byte(t.Schema()))
	if err != nil {
		return t, err
	}

	err = vm.Eval(r, deser, t)

	if err != nil {
		return t, err
	}
	return t, err
}

func (r *UnionNullHeaderworksDatatypeUUID) Schema() string {
	return "[\"null\",{\"doc\":\"A Universally Unique Identifier, in canonical form in lowercase. Example: de305d54-75b4-431b-adb2-eb6b9e546014\",\"fields\":[{\"default\":\"\",\"name\":\"uuid\",\"type\":\"string\"}],\"name\":\"UUID\",\"namespace\":\"headerworks.datatype\",\"type\":\"record\"}]"
}

func (_ *UnionNullHeaderworksDatatypeUUID) SetBoolean(v bool)   { panic("Unsupported operation") }
func (_ *UnionNullHeaderworksDatatypeUUID) SetInt(v int32)      { panic("Unsupported operation") }
func (_ *UnionNullHeaderworksDatatypeUUID) SetFloat(v float32)  { panic("Unsupported operation") }
func (_ *UnionNullHeaderworksDatatypeUUID) SetDouble(v float64) { panic("Unsupported operation") }
func (_ *UnionNullHeaderworksDatatypeUUID) SetBytes(v []byte)   { panic("Unsupported operation") }
func (_ *UnionNullHeaderworksDatatypeUUID) SetString(v string)  { panic("Unsupported operation") }

func (r *UnionNullHeaderworksDatatypeUUID) SetLong(v int64) {

	r.UnionType = (UnionNullHeaderworksDatatypeUUIDTypeEnum)(v)
}

func (r *UnionNullHeaderworksDatatypeUUID) Get(i int) types.Field {

	switch i {
	case 0:
		return r.Null
	case 1:
		r.HeaderworksDatatypeUUID = NewHeaderworksDatatypeUUID()
		return &types.Record{Target: (&r.HeaderworksDatatypeUUID)}
	}
	panic("Unknown field index")
}
func (_ *UnionNullHeaderworksDatatypeUUID) NullField(i int)  { panic("Unsupported operation") }
func (_ *UnionNullHeaderworksDatatypeUUID) HintSize(i int)   { panic("Unsupported operation") }
func (_ *UnionNullHeaderworksDatatypeUUID) SetDefault(i int) { panic("Unsupported operation") }
func (_ *UnionNullHeaderworksDatatypeUUID) AppendMap(key string) types.Field {
	panic("Unsupported operation")
}
func (_ *UnionNullHeaderworksDatatypeUUID) AppendArray() types.Field { panic("Unsupported operation") }
func (_ *UnionNullHeaderworksDatatypeUUID) Finalize()                {}

func (r *UnionNullHeaderworksDatatypeUUID) MarshalJSON() ([]byte, error) {

	if r == nil {
		return []byte("null"), nil
	}

	switch r.UnionType {
	case UnionNullHeaderworksDatatypeUUIDTypeEnumHeaderworksDatatypeUUID:
		return json.Marshal(map[string]interface{}{"headerworks.datatype.UUID": r.HeaderworksDatatypeUUID})
	}
	return nil, fmt.Errorf("invalid value for *UnionNullHeaderworksDatatypeUUID")
}

func (r *UnionNullHeaderworksDatatypeUUID) UnmarshalJSON(data []byte) error {

	var fields map[string]json.RawMessage
	if err := json.Unmarshal(data, &fields); err != nil {
		return err
	}
	if len(fields) > 1 {
		return fmt.Errorf("more than one type supplied for union")
	}
	if value, ok := fields["headerworks.datatype.UUID"]; ok {
		r.UnionType = 1
		return json.Unmarshal([]byte(value), &r.HeaderworksDatatypeUUID)
	}
	return fmt.Errorf("invalid value for *UnionNullHeaderworksDatatypeUUID")
}
