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

package test

import "encoding/json"
import "fmt"
import yaml "gopkg.in/yaml.v3"
import "time"

type Duration struct {
	// MyObject corresponds to the JSON schema field "myObject".
	MyObject *DurationMyObject `json:"myObject,omitempty,omitzero" yaml:"myObject,omitempty" mapstructure:"myObject,omitempty"`
}

type DurationMyObject struct {
	// WithDefault corresponds to the JSON schema field "withDefault".
	WithDefault time.Duration `json:"withDefault,omitempty,omitzero" yaml:"withDefault,omitempty" mapstructure:"withDefault,omitempty"`

	// WithoutDefault corresponds to the JSON schema field "withoutDefault".
	WithoutDefault *time.Duration `json:"withoutDefault,omitempty,omitzero" yaml:"withoutDefault,omitempty" mapstructure:"withoutDefault,omitempty"`
}

// UnmarshalJSON implements json.Unmarshaler.
func (j *DurationMyObject) UnmarshalJSON(value []byte) error {
	var raw map[string]interface{}
	if err := json.Unmarshal(value, &raw); err != nil {
		return err
	}
	type Plain DurationMyObject
	var plain Plain
	if err := json.Unmarshal(value, &plain); err != nil {
		return err
	}
	if v, ok := raw["withDefault"]; !ok || v == nil {
		defaultDuration, err := time.ParseDuration("20s")
		if err != nil {
			return fmt.Errorf("failed to parse the \"20s\" default value for field withDefault: %w", err)
		}
		plain.WithDefault = defaultDuration

	}
	*j = DurationMyObject(plain)
	return nil
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (j *DurationMyObject) UnmarshalYAML(value *yaml.Node) error {
	var raw map[string]interface{}
	if err := value.Decode(&raw); err != nil {
		return err
	}
	type Plain DurationMyObject
	var plain Plain
	if err := value.Decode(&plain); err != nil {
		return err
	}
	if v, ok := raw["withDefault"]; !ok || v == nil {
		defaultDuration, err := time.ParseDuration("20s")
		if err != nil {
			return fmt.Errorf("failed to parse the \"20s\" default value for field withDefault: %w", err)
		}
		plain.WithDefault = defaultDuration

	}
	*j = DurationMyObject(plain)
	return nil
}
