Skip to content

Commit ea1ffb1

Browse files
bahkauv70Fyusel
authored andcommitted
fix: correctly handle bitsizes and nullability of numeric attributes
1 parent fb6a391 commit ea1ffb1

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

templates/go/model_simple.mustache

Lines changed: 20 additions & 22 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}}{{#isInteger}}NullableInt64{{/isInteger}}{{^isInteger}}{{dataType}}{{/isInteger}}{{/isNullable}}{{^isNullable}}{{^isPrimitiveType}}{{dataType}}{{/isPrimitiveType}}{{#isPrimitiveType}}{{#isInteger}}int64{{/isInteger}}{{^isInteger}}{{dataType}}{{/isInteger}}{{/isPrimitiveType}}{{/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}} *{{#isInteger}}int64{{/isInteger}}{{^isInteger}}{{datatype}}{{/isInteger}}{{^-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}} {{#isInteger}}int64{{/isInteger}}{{^isInteger}}{{dataType}}{{/isInteger}} = {{{.}}}
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}} {{#isInteger}}int64{{/isInteger}}{{^isInteger}}{{dataType}}{{/isInteger}} = {{{.}}}
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}}() *{{#isInteger}}int64{{/isInteger}}{{^isInteger}}{{dataType}}{{/isInteger}} {
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 *{{#isInteger}}int64{{/isInteger}}{{^isInteger}}{{dataType}}{{/isInteger}}
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() (*{{#isInteger}}int64{{/isInteger}}{{^isInteger}}{{dataType}}{{/isInteger}}, 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 &{{datatype}}{}, 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 *{{#isInteger}}int64{{/isInteger}}{{^isInteger}}{{dataType}}{{/isInteger}}) {
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}}{{#isInteger}}int64{{/isInteger}}{{^isInteger}}{{baseType}}{{/isInteger}}{{/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}}{{#isInteger}}int64{{/isInteger}}{{^isInteger}}{{baseType}}{{/isInteger}}{{/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}}{{#isInteger}}int64{{/isInteger}}{{^isInteger}}{{baseType}}{{/isInteger}}{{/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 &{{datatype}}{}, false
231229
{{/isFreeFormObject}}
232230
}
233231
{{#isNullable}}
@@ -256,14 +254,14 @@ func (o *{{classname}}) Has{{name}}() bool {
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}}{{#isInteger}}int64{{/isInteger}}{{^isInteger}}{{baseType}}{{/isInteger}}{{/isContainer}}) {
260258
{{#isNullable}}
261259
{{#vendorExtensions.x-golang-is-container}}
262260
o.{{name}} = v
263261
{{/vendorExtensions.x-golang-is-container}}
264262
{{^vendorExtensions.x-golang-is-container}}
265263
if IsNil(o.{{name}}) {
266-
o.{{name}} = new({{dataType}})
264+
o.{{name}} = new({{#isInteger}}NullableInt64{{/isInteger}}{{^isInteger}}{{dataType}}{{/isInteger}})
267265
}
268266
o.{{name}}.Set(v)
269267
{{/vendorExtensions.x-golang-is-container}}
@@ -277,15 +275,15 @@ func (o *{{classname}}) Set{{name}}(v *{{#isNumber}}float64{{/isNumber}}{{#isFlo
277275
// Set{{name}}Nil sets the value for {{name}} to be an explicit nil
278276
func (o *{{classname}}) Set{{name}}Nil() {
279277
if IsNil(o.{{name}}) {
280-
o.{{name}} = new({{dataType}})
278+
o.{{name}} = new({{#isInteger}}NullableInt64{{/isInteger}}{{^isInteger}}{{dataType}}{{/isInteger}})
281279
}
282280
o.{{name}}.Set(nil)
283281
}
284282

285283
// Unset{{name}} ensures that no value is present for {{name}}, not even an explicit nil
286284
func (o *{{classname}}) Unset{{name}}() {
287285
if IsNil(o.{{name}}) {
288-
o.{{name}} = new({{dataType}})
286+
o.{{name}} = new({{#isInteger}}NullableInt64{{/isInteger}}{{^isInteger}}{{dataType}}{{/isInteger}})
289287
}
290288
o.{{name}}.Unset()
291289
}

0 commit comments

Comments
 (0)