/*
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"
)

// Credentials - struct for Credentials
type Credentials struct {
	AWSCredentials *AWSCredentials
	AzureSharedAccessSecret *AzureSharedAccessSecret
	HTTP *HTTP
}

// AWSCredentialsAsCredentials is a convenience function that returns AWSCredentials wrapped in Credentials
func AWSCredentialsAsCredentials(v *AWSCredentials) Credentials {
	return Credentials{
		AWSCredentials: v,
	}
}

// AzureSharedAccessSecretAsCredentials is a convenience function that returns AzureSharedAccessSecret wrapped in Credentials
func AzureSharedAccessSecretAsCredentials(v *AzureSharedAccessSecret) Credentials {
	return Credentials{
		AzureSharedAccessSecret: v,
	}
}

// HTTPAsCredentials is a convenience function that returns HTTP wrapped in Credentials
func HTTPAsCredentials(v *HTTP) Credentials {
	return Credentials{
		HTTP: v,
	}
}


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

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

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

	if match > 1 { // more than 1 match
		// reset to nil
		dst.AWSCredentials = nil
		dst.AzureSharedAccessSecret = nil
		dst.HTTP = nil

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

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

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

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

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

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

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

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

	// all schemas are nil
	return nil
}

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

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

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

	// all schemas are nil
	return nil
}

type NullableCredentials struct {
	value *Credentials
	isSet bool
}

func (v NullableCredentials) Get() *Credentials {
	return v.value
}

func (v *NullableCredentials) Set(val *Credentials) {
	v.value = val
	v.isSet = true
}

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

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

func NewNullableCredentials(val *Credentials) *NullableCredentials {
	return &NullableCredentials{value: val, isSet: true}
}

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

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


