@@ -85,19 +85,49 @@ public function getFiles(): array
85
85
],
86
86
[
87
87
'scope ' => 'default ' ,
88
- 'destination ' => '{{ spec.title | caseLower}} /client.go ' ,
88
+ 'destination ' => 'client /client.go ' ,
89
89
'template ' => 'go/client.go.twig ' ,
90
90
],
91
+ [
92
+ 'scope ' => 'default ' ,
93
+ 'destination ' => 'file/inputFile.go ' ,
94
+ 'template ' => 'go/inputFile.go.twig ' ,
95
+ ],
96
+ [
97
+ 'scope ' => 'default ' ,
98
+ 'destination ' => 'query/query.go ' ,
99
+ 'template ' => 'go/query.go.twig ' ,
100
+ ],
101
+ [
102
+ 'scope ' => 'default ' ,
103
+ 'destination ' => 'permission/permission.go ' ,
104
+ 'template ' => 'go/permission.go.twig ' ,
105
+ ],
106
+ [
107
+ 'scope ' => 'default ' ,
108
+ 'destination ' => 'role/role.go ' ,
109
+ 'template ' => 'go/role.go.twig ' ,
110
+ ],
111
+ [
112
+ 'scope ' => 'default ' ,
113
+ 'destination ' => 'id/id.go ' ,
114
+ 'template ' => 'go/id.go.twig ' ,
115
+ ],
91
116
[
92
117
'scope ' => 'service ' ,
93
- 'destination ' => '{{ spec.title | caseLower}}/{{service.name | caseDash}}.go ' ,
118
+ 'destination ' => '{{ service.name | caseLower}}/{{service.name | caseDash}}.go ' ,
94
119
'template ' => 'go/services/service.go.twig ' ,
95
120
],
96
121
[
97
122
'scope ' => 'method ' ,
98
123
'destination ' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md ' ,
99
124
'template ' => 'go/docs/example.md.twig ' ,
100
125
],
126
+ [
127
+ 'scope ' => 'definition ' ,
128
+ 'destination ' => 'models/{{ definition.name | caseLower }}.go ' ,
129
+ 'template ' => 'go/models/model.go.twig ' ,
130
+ ],
101
131
];
102
132
}
103
133
@@ -111,7 +141,8 @@ public function getTypeName(array $parameter, array $spec = []): string
111
141
return match ($ parameter ['type ' ]) {
112
142
self ::TYPE_INTEGER => 'int ' ,
113
143
self ::TYPE_NUMBER => 'float64 ' ,
114
- self ::TYPE_FILE , self ::TYPE_STRING => 'string ' ,
144
+ self ::TYPE_FILE => 'file.InputFile ' ,
145
+ self ::TYPE_STRING => 'string ' ,
115
146
self ::TYPE_BOOLEAN => 'bool ' ,
116
147
self ::TYPE_OBJECT => 'interface{} ' ,
117
148
self ::TYPE_ARRAY => '[]interface{} ' ,
@@ -189,31 +220,41 @@ public function getParamExample(array $param): string
189
220
switch ($ type ) {
190
221
case self ::TYPE_NUMBER :
191
222
case self ::TYPE_INTEGER :
223
+ $ output .= '0 ' ;
224
+ break ;
192
225
case self ::TYPE_BOOLEAN :
193
- $ output .= 'null ' ;
226
+ $ output .= 'false ' ;
194
227
break ;
195
228
case self ::TYPE_STRING :
196
229
$ output .= '"" ' ;
197
230
break ;
198
231
case self ::TYPE_OBJECT :
199
- $ output .= 'nil ' ;
232
+ $ output .= 'map[string]interface{}{} ' ;
200
233
break ;
201
234
case self ::TYPE_ARRAY :
202
- $ output .= '[] ' ;
235
+ $ output .= '[]interface{}{} ' ;
203
236
break ;
204
237
case self ::TYPE_FILE :
205
- $ output .= " file " ;
238
+ $ output .= ' file.NewInputFile("/path/to/ file.png", "file.png") ' ;
206
239
break ;
207
240
}
208
241
} else {
209
242
switch ($ type ) {
210
243
case self ::TYPE_NUMBER :
211
244
case self ::TYPE_INTEGER :
212
- case self ::TYPE_ARRAY :
213
245
$ output .= $ example ;
214
246
break ;
247
+ case self ::TYPE_ARRAY :
248
+ if (\str_starts_with ($ example , '[ ' )) {
249
+ $ example = \substr ($ example , 1 );
250
+ }
251
+ if (\str_ends_with ($ example , '] ' )) {
252
+ $ example = \substr ($ example , 0 , -1 );
253
+ }
254
+ $ output .= 'interface{}{ ' . $ example . '} ' ;
255
+ break ;
215
256
case self ::TYPE_OBJECT :
216
- $ output .= 'nil ' ;
257
+ $ output .= 'map[string]interface{}{} ' ;
217
258
break ;
218
259
case self ::TYPE_BOOLEAN :
219
260
$ output .= ($ example ) ? 'true ' : 'false ' ;
@@ -222,7 +263,7 @@ public function getParamExample(array $param): string
222
263
$ output .= "\"{$ example }\"" ;
223
264
break ;
224
265
case self ::TYPE_FILE :
225
- $ output .= " file " ;
266
+ $ output .= ' file.NewInputFile("/path/to/ file.png", "file.png") ' ;
226
267
break ;
227
268
}
228
269
}
@@ -233,16 +274,52 @@ public function getParamExample(array $param): string
233
274
public function getFilters (): array
234
275
{
235
276
return [
236
- new TwigFilter ('godocComment ' , function ($ value ) {
277
+ new TwigFilter ('godocComment ' , function ($ value , $ indent = 0 ) {
278
+ $ value = trim ($ value );
237
279
$ value = explode ("\n" , $ value );
280
+ $ indent = \str_repeat (' ' , $ indent );
238
281
foreach ($ value as $ key => $ line ) {
239
- $ value [$ key ] = "// " . wordwrap ($ line , 75 , "\n// " );
282
+ $ value [$ key ] = "// " . wordwrap (trim ( $ line) , 75 , "\n" . $ indent . " // " );
240
283
}
241
- return implode ("\n" , $ value );
284
+ return implode ("\n" . $ indent , $ value );
242
285
}, ['is_safe ' => ['html ' ]]),
286
+ new TwigFilter ('propertyType ' , function (array $ property , array $ spec , string $ generic = 'interface{} ' ) {
287
+ return $ this ->getPropertyType ($ property , $ spec , $ generic );
288
+ }),
289
+ new TwigFilter ('returnType ' , function (array $ method , array $ spec , string $ namespace , string $ generic = 'T ' ) {
290
+ return $ this ->getReturnType ($ method , $ spec , $ namespace , $ generic );
291
+ }),
243
292
new TwigFilter ('caseEnumKey ' , function (string $ value ) {
244
293
return $ this ->toUpperSnakeCase ($ value );
245
- }),
294
+ })
246
295
];
247
296
}
297
+
298
+ protected function getPropertyType (array $ property , array $ spec , string $ generic = 'interface{} ' ): string
299
+ {
300
+ $ type = $ this ->getTypeName ($ property );
301
+ return $ type ;
302
+ }
303
+
304
+ protected function getReturnType (array $ method , array $ spec , string $ namespace , string $ generic = 'T ' ): string
305
+ {
306
+ if ($ method ['type ' ] === 'webAuth ' ) {
307
+ return 'bool ' ;
308
+ }
309
+ if ($ method ['type ' ] === 'location ' ) {
310
+ return '[]byte ' ;
311
+ }
312
+
313
+ if (
314
+ !\array_key_exists ('responseModel ' , $ method )
315
+ || empty ($ method ['responseModel ' ])
316
+ || $ method ['responseModel ' ] === 'any '
317
+ ) {
318
+ return 'interface{} ' ;
319
+ }
320
+
321
+ $ ret = ucfirst ($ method ['responseModel ' ]);
322
+
323
+ return 'models. ' . $ ret ;
324
+ }
248
325
}
0 commit comments