Skip to content

Commit f3b4df6

Browse files
committed
wip
1 parent 9268ddc commit f3b4df6

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

templates/go/model_simple.mustache

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@
22
var _ MappedNullable = &{{classname}}{}
33

44
{{#vars}}
5-
// {{baseName}}
5+
66
/*
7-
{{{.}}}
7+
types and functions for {{baseName}}
8+
{{!.}}
89
*/
10+
11+
{{! The basic approach is as follows: To avoid checking the various datatypes, }}
12+
{{! exceptions and non-orthogonal model states we introduce go type-alias }}
13+
{{! These are compatible with their basic types (this maintains backwards)}}
14+
{{! compatibility with existing code). This allows to concentrate the decision what }}
15+
{{! actual type the openapi represents in a single place. Reusing the generated}}
16+
{{! type in all other places avoids repetion of the non-trivial decision matrix}}
17+
{{! which type is to be used.}}
18+
{{! We define three basic types:}}
19+
{{! - FooAttributeType: The type used for the model-struct definitions, i.e. the struct attribute type}}
20+
{{! - FooArgType: The type used for parameter passing, i.e. in function calls}}
21+
{{! - FooRetType: The type used for function returns.}}
22+
{{! To simplify reading and writing values even further, type-safe}}
23+
{{! helper methods for getting and setting valuesare generated as well}}
24+
{{! (these are not rendered as methods, as the structs themselves may be nil)}}
25+
{{! (a pure function avoids possible nil-pointer exceptions)}}
26+
27+
28+
{{! address primitives types, excluding enums (they are handled explicitly below) }}
929
{{^isEnum}}
1030
{{#isNumber}}
1131
// isNumber
@@ -71,6 +91,7 @@ func set{{classname}}{{getter}}AttributeType(arg *{{classname}}{{getter}}Attribu
7191
}
7292
{{/isDouble}}
7393
{{#isShort}}
94+
{{! isShort and isInteger may be true at the same time, so select one}}
7495
{{^isInteger}}
7596
// isShort
7697
type {{classname}}{{getter}}AttributeType = *int64
@@ -136,8 +157,11 @@ func set{{classname}}{{getter}}AttributeType(arg *{{classname}}{{getter}}Attribu
136157
}
137158
{{/isLong}}
138159
{{#isString}}
160+
{{! string-types are definitely used non-orthogonally, so we have to exclude some possibilities}}
139161
{{^isArray}}
162+
{{! skip arrays, they are addressed explicitly below}}
140163
{{#isNullable}}
164+
{{! nullable strings have a special type}}
141165
// isNullableString
142166
type {{classname}}{{getter}}AttributeType = *NullableString
143167
func get{{classname}}{{getter}}AttributeTypeOk(arg {{classname}}{{getter}}AttributeType) (ret {{classname}}{{getter}}RetType, ok bool) {
@@ -161,6 +185,7 @@ func set{{classname}}{{getter}}AttributeType(arg *{{classname}}{{getter}}Attribu
161185
}
162186
{{/isNullable}}
163187
{{^isNullable}}
188+
{{! non nullable strings are plain string pointers}}
164189
// isNotNullableString
165190
type {{classname}}{{getter}}AttributeType = *string
166191
func get{{classname}}{{getter}}AttributeTypeOk(arg {{classname}}{{getter}}AttributeType) (ret {{classname}}{{getter}}RetType, ok bool) {
@@ -207,6 +232,7 @@ func set{{classname}}{{getter}}AttributeType(arg *{{classname}}{{getter}}Attribu
207232
{{/isEnum}}
208233

209234
{{#isDateTime}}
235+
{{! special handling for date-time}}
210236
// isDateTime
211237
type {{classname}}{{getter}}AttributeType = *time.Time
212238
type {{classname}}{{getter}}ArgType = {{#isNullable}}*{{/isNullable}}time.Time
@@ -228,6 +254,7 @@ func set{{classname}}{{getter}}AttributeType(arg *{{classname}}{{getter}}Attribu
228254
}
229255
{{/isDateTime}}
230256
{{#isDate}}
257+
{{! special handling for date}}
231258
// isDate
232259
type {{classname}}{{getter}}AttributeType = *time.Time
233260
type {{classname}}{{getter}}ArgType = {{#isNullable}}*{{/isNullable}}time.Time
@@ -249,6 +276,7 @@ func set{{classname}}{{getter}}AttributeType(arg *{{classname}}{{getter}}Attribu
249276
}
250277
{{/isDate}}
251278
{{#isEnumRef}}
279+
{{! special handling for enums}}
252280
// isEnumRef
253281
type {{classname}}{{getter}}AttributeType = *{{^isNumeric}}{{dataType}}{{/isNumeric}}{{#isNumeric}}int64{{/isNumeric}}
254282
type {{classname}}{{getter}}ArgType = {{#isNullable}}*{{/isNullable}}{{^isNumeric}}{{dataType}}{{/isNumeric}}{{#isNumeric}}int64{{/isNumeric}}
@@ -270,6 +298,7 @@ func set{{classname}}{{getter}}AttributeType(arg *{{classname}}{{getter}}Attribu
270298
}
271299
{{/isEnumRef}}
272300
{{#isModel}}
301+
{{! special handling for recursive datatypes}}
273302
// isModel
274303
type {{classname}}{{getter}}AttributeType = *{{dataType}}
275304
type {{classname}}{{getter}}ArgType = {{#isNullable}}*{{/isNullable}}{{dataType}}
@@ -297,6 +326,7 @@ func set{{classname}}{{getter}}AttributeType(arg *{{classname}}{{getter}}Attribu
297326
{{^isDateTime}}
298327
{{^isEnumRef}}
299328
// fallback
329+
{{! everything else will end up here}}
300330
type {{classname}}{{getter}}AttributeType = *{{dataType}}
301331
type {{classname}}{{getter}}ArgType = {{#isNullable}}*{{/isNullable}}{{dataType}}
302332
type {{classname}}{{getter}}RetType = {{#isNullable}}*{{/isNullable}}{{dataType}}

0 commit comments

Comments
 (0)