/*
Transfer webhooks

API version: 4
*/

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

package transferwebhook

import (
	"encoding/json"
	"fmt"
)

// TransferDataCategoryData - The relevant data according to the transfer category.
type TransferDataCategoryData struct {
	BankCategoryData     *BankCategoryData
	InternalCategoryData *InternalCategoryData
	IssuedCard           *IssuedCard
	PlatformPayment      *PlatformPayment
}

// BankCategoryDataAsTransferDataCategoryData is a convenience function that returns BankCategoryData wrapped in TransferDataCategoryData
func BankCategoryDataAsTransferDataCategoryData(v *BankCategoryData) TransferDataCategoryData {
	return TransferDataCategoryData{
		BankCategoryData: v,
	}
}

// InternalCategoryDataAsTransferDataCategoryData is a convenience function that returns InternalCategoryData wrapped in TransferDataCategoryData
func InternalCategoryDataAsTransferDataCategoryData(v *InternalCategoryData) TransferDataCategoryData {
	return TransferDataCategoryData{
		InternalCategoryData: v,
	}
}

// IssuedCardAsTransferDataCategoryData is a convenience function that returns IssuedCard wrapped in TransferDataCategoryData
func IssuedCardAsTransferDataCategoryData(v *IssuedCard) TransferDataCategoryData {
	return TransferDataCategoryData{
		IssuedCard: v,
	}
}

// PlatformPaymentAsTransferDataCategoryData is a convenience function that returns PlatformPayment wrapped in TransferDataCategoryData
func PlatformPaymentAsTransferDataCategoryData(v *PlatformPayment) TransferDataCategoryData {
	return TransferDataCategoryData{
		PlatformPayment: v,
	}
}

// Unmarshal JSON data into one of the pointers in the struct
func (dst *TransferDataCategoryData) UnmarshalJSON(data []byte) error {
	var err error
	match := 0
	// try to unmarshal data into BankCategoryData
	err = json.Unmarshal(data, &dst.BankCategoryData)
	if err == nil {
		jsonBankCategoryData, _ := json.Marshal(dst.BankCategoryData)
		if string(jsonBankCategoryData) == "{}" || !dst.BankCategoryData.isValidType() { // empty struct
			dst.BankCategoryData = nil
		} else {
			match++
		}
	} else {
		dst.BankCategoryData = nil
	}

	// try to unmarshal data into InternalCategoryData
	err = json.Unmarshal(data, &dst.InternalCategoryData)
	if err == nil {
		jsonInternalCategoryData, _ := json.Marshal(dst.InternalCategoryData)
		if string(jsonInternalCategoryData) == "{}" || !dst.InternalCategoryData.isValidType() { // empty struct
			dst.InternalCategoryData = nil
		} else {
			match++
		}
	} else {
		dst.InternalCategoryData = nil
	}

	// try to unmarshal data into IssuedCard
	err = json.Unmarshal(data, &dst.IssuedCard)
	if err == nil {
		jsonIssuedCard, _ := json.Marshal(dst.IssuedCard)
		if string(jsonIssuedCard) == "{}" || !dst.IssuedCard.isValidType() { // empty struct
			dst.IssuedCard = nil
		} else {
			match++
		}
	} else {
		dst.IssuedCard = nil
	}

	// try to unmarshal data into PlatformPayment
	err = json.Unmarshal(data, &dst.PlatformPayment)
	if err == nil {
		jsonPlatformPayment, _ := json.Marshal(dst.PlatformPayment)
		if string(jsonPlatformPayment) == "{}" || !dst.PlatformPayment.isValidType() { // empty struct
			dst.PlatformPayment = nil
		} else {
			match++
		}
	} else {
		dst.PlatformPayment = nil
	}

	if match > 1 { // more than 1 match
		// reset to nil
		dst.BankCategoryData = nil
		dst.InternalCategoryData = nil
		dst.IssuedCard = nil
		dst.PlatformPayment = nil

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

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

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

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

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

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

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

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

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

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

	// all schemas are nil
	return nil
}

type NullableTransferDataCategoryData struct {
	value *TransferDataCategoryData
	isSet bool
}

func (v NullableTransferDataCategoryData) Get() *TransferDataCategoryData {
	return v.value
}

func (v *NullableTransferDataCategoryData) Set(val *TransferDataCategoryData) {
	v.value = val
	v.isSet = true
}

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

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

func NewNullableTransferDataCategoryData(val *TransferDataCategoryData) *NullableTransferDataCategoryData {
	return &NullableTransferDataCategoryData{value: val, isSet: true}
}

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

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