Skip to content

Commit ecb1ea7

Browse files
committed
fix: correctly handle bitsizes and nullability of numeric attributes
1 parent eb351ad commit ecb1ea7

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

templates/go/model_simple.mustache

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type {{classname}} struct {
3737
{{/isWriteOnly}}
3838
{{/isReadOnly}}
3939
{{/required}}
40-
{{name}} *{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{{dataType}}}{{/isNumeric}} `json:"{{baseName}}{{#required}}{{#isReadOnly}},omitempty{{/isReadOnly}}{{/required}}{{#required}}{{#isWriteOnly}},omitempty{{/isWriteOnly}}{{/required}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
40+
{{name}} *{{#isNullable}}{{dataType}}{{/isNullable}}{{^isNullable}}{{^isPrimitive}}{{dataType}}{{/isPrimitive}}{{#isPrimitive}}{{baseType}}{{/isPrimitive}}{{/isNullable}} `json:"{{baseName}}{{#required}}{{#isReadOnly}},omitempty{{/isReadOnly}}{{/required}}{{#required}}{{#isWriteOnly}},omitempty{{/isWriteOnly}}{{/required}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
4141
{{#isAdditionalPropertiesTrue}}
4242
AdditionalProperties map[string]interface{}
4343
{{/isAdditionalPropertiesTrue}}
@@ -59,7 +59,7 @@ type _{{{classname}}} {{{classname}}}
5959
// This constructor will assign default values to properties that have it defined,
6060
// and makes sure properties required by API are set, but the set of arguments
6161
// will change when the set of required properties is changed
62-
func New{{classname}}({{#requiredVars}}{{nameInCamelCase}} *{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{{dataType}}}{{/isNumeric}}{{^-last}}, {{/-last}}{{/requiredVars}}) *{{classname}} {
62+
func New{{classname}}({{#requiredVars}}{{nameInCamelCase}} *{{#isNullable}}{{dataType}}{{/isNullable}}{{^isNullable}}{{dataType}}{{/isNullable}}{{^-last}}, {{/-last}}{{/requiredVars}}) *{{classname}} {
6363
this := {{classname}}{}
6464
{{#allVars}}
6565
{{#required}}
@@ -69,12 +69,11 @@ func New{{classname}}({{#requiredVars}}{{nameInCamelCase}} *{{#isNumber}}float64
6969
{{#defaultValue}}
7070
{{^vendorExtensions.x-golang-is-container}}
7171
{{^isReadOnly}}
72+
var {{nameInCamelCase}} {{{dataType}}} = {{{.}}}
7273
{{#isNullable}}
73-
var {{nameInCamelCase}} {{{datatypeWithEnum}}} = {{{.}}}
74-
this.{{name}} = *New{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{{dataType}}}{{/isNumeric}}(&{{nameInCamelCase}})
74+
this.{{name}} = *New{{dataType}}(&{{nameInCamelCase}})
7575
{{/isNullable}}
7676
{{^isNullable}}
77-
var {{nameInCamelCase}} {{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{{dataType}}}{{/isNumeric}} = {{{.}}}
7877
this.{{name}} = &{{nameInCamelCase}}
7978
{{/isNullable}}
8079
{{/isReadOnly}}
@@ -94,13 +93,12 @@ func New{{classname}}WithDefaults() *{{classname}} {
9493
{{#defaultValue}}
9594
{{^vendorExtensions.x-golang-is-container}}
9695
{{^isReadOnly}}
97-
{{#isNullable}}
9896
{{!we use datatypeWithEnum here, since it will represent the non-nullable name of the datatype, e.g. int64 for NullableInt64}}
99-
var {{nameInCamelCase}} {{{datatypeWithEnum}}} = {{{.}}}
100-
this.{{name}} = *New{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{{dataType}}}{{/isNumeric}}(&{{nameInCamelCase}})
97+
var {{nameInCamelCase}} {{{dataType}}} = {{{.}}}
98+
{{#isNullable}}
99+
this.{{name}} = *New{{dataType}}(&{{nameInCamelCase}})
101100
{{/isNullable}}
102101
{{^isNullable}}
103-
var {{nameInCamelCase}} {{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{{dataType}}}{{/isNumeric}} = {{{.}}}
104102
this.{{name}} = &{{nameInCamelCase}}
105103
{{/isNullable}}
106104
{{/isReadOnly}}
@@ -119,9 +117,9 @@ func New{{classname}}WithDefaults() *{{classname}} {
119117
{{#deprecated}}
120118
// Deprecated
121119
{{/deprecated}}
122-
func (o *{{classname}}) Get{{name}}() *{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{vendorExtensions.x-go-base-type}}{{/isNumeric}} {
120+
func (o *{{classname}}) Get{{name}}() *{{#isNullable}}{{dataType}}{{/isNullable}}{{^isNullable}}{{dataType}}{{/isNullable}} {
123121
if o == nil|| IsNil(o.{{name}}) {{#isNullable}}{{^vendorExtensions.x-golang-is-container}} || o.{{name}}.Get() == nil{{/vendorExtensions.x-golang-is-container}}{{/isNullable}} {
124-
var ret *{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{vendorExtensions.x-go-base-type}}{{/isNumeric}}
122+
var ret *{{#isNullable}}{{dataType}}{{/isNullable}}{{^isNullable}}{{dataType}}{{/isNullable}}
125123
return ret
126124
}
127125

@@ -146,13 +144,13 @@ func (o *{{classname}}) Get{{name}}() *{{#isNumber}}float64{{/isNumber}}{{#isFlo
146144
{{#deprecated}}
147145
// Deprecated
148146
{{/deprecated}}
149-
func (o *{{classname}}) Get{{name}}Ok() (*{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{vendorExtensions.x-go-base-type}}{{/isNumeric}}, bool) {
147+
func (o *{{classname}}) Get{{name}}Ok() (*{{#isNullable}}{{dataType}}{{/isNullable}}{{^isNullable}}{{dataType}}{{/isNullable}}, bool) {
150148
if o == nil{{#isNullable}}{{#vendorExtensions.x-golang-is-container}} || IsNil(o.{{name}}){{/vendorExtensions.x-golang-is-container}}{{/isNullable}} {
151149
{{^isFreeFormObject}}
152150
return nil, false
153151
{{/isFreeFormObject}}
154152
{{#isFreeFormObject}}
155-
return &{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{vendorExtensions.x-go-base-type}}{{/isNumeric}}{}, false
153+
return &{{#isNullable}}{{dataType}}{{/isNullable}}{{^isNullable}}{{dataType}}{{/isNullable}}{}, false
156154
{{/isFreeFormObject}}
157155
}
158156
{{#isNullable}}
@@ -172,7 +170,7 @@ func (o *{{classname}}) Get{{name}}Ok() (*{{#isNumber}}float64{{/isNumber}}{{#is
172170
{{#deprecated}}
173171
// Deprecated
174172
{{/deprecated}}
175-
func (o *{{classname}}) Set{{name}}(v *{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{vendorExtensions.x-go-base-type}}{{/isNumeric}}) {
173+
func (o *{{classname}}) Set{{name}}(v *{{#isNullable}}{{dataType}}{{/isNullable}}{{^isNullable}}{{dataType}}{{/isNullable}}) {
176174
{{#isNullable}}
177175
{{#vendorExtensions.x-golang-is-container}}
178176
o.{{name}} = v
@@ -195,9 +193,9 @@ func (o *{{classname}}) Set{{name}}(v *{{#isNumber}}float64{{/isNumber}}{{#isFlo
195193
{{#deprecated}}
196194
// Deprecated
197195
{{/deprecated}}
198-
func (o *{{classname}}) Get{{name}}() *{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{vendorExtensions.x-go-base-type}}{{/isNumeric}} {
196+
func (o *{{classname}}) Get{{name}}() *{{#isContainer}}{{dataType}}{{/isContainer}}{{^isContainer}}{{baseType}}{{/isContainer}} {
199197
if o == nil || IsNil(o.{{name}}) {{#isNullable}}{{^vendorExtensions.x-golang-is-container}} || IsNil(o.{{name}}.Get()){{/vendorExtensions.x-golang-is-container}}{{/isNullable}} {
200-
var ret *{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{vendorExtensions.x-go-base-type}}{{/isNumeric}}
198+
var ret *{{#isContainer}}{{dataType}}{{/isContainer}}{{^isContainer}}{{baseType}}{{/isContainer}}
201199
return ret
202200
}
203201
{{#isNullable}}
@@ -221,13 +219,13 @@ func (o *{{classname}}) Get{{name}}() *{{#isNumber}}float64{{/isNumber}}{{#isFlo
221219
{{#deprecated}}
222220
// Deprecated
223221
{{/deprecated}}
224-
func (o *{{classname}}) Get{{name}}Ok() (*{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{vendorExtensions.x-go-base-type}}{{/isNumeric}}, bool) {
222+
func (o *{{classname}}) Get{{name}}Ok() (*{{#isContainer}}{{dataType}}{{/isContainer}}{{^isContainer}}{{baseType}}{{/isContainer}}, bool) {
225223
if o == nil || IsNil(o.{{name}}) {
226224
{{^isFreeFormObject}}
227225
return nil, false
228226
{{/isFreeFormObject}}
229227
{{#isFreeFormObject}}
230-
return &{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{vendorExtensions.x-go-base-type}}{{/isNumeric}}{}, false
228+
return &{{#isNullable}}{{dataType}}{{/isNullable}}{{^isNullable}}{{dataType}}{{/isNullable}}{}, false
231229
{{/isFreeFormObject}}
232230
}
233231
{{#isNullable}}
@@ -252,11 +250,11 @@ func (o *{{classname}}) Has{{name}}() bool {
252250
return false
253251
}
254252

255-
// Set{{name}} gets a reference to the given {{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{vendorExtensions.x-go-base-type}}{{/isNumeric}} and assigns it to the {{name}} field.
253+
// Set{{name}} gets a reference to the given {{dataType}} and assigns it to the {{name}} field.
256254
{{#deprecated}}
257255
// Deprecated
258256
{{/deprecated}}
259-
func (o *{{classname}}) Set{{name}}(v *{{#isNumber}}float64{{/isNumber}}{{#isFloat}}float64{{/isFloat}}{{#isDouble}}float64{{/isDouble}}{{#isInteger}}int64{{/isInteger}}{{#isLong}}int64{{/isLong}}{{^isNumeric}}{{vendorExtensions.x-go-base-type}}{{/isNumeric}}) {
257+
func (o *{{classname}}) Set{{name}}(v *{{#isContainer}}{{dataType}}{{/isContainer}}{{^isContainer}}{{baseType}}{{/isContainer}}) {
260258
{{#isNullable}}
261259
{{#vendorExtensions.x-golang-is-container}}
262260
o.{{name}} = v

0 commit comments

Comments
 (0)