/*
 * In-Memory DB API
 *
 * API description for the IONOS In-Memory DB
 *
 * API version: 1.0.0
 * Contact: support@cloud.ionos.com
 */

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

package ionoscloud

import (
	"encoding/json"
	"fmt"
)

// UserPassword - struct for UserPassword
type UserPassword struct {
	HashedPassword *HashedPassword
	PlainTextPassword         *string
}

// HashedPasswordAsUserPassword is a convenience function that returns HashedPassword wrapped in UserPassword
func HashedPasswordAsUserPassword(v *HashedPassword) UserPassword {
	return UserPassword{HashedPassword: v}
}

// PlainTextPasswordAsUserPassword is a convenience function that returns PlainTextPassword wrapped in UserPassword
func PlainTextPasswordAsUserPassword(v *string) UserPassword {
	return UserPassword{PlainTextPassword: v}
}

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

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

	if match > 1 { // more than 1 match
		// reset to nil
		dst.HashedPassword = nil
		dst.PlainTextPassword = nil

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

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

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

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

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

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

	// all schemas are nil
	return nil
}

type NullableUserPassword struct {
	value *UserPassword
	isSet bool
}

func (v NullableUserPassword) Get() *UserPassword {
	return v.value
}

func (v *NullableUserPassword) Set(val *UserPassword) {
	v.value = val
	v.isSet = true
}

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

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

func NewNullableUserPassword(val *UserPassword) *NullableUserPassword {
	return &NullableUserPassword{value: val, isSet: true}
}

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

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