go-experimental.model.mustache Maven / Gradle / Ivy
{{>partial_header}}
package {{packageName}}
{{#models}}
import (
"bytes"
"encoding/json"
{{#imports}}
"{{import}}"
{{/imports}}
)
{{#model}}
{{#isEnum}}
// {{{classname}}} {{#description}}{{{.}}}{{/description}}{{^description}}the model '{{{classname}}}'{{/description}}
type {{{classname}}} {{^format}}{{dataType}}{{/format}}{{#format}}{{{format}}}{{/format}}
// List of {{{name}}}
const (
{{#allowableValues}}
{{#enumVars}}
{{^-first}}
{{/-first}}
{{#enumClassPrefix}}{{{classname.toUpperCase}}}_{{/enumClassPrefix}}{{name}} {{{classname}}} = {{{value}}}
{{/enumVars}}
{{/allowableValues}}
)
// Ptr returns reference to {{{name}}} value
func (v {{{classname}}}) Ptr() *{{{classname}}} {
return &v
}
{{/isEnum}}
{{^isEnum}}
// {{classname}}{{#description}} {{{description}}}{{/description}}{{^description}} struct for {{{classname}}}{{/description}}
type {{classname}} struct {
{{#vendorExtensions.x-is-one-of-interface}}
{{classname}}Interface interface { {{#discriminator}}{{propertyGetter}}() {{propertyType}}{{/discriminator}} }
{{/vendorExtensions.x-is-one-of-interface}}
{{^vendorExtensions.x-is-one-of-interface}}
{{#parent}}
{{^isMapModel}}
{{{parent}}}
{{/isMapModel}}
{{/parent}}
{{#vars}}
{{^-first}}
{{/-first}}
{{#description}}
// {{{description}}}
{{/description}}
{{name}} {{^required}}*{{/required}}{{{dataType}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
{{/vars}}
{{/vendorExtensions.x-is-one-of-interface}}
}
{{/isEnum}}
{{^isEnum}}
{{^vendorExtensions.x-is-one-of-interface}}
// New{{classname}} instantiates a new {{classname}} object
// This constructor will assign default values to properties that have it defined,
// and makes sure properties required by API are set, but the set of arguments
// will change when the set of required properties is changed
func New{{classname}}({{#vars}}{{#required}}{{nameInCamelCase}} {{dataType}}, {{/required}}{{/vars}}) *{{classname}} {
this := {{classname}}{}
{{#vars}}
{{#required}}
this.{{name}} = {{nameInCamelCase}}
{{/required}}
{{^required}}
{{#defaultValue}}
{{^isContainer}}
var {{nameInCamelCase}} {{{dataType}}} = {{#isNullable}}{{{dataType}}}{Value: {{/isNullable}}{{{.}}}{{#isNullable}} }{{/isNullable}}
this.{{name}} = {{^required}}&{{/required}}{{nameInCamelCase}}
{{/isContainer}}
{{/defaultValue}}
{{/required}}
{{/vars}}
return &this
}
// New{{classname}}WithDefaults instantiates a new {{classname}} object
// This constructor will only assign default values to properties that have it defined,
// but it doesn't guarantee that properties required by API are set
func New{{classname}}WithDefaults() *{{classname}} {
this := {{classname}}{}
{{#vars}}
{{#defaultValue}}
{{^isContainer}}
var {{nameInCamelCase}} {{{dataType}}} = {{#isNullable}}{{{dataType}}}{Value: {{/isNullable}}{{{.}}}{{#isNullable}} }{{/isNullable}}
this.{{name}} = {{^required}}&{{/required}}{{nameInCamelCase}}
{{/isContainer}}
{{/defaultValue}}
{{/vars}}
return &this
}
{{#vars}}
{{#required}}
// Get{{name}} returns the {{name}} field value
func (o *{{classname}}) Get{{name}}() {{dataType}} {
if o == nil {
var ret {{dataType}}
return ret
}
return o.{{name}}
}
// Set{{name}} sets field value
func (o *{{classname}}) Set{{name}}(v {{dataType}}) {
o.{{name}} = v
}
{{/required}}
{{^required}}
// Get{{name}} returns the {{name}} field value if set, zero value otherwise.
func (o *{{classname}}) Get{{name}}() {{dataType}} {
if o == nil || o.{{name}} == nil {
var ret {{dataType}}
return ret
}
return *o.{{name}}
}
// Get{{name}}Ok returns a tuple with the {{name}} field value if set, zero value otherwise
// and a boolean to check if the value has been set.
func (o *{{classname}}) Get{{name}}Ok() ({{dataType}}, bool) {
if o == nil || o.{{name}} == nil {
var ret {{dataType}}
return ret, false
}
return *o.{{name}}, true
}
// Has{{name}} returns a boolean if a field has been set.
func (o *{{classname}}) Has{{name}}() bool {
if o != nil && o.{{name}} != nil {
return true
}
return false
}
// Set{{name}} gets a reference to the given {{dataType}} and assigns it to the {{name}} field.
func (o *{{classname}}) Set{{name}}(v {{dataType}}) {
o.{{name}} = &v
}
{{/required}}
{{/vars}}
{{/vendorExtensions.x-is-one-of-interface}}
{{#vendorExtensions.x-is-one-of-interface}}
func (s *{{classname}}) MarshalJSON() ([]byte, error) {
return json.Marshal(s.{{classname}}Interface)
}
func (s *{{classname}}) UnmarshalJSON(src []byte) error {
var err error
{{#discriminator}}
var unmarshaled map[string]interface{}
err = json.Unmarshal(src, &unmarshaled)
if err != nil {
return err
}
if v, ok := unmarshaled["{{discriminator.propertyBaseName}}"]; ok {
switch v {
{{#discriminator.mappedModels}}
case "{{mappingName}}":
var result *{{modelName}} = &{{modelName}}{}
err = json.Unmarshal(src, result)
if err != nil {
return err
}
s.{{classname}}Interface = result
return nil
{{/discriminator.mappedModels}}
default:
return fmt.Errorf("No oneOf model has '{{discriminator.propertyBaseName}}' equal to %s", v)
}
} else {
return fmt.Errorf("Discriminator property '{{discriminator.propertyBaseName}}' not found in unmarshaled payload: %+v", unmarshaled)
}
{{/discriminator}}
{{^discriminator}}
{{#oneOf}}
var unmarshaled{{{.}}} *{{{.}}} = &{{{.}}}{}
err = json.Unmarshal(src, unmarshaled{{{.}}})
if err == nil {
s.{{classname}}Interface = unmarshaled{{{.}}}
return nil
}
{{/oneOf}}
return fmt.Errorf("No oneOf model could be deserialized from payload: %s", string(src))
{{/discriminator}}
}
{{/vendorExtensions.x-is-one-of-interface}}
{{#vendorExtensions.x-implements}}
// As{{{.}}} wraps this instance of {{classname}} in {{{.}}}
func (s *{{classname}}) As{{{.}}}() {{{.}}} {
return {{{.}}}{ {{{.}}}Interface: s }
}
{{/vendorExtensions.x-implements}}
{{/isEnum}}
type Nullable{{{classname}}} struct {
Value {{{classname}}}
ExplicitNull bool
}
func (v Nullable{{{classname}}}) MarshalJSON() ([]byte, error) {
switch {
case v.ExplicitNull:
return []byte("null"), nil
default:
return json.Marshal(v.Value)
}
}
func (v *Nullable{{{classname}}}) UnmarshalJSON(src []byte) error {
if bytes.Equal(src, []byte("null")) {
v.ExplicitNull = true
return nil
}
return json.Unmarshal(src, &v.Value)
}
{{/model}}
{{/models}}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy