/*
Mender API

Combined API specification for the features of the different Mender backend services, suitable for code generation applications 

API version: 1
Contact: support@mender.io
*/

// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.

package client

import (
	"encoding/json"
	"fmt"
	"gopkg.in/validator.v2"
)

// AttributeValue - The current value of the attribute.  Attribute type is implicit, inferred from the JSON type.  Supported types: number, string, array of numbers, array of strings. Mixed arrays are not allowed. 
type AttributeValue struct {
	ArrayOfFloat32 *[]float32
	ArrayOfString *[]string
	Float32 *float32
	String *string
}

// []float32AsAttributeValue is a convenience function that returns []float32 wrapped in AttributeValue
func ArrayOfFloat32AsAttributeValue(v *[]float32) AttributeValue {
	return AttributeValue{
		ArrayOfFloat32: v,
	}
}

// []stringAsAttributeValue is a convenience function that returns []string wrapped in AttributeValue
func ArrayOfStringAsAttributeValue(v *[]string) AttributeValue {
	return AttributeValue{
		ArrayOfString: v,
	}
}

// float32AsAttributeValue is a convenience function that returns float32 wrapped in AttributeValue
func Float32AsAttributeValue(v *float32) AttributeValue {
	return AttributeValue{
		Float32: v,
	}
}

// stringAsAttributeValue is a convenience function that returns string wrapped in AttributeValue
func StringAsAttributeValue(v *string) AttributeValue {
	return AttributeValue{
		String: v,
	}
}


// Unmarshal JSON data into one of the pointers in the struct
func (dst *AttributeValue) UnmarshalJSON(data []byte) error {
	var err error
	match := 0
	// try to unmarshal data into ArrayOfFloat32
	err = newStrictDecoder(data).Decode(&dst.ArrayOfFloat32)
	if err == nil {
		jsonArrayOfFloat32, _ := json.Marshal(dst.ArrayOfFloat32)
		if string(jsonArrayOfFloat32) == "{}" { // empty struct
			dst.ArrayOfFloat32 = nil
		} else {
			if err = validator.Validate(dst.ArrayOfFloat32); err != nil {
				dst.ArrayOfFloat32 = nil
			} else {
				match++
			}
		}
	} else {
		dst.ArrayOfFloat32 = nil
	}

	// try to unmarshal data into ArrayOfString
	err = newStrictDecoder(data).Decode(&dst.ArrayOfString)
	if err == nil {
		jsonArrayOfString, _ := json.Marshal(dst.ArrayOfString)
		if string(jsonArrayOfString) == "{}" { // empty struct
			dst.ArrayOfString = nil
		} else {
			if err = validator.Validate(dst.ArrayOfString); err != nil {
				dst.ArrayOfString = nil
			} else {
				match++
			}
		}
	} else {
		dst.ArrayOfString = nil
	}

	// try to unmarshal data into Float32
	err = newStrictDecoder(data).Decode(&dst.Float32)
	if err == nil {
		jsonFloat32, _ := json.Marshal(dst.Float32)
		if string(jsonFloat32) == "{}" { // empty struct
			dst.Float32 = nil
		} else {
			if err = validator.Validate(dst.Float32); err != nil {
				dst.Float32 = nil
			} else {
				match++
			}
		}
	} else {
		dst.Float32 = nil
	}

	// try to unmarshal data into String
	err = newStrictDecoder(data).Decode(&dst.String)
	if err == nil {
		jsonString, _ := json.Marshal(dst.String)
		if string(jsonString) == "{}" { // empty struct
			dst.String = nil
		} else {
			if err = validator.Validate(dst.String); err != nil {
				dst.String = nil
			} else {
				match++
			}
		}
	} else {
		dst.String = nil
	}

	if match > 1 { // more than 1 match
		// reset to nil
		dst.ArrayOfFloat32 = nil
		dst.ArrayOfString = nil
		dst.Float32 = nil
		dst.String = nil

		return fmt.Errorf("data matches more than one schema in oneOf(AttributeValue)")
	} else if match == 1 {
		return nil // exactly one match
	} else { // no match
		return fmt.Errorf("data failed to match schemas in oneOf(AttributeValue)")
	}
}

// Marshal data from the first non-nil pointers in the struct to JSON
func (src AttributeValue) MarshalJSON() ([]byte, error) {
	if src.ArrayOfFloat32 != nil {
		return json.Marshal(&src.ArrayOfFloat32)
	}

	if src.ArrayOfString != nil {
		return json.Marshal(&src.ArrayOfString)
	}

	if src.Float32 != nil {
		return json.Marshal(&src.Float32)
	}

	if src.String != nil {
		return json.Marshal(&src.String)
	}

	return nil, nil // no data in oneOf schemas
}

// Get the actual instance
func (obj *AttributeValue) GetActualInstance() (interface{}) {
	if obj == nil {
		return nil
	}
	if obj.ArrayOfFloat32 != nil {
		return obj.ArrayOfFloat32
	}

	if obj.ArrayOfString != nil {
		return obj.ArrayOfString
	}

	if obj.Float32 != nil {
		return obj.Float32
	}

	if obj.String != nil {
		return obj.String
	}

	// all schemas are nil
	return nil
}

// Get the actual instance value
func (obj AttributeValue) GetActualInstanceValue() (interface{}) {
	if obj.ArrayOfFloat32 != nil {
		return *obj.ArrayOfFloat32
	}

	if obj.ArrayOfString != nil {
		return *obj.ArrayOfString
	}

	if obj.Float32 != nil {
		return *obj.Float32
	}

	if obj.String != nil {
		return *obj.String
	}

	// all schemas are nil
	return nil
}

type NullableAttributeValue struct {
	value *AttributeValue
	isSet bool
}

func (v NullableAttributeValue) Get() *AttributeValue {
	return v.value
}

func (v *NullableAttributeValue) Set(val *AttributeValue) {
	v.value = val
	v.isSet = true
}

func (v NullableAttributeValue) IsSet() bool {
	return v.isSet
}

func (v *NullableAttributeValue) Unset() {
	v.value = nil
	v.isSet = false
}

func NewNullableAttributeValue(val *AttributeValue) *NullableAttributeValue {
	return &NullableAttributeValue{value: val, isSet: true}
}

func (v NullableAttributeValue) MarshalJSON() ([]byte, error) {
	return json.Marshal(v.value)
}

func (v *NullableAttributeValue) UnmarshalJSON(src []byte) error {
	v.isSet = true
	return json.Unmarshal(src, &v.value)
}


