// 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 UnionNullStringLongIntFloatDoubleBoolTypeEnum int

const (
	UnionNullStringLongIntFloatDoubleBoolTypeEnumString UnionNullStringLongIntFloatDoubleBoolTypeEnum = 1

	UnionNullStringLongIntFloatDoubleBoolTypeEnumLong UnionNullStringLongIntFloatDoubleBoolTypeEnum = 2

	UnionNullStringLongIntFloatDoubleBoolTypeEnumInt UnionNullStringLongIntFloatDoubleBoolTypeEnum = 3

	UnionNullStringLongIntFloatDoubleBoolTypeEnumFloat UnionNullStringLongIntFloatDoubleBoolTypeEnum = 4

	UnionNullStringLongIntFloatDoubleBoolTypeEnumDouble UnionNullStringLongIntFloatDoubleBoolTypeEnum = 5

	UnionNullStringLongIntFloatDoubleBoolTypeEnumBool UnionNullStringLongIntFloatDoubleBoolTypeEnum = 6
)

type UnionNullStringLongIntFloatDoubleBool struct {
	Null      *types.NullVal
	String    string
	Long      int64
	Int       int32
	Float     float32
	Double    float64
	Bool      bool
	UnionType UnionNullStringLongIntFloatDoubleBoolTypeEnum
}

func writeUnionNullStringLongIntFloatDoubleBool(r *UnionNullStringLongIntFloatDoubleBool, 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 UnionNullStringLongIntFloatDoubleBoolTypeEnumString:
		return vm.WriteString(r.String, w)
	case UnionNullStringLongIntFloatDoubleBoolTypeEnumLong:
		return vm.WriteLong(r.Long, w)
	case UnionNullStringLongIntFloatDoubleBoolTypeEnumInt:
		return vm.WriteInt(r.Int, w)
	case UnionNullStringLongIntFloatDoubleBoolTypeEnumFloat:
		return vm.WriteFloat(r.Float, w)
	case UnionNullStringLongIntFloatDoubleBoolTypeEnumDouble:
		return vm.WriteDouble(r.Double, w)
	case UnionNullStringLongIntFloatDoubleBoolTypeEnumBool:
		return vm.WriteBool(r.Bool, w)
	}
	return fmt.Errorf("invalid value for *UnionNullStringLongIntFloatDoubleBool")
}

func NewUnionNullStringLongIntFloatDoubleBool() *UnionNullStringLongIntFloatDoubleBool {
	return &UnionNullStringLongIntFloatDoubleBool{}
}

func (r *UnionNullStringLongIntFloatDoubleBool) Serialize(w io.Writer) error {
	return writeUnionNullStringLongIntFloatDoubleBool(r, w)
}

func DeserializeUnionNullStringLongIntFloatDoubleBool(r io.Reader) (*UnionNullStringLongIntFloatDoubleBool, error) {
	t := NewUnionNullStringLongIntFloatDoubleBool()
	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 DeserializeUnionNullStringLongIntFloatDoubleBoolFromSchema(r io.Reader, schema string) (*UnionNullStringLongIntFloatDoubleBool, error) {
	t := NewUnionNullStringLongIntFloatDoubleBool()
	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 *UnionNullStringLongIntFloatDoubleBool) Schema() string {
	return "[\"null\",\"string\",\"long\",\"int\",\"float\",\"double\",\"boolean\"]"
}

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

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

	r.UnionType = (UnionNullStringLongIntFloatDoubleBoolTypeEnum)(v)
}

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

	switch i {
	case 0:
		return r.Null
	case 1:
		return &types.String{Target: (&r.String)}
	case 2:
		return &types.Long{Target: (&r.Long)}
	case 3:
		return &types.Int{Target: (&r.Int)}
	case 4:
		return &types.Float{Target: (&r.Float)}
	case 5:
		return &types.Double{Target: (&r.Double)}
	case 6:
		return &types.Boolean{Target: (&r.Bool)}
	}
	panic("Unknown field index")
}
func (_ *UnionNullStringLongIntFloatDoubleBool) NullField(i int)  { panic("Unsupported operation") }
func (_ *UnionNullStringLongIntFloatDoubleBool) HintSize(i int)   { panic("Unsupported operation") }
func (_ *UnionNullStringLongIntFloatDoubleBool) SetDefault(i int) { panic("Unsupported operation") }
func (_ *UnionNullStringLongIntFloatDoubleBool) AppendMap(key string) types.Field {
	panic("Unsupported operation")
}
func (_ *UnionNullStringLongIntFloatDoubleBool) AppendArray() types.Field {
	panic("Unsupported operation")
}
func (_ *UnionNullStringLongIntFloatDoubleBool) Finalize() {}

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

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

	switch r.UnionType {
	case UnionNullStringLongIntFloatDoubleBoolTypeEnumString:
		return json.Marshal(map[string]interface{}{"string": r.String})
	case UnionNullStringLongIntFloatDoubleBoolTypeEnumLong:
		return json.Marshal(map[string]interface{}{"long": r.Long})
	case UnionNullStringLongIntFloatDoubleBoolTypeEnumInt:
		return json.Marshal(map[string]interface{}{"int": r.Int})
	case UnionNullStringLongIntFloatDoubleBoolTypeEnumFloat:
		return json.Marshal(map[string]interface{}{"float": r.Float})
	case UnionNullStringLongIntFloatDoubleBoolTypeEnumDouble:
		return json.Marshal(map[string]interface{}{"double": r.Double})
	case UnionNullStringLongIntFloatDoubleBoolTypeEnumBool:
		return json.Marshal(map[string]interface{}{"boolean": r.Bool})
	}
	return nil, fmt.Errorf("invalid value for *UnionNullStringLongIntFloatDoubleBool")
}

func (r *UnionNullStringLongIntFloatDoubleBool) 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["string"]; ok {
		r.UnionType = 1
		return json.Unmarshal([]byte(value), &r.String)
	}
	if value, ok := fields["long"]; ok {
		r.UnionType = 2
		return json.Unmarshal([]byte(value), &r.Long)
	}
	if value, ok := fields["int"]; ok {
		r.UnionType = 3
		return json.Unmarshal([]byte(value), &r.Int)
	}
	if value, ok := fields["float"]; ok {
		r.UnionType = 4
		return json.Unmarshal([]byte(value), &r.Float)
	}
	if value, ok := fields["double"]; ok {
		r.UnionType = 5
		return json.Unmarshal([]byte(value), &r.Double)
	}
	if value, ok := fields["boolean"]; ok {
		r.UnionType = 6
		return json.Unmarshal([]byte(value), &r.Bool)
	}
	return fmt.Errorf("invalid value for *UnionNullStringLongIntFloatDoubleBool")
}
