Skip to content

Commit 4ab3692

Browse files
committed
Fix generics to use json type
1 parent 6b02e95 commit 4ab3692

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

src/SDK/Language/Go.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ public function getTypeName(array $parameter, array $spec = []): string
150150
self::TYPE_STRING => 'string',
151151
self::TYPE_BOOLEAN => 'bool',
152152
self::TYPE_OBJECT => 'interface{}',
153-
self::TYPE_ARRAY => '[]interface{}',
153+
self::TYPE_ARRAY => (!empty(($parameter['array'] ?? [])['type']) && !\is_array($parameter['array']['type']))
154+
? '[]' . $this->getTypeName($parameter['array'])
155+
: '[]string',
154156
default => $parameter['type'],
155157
};
156158
}
@@ -175,8 +177,10 @@ public function getParamDefault(array $param): string
175177
switch ($type) {
176178
case self::TYPE_NUMBER:
177179
case self::TYPE_INTEGER:
180+
$output .= "0";
181+
break;
178182
case self::TYPE_BOOLEAN:
179-
$output .= 'null';
183+
$output .= 'false';
180184
break;
181185
case self::TYPE_STRING:
182186
$output .= '""';
@@ -288,25 +292,38 @@ public function getFilters(): array
288292
}
289293
return implode("\n" . $indent, $value);
290294
}, ['is_safe' => ['html']]),
291-
new TwigFilter('propertyType', function (array $property, array $spec, string $generic = 'interface{}') {
295+
new TwigFilter('propertyType', function (array $property, array $spec, string $generic = 'map[string]interface{}') {
292296
return $this->getPropertyType($property, $spec, $generic);
293297
}),
294-
new TwigFilter('returnType', function (array $method, array $spec, string $namespace, string $generic = 'T') {
298+
new TwigFilter('returnType', function (array $method, array $spec, string $namespace, string $generic = 'map[string]interface{}') {
295299
return $this->getReturnType($method, $spec, $namespace, $generic);
296300
}),
297301
new TwigFilter('caseEnumKey', function (string $value) {
298302
return $this->toUpperSnakeCase($value);
299-
})
303+
}),
300304
];
301305
}
302306

303-
protected function getPropertyType(array $property, array $spec, string $generic = 'interface{}'): string
307+
protected function getPropertyType(array $property, array $spec, string $generic = 'map[string]interface{}'): string
304308
{
305-
$type = $this->getTypeName($property);
309+
if (\array_key_exists('sub_schema', $property)) {
310+
$type = $this->toPascalCase($property['sub_schema']);
311+
312+
if ($this->hasGenericType($property['sub_schema'], $spec)) {
313+
$type = $generic;
314+
}
315+
316+
if ($property['type'] === 'array') {
317+
$type = '[]' . $type;
318+
}
319+
} else {
320+
$type = $this->getTypeName($property);
321+
}
322+
306323
return $type;
307324
}
308325

309-
protected function getReturnType(array $method, array $spec, string $namespace, string $generic = 'T'): string
326+
protected function getReturnType(array $method, array $spec, string $namespace, string $generic = 'map[string]interface{}'): string
310327
{
311328
if ($method['type'] === 'webAuth') {
312329
return 'bool';
@@ -327,4 +344,27 @@ protected function getReturnType(array $method, array $spec, string $namespace,
327344

328345
return 'models.' . $ret;
329346
}
347+
348+
protected function hasGenericType(?string $model, array $spec): string
349+
{
350+
if (empty($model) || $model === 'any') {
351+
return false;
352+
}
353+
354+
$model = $spec['definitions'][$model];
355+
356+
if ($model['additionalProperties']) {
357+
return true;
358+
}
359+
360+
foreach ($model['properties'] as $property) {
361+
if (!\array_key_exists('sub_schema', $property) || !$property['sub_schema']) {
362+
continue;
363+
}
364+
365+
return $this->hasGenericType($property['sub_schema'], $spec);
366+
}
367+
368+
return false;
369+
}
330370
}

0 commit comments

Comments
 (0)