@@ -85,19 +85,49 @@ public function getFiles(): array
8585 ],
8686 [
8787 'scope ' => 'default ' ,
88- 'destination ' => '{{ spec.title | caseLower}} /client.go ' ,
88+ 'destination ' => 'client /client.go ' ,
8989 'template ' => 'go/client.go.twig ' ,
9090 ],
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+ ],
91116 [
92117 'scope ' => 'service ' ,
93- 'destination ' => '{{ spec.title | caseLower}}/{{service.name | caseDash}}.go ' ,
118+ 'destination ' => '{{ service.name | caseLower}}/{{service.name | caseDash}}.go ' ,
94119 'template ' => 'go/services/service.go.twig ' ,
95120 ],
96121 [
97122 'scope ' => 'method ' ,
98123 'destination ' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md ' ,
99124 'template ' => 'go/docs/example.md.twig ' ,
100125 ],
126+ [
127+ 'scope ' => 'definition ' ,
128+ 'destination ' => 'models/{{ definition.name | caseLower }}.go ' ,
129+ 'template ' => 'go/models/model.go.twig ' ,
130+ ],
101131 ];
102132 }
103133
@@ -111,7 +141,8 @@ public function getTypeName(array $parameter, array $spec = []): string
111141 return match ($ parameter ['type ' ]) {
112142 self ::TYPE_INTEGER => 'int ' ,
113143 self ::TYPE_NUMBER => 'float64 ' ,
114- self ::TYPE_FILE , self ::TYPE_STRING => 'string ' ,
144+ self ::TYPE_FILE => 'file.InputFile ' ,
145+ self ::TYPE_STRING => 'string ' ,
115146 self ::TYPE_BOOLEAN => 'bool ' ,
116147 self ::TYPE_OBJECT => 'interface{} ' ,
117148 self ::TYPE_ARRAY => '[]interface{} ' ,
@@ -189,31 +220,41 @@ public function getParamExample(array $param): string
189220 switch ($ type ) {
190221 case self ::TYPE_NUMBER :
191222 case self ::TYPE_INTEGER :
223+ $ output .= '0 ' ;
224+ break ;
192225 case self ::TYPE_BOOLEAN :
193- $ output .= 'null ' ;
226+ $ output .= 'false ' ;
194227 break ;
195228 case self ::TYPE_STRING :
196229 $ output .= '"" ' ;
197230 break ;
198231 case self ::TYPE_OBJECT :
199- $ output .= 'nil ' ;
232+ $ output .= 'map[string]interface{}{} ' ;
200233 break ;
201234 case self ::TYPE_ARRAY :
202- $ output .= '[] ' ;
235+ $ output .= '[]interface{}{} ' ;
203236 break ;
204237 case self ::TYPE_FILE :
205- $ output .= " file " ;
238+ $ output .= ' file.NewInputFile("/path/to/ file.png", "file.png") ' ;
206239 break ;
207240 }
208241 } else {
209242 switch ($ type ) {
210243 case self ::TYPE_NUMBER :
211244 case self ::TYPE_INTEGER :
212- case self ::TYPE_ARRAY :
213245 $ output .= $ example ;
214246 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 ;
215256 case self ::TYPE_OBJECT :
216- $ output .= 'nil ' ;
257+ $ output .= 'map[string]interface{}{} ' ;
217258 break ;
218259 case self ::TYPE_BOOLEAN :
219260 $ output .= ($ example ) ? 'true ' : 'false ' ;
@@ -222,7 +263,7 @@ public function getParamExample(array $param): string
222263 $ output .= "\"{$ example }\"" ;
223264 break ;
224265 case self ::TYPE_FILE :
225- $ output .= " file " ;
266+ $ output .= ' file.NewInputFile("/path/to/ file.png", "file.png") ' ;
226267 break ;
227268 }
228269 }
@@ -233,16 +274,52 @@ public function getParamExample(array $param): string
233274 public function getFilters (): array
234275 {
235276 return [
236- new TwigFilter ('godocComment ' , function ($ value ) {
277+ new TwigFilter ('godocComment ' , function ($ value , $ indent = 0 ) {
278+ $ value = trim ($ value );
237279 $ value = explode ("\n" , $ value );
280+ $ indent = \str_repeat (' ' , $ indent );
238281 foreach ($ value as $ key => $ line ) {
239- $ value [$ key ] = "// " . wordwrap ($ line , 75 , "\n// " );
282+ $ value [$ key ] = "// " . wordwrap (trim ( $ line) , 75 , "\n" . $ indent . " // " );
240283 }
241- return implode ("\n" , $ value );
284+ return implode ("\n" . $ indent , $ value );
242285 }, ['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+ }),
243292 new TwigFilter ('caseEnumKey ' , function (string $ value ) {
244293 return $ this ->toUpperSnakeCase ($ value );
245- }),
294+ })
246295 ];
247296 }
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+ }
248325}
0 commit comments