// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT.

package test

import "encoding/json"
import "fmt"
import "github.com/go-viper/mapstructure/v2"
import yaml "gopkg.in/yaml.v3"
import "reflect"
import "strings"

type StructWithConstraints struct {
	// Prop corresponds to the JSON schema field "prop".
	Prop *float64 `json:"prop,omitempty,omitzero" yaml:"prop,omitempty" mapstructure:"prop,omitempty"`

	AdditionalProperties interface{} `mapstructure:",remain"`
}

// UnmarshalJSON implements json.Unmarshaler.
func (j *StructWithConstraints) UnmarshalJSON(value []byte) error {
	var raw map[string]interface{}
	if err := json.Unmarshal(value, &raw); err != nil {
		return err
	}
	type Plain StructWithConstraints
	var plain Plain
	if err := json.Unmarshal(value, &plain); err != nil {
		return err
	}
	if plain.Prop != nil && 0 > *plain.Prop {
		return fmt.Errorf("field %s: must be >= %v", "prop", 0)
	}
	st := reflect.TypeOf(Plain{})
	for i := range st.NumField() {
		delete(raw, st.Field(i).Name)
		delete(raw, strings.Split(st.Field(i).Tag.Get("json"), ",")[0])
	}
	if err := mapstructure.Decode(raw, &plain.AdditionalProperties); err != nil {
		return err
	}
	*j = StructWithConstraints(plain)
	return nil
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (j *StructWithConstraints) UnmarshalYAML(value *yaml.Node) error {
	var raw map[string]interface{}
	if err := value.Decode(&raw); err != nil {
		return err
	}
	type Plain StructWithConstraints
	var plain Plain
	if err := value.Decode(&plain); err != nil {
		return err
	}
	if plain.Prop != nil && 0 > *plain.Prop {
		return fmt.Errorf("field %s: must be >= %v", "prop", 0)
	}
	st := reflect.TypeOf(Plain{})
	for i := range st.NumField() {
		delete(raw, st.Field(i).Name)
		delete(raw, strings.Split(st.Field(i).Tag.Get("json"), ",")[0])
	}
	if err := mapstructure.Decode(raw, &plain.AdditionalProperties); err != nil {
		return err
	}
	*j = StructWithConstraints(plain)
	return nil
}
