All Downloads are FREE. Search and download functionalities are using the official Maven repository.

go.model_anyof.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{#discriminator}}
// checks if the {{classname}} type satisfies the MappedNullable interface at compile time
var _ MappedNullable = &{{classname}}{}
{{/discriminator}}

// {{classname}} {{{description}}}{{^description}}struct for {{{classname}}}{{/description}}
type {{classname}} struct {
	{{#anyOf}}
	{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} *{{{.}}}
	{{/anyOf}}
}

// Unmarshal JSON data into any of the pointers in the struct
func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
	var err error
	{{#isNullable}}
	// this object is nullable so check if the payload is null or empty string
	if string(data) == "" || string(data) == "{}" {
		return nil
	}

	{{/isNullable}}
	{{#discriminator}}
	{{#mappedModels}}
	{{#-first}}
	// use discriminator value to speed up the lookup
	var jsonDict map[string]interface{}
	err = json.Unmarshal(data, &jsonDict)
	if err != nil {
		return fmt.Errorf("failed to unmarshal JSON into map for the discriminator lookup")
	}

	{{/-first}}
	// check if the discriminator value is '{{{mappingName}}}'
	if jsonDict["{{{propertyBaseName}}}"] == "{{{mappingName}}}" {
		// try to unmarshal JSON data into {{{modelName}}}
		err = json.Unmarshal(data, &dst.{{{modelName}}});
		if err == nil {
			json{{{modelName}}}, _ := json.Marshal(dst.{{{modelName}}})
			if string(json{{{modelName}}}) == "{}" { // empty struct
				dst.{{{modelName}}} = nil
			} else {
				return nil // data stored in dst.{{{modelName}}}, return on the first match
			}
		} else {
			dst.{{{modelName}}} = nil
		}
	}

	{{/mappedModels}}
	{{/discriminator}}
	{{#anyOf}}
	// try to unmarshal JSON data into {{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}
	err = json.Unmarshal(data, &dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}});
	if err == nil {
		json{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}, _ := json.Marshal(dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
		if string(json{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}) == "{}" { // empty struct
			dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
		} else {
			return nil // data stored in dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}, return on the first match
		}
	} else {
		dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
	}

	{{/anyOf}}
	return fmt.Errorf("data failed to match schemas in anyOf({{classname}})")
}

// Marshal data from the first non-nil pointers in the struct to JSON
func (src *{{classname}}) MarshalJSON() ([]byte, error) {
{{#anyOf}}
	if src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} != nil {
		return json.Marshal(&src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
	}

{{/anyOf}}
	return nil, nil // no data in anyOf schemas
}

{{#discriminator}}
func (src {{classname}}) ToMap() (map[string]interface{}, error) {
{{#anyOf}}
	if src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} != nil {
		return src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}.ToMap()
	}

{{/anyOf}}
    return nil, nil // no data in anyOf schemas
}
{{/discriminator}}

{{>nullable_model}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy