Skip to content

Commit 1413a93

Browse files
committed
fix: js enums
1 parent a7c1422 commit 1413a93

File tree

22 files changed

+77
-79
lines changed

22 files changed

+77
-79
lines changed

src/SDK/Language.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,21 @@ protected function toCamelCase($str): string
9898
return lcfirst($str);
9999
}
100100

101-
protected function toSnakeCase($str): string
102-
{
103-
$str = \preg_replace('/([a-z])([A-Z])/', '$1 $2', $str);
104-
$str = \explode(' ', $str);
105-
$str = \implode('_', $str);
106-
return \strtolower($str);
101+
function toSnakeCase($string) {
102+
// Replace alternative character sets
103+
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
104+
105+
// Replace seperating characters with underscores
106+
// Includes: spaces, dashes, apostrophes, periods and slashes
107+
$string = preg_replace('/[ \'.\/-]/', '_', $string);
108+
109+
// Seperate camelCase with underscores
110+
$string = preg_replace_callback('/([a-z])([^a-z_])/', function($matches) {
111+
return $matches[1] . '_' . strtolower($matches[2]);
112+
}, $string);
113+
114+
// Remove ignorable characters
115+
return preg_replace('/[^a-z0-9_]/', '', strtolower($string));
107116
}
108117

109118
protected function toUpperSnakeCase($str): string

src/SDK/Language/Deno.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,10 @@ public function getFiles(): array
134134
*/
135135
public function getTypeName(array $parameter, array $spec = []): string
136136
{
137-
if (isset($parameter['enumName'])) {
138-
return \ucfirst($parameter['enumName']);
139-
}
140137
if (!empty($parameter['enumValues'])) {
141-
return \ucfirst($parameter['name']);
138+
return \implode(' | ', \array_map(function ($value) {
139+
return "\"$value\"";
140+
}, $parameter['enumValues']));
142141
}
143142
return match ($parameter['type']) {
144143
self::TYPE_INTEGER => 'number',

src/SDK/Language/JS.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,10 @@ public function getIdentifierOverrides(): array
125125
*/
126126
public function getTypeName(array $parameter, array $spec = []): string
127127
{
128-
if (isset($parameter['enumName'])) {
129-
return \ucfirst($parameter['enumName']);
130-
}
131128
if (!empty($parameter['enumValues'])) {
132-
return \ucfirst($parameter['name']);
129+
return \implode(' | ', \array_map(function ($value) {
130+
return "\"$value\"";
131+
}, $parameter['enumValues']));
133132
}
134133
switch ($parameter['type']) {
135134
case self::TYPE_INTEGER:
@@ -204,7 +203,7 @@ public function getFilters(): array
204203
{
205204
return [
206205
new TwigFilter('caseEnumKey', function (string $value) {
207-
return $this->toPascalCase($value);
206+
return $this->toUpperSnakeCase($value);
208207
}),
209208
];
210209
}

src/SDK/Language/Node.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ public function getName(): string
1919
*/
2020
public function getTypeName(array $parameter, array $spec = []): string
2121
{
22-
if (isset($parameter['enumName'])) {
23-
return \ucfirst($parameter['enumName']);
24-
}
2522
if (!empty($parameter['enumValues'])) {
26-
return \ucfirst($parameter['name']);
23+
return \implode(' | ', \array_map(function ($value) {
24+
return "\"$value\"";
25+
}, $parameter['enumValues']));
2726
}
2827
return match ($parameter['type']) {
2928
self::TYPE_INTEGER,

src/SDK/Language/Web.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,10 @@ public function getParamExample(array $param): string
179179

180180
public function getTypeName(array $parameter, array $method = []): string
181181
{
182-
if (isset($parameter['enumName'])) {
183-
return \ucfirst($parameter['enumName']);
184-
}
185182
if (!empty($parameter['enumValues'])) {
186-
return \ucfirst($parameter['name']);
183+
return \implode(' | ', \array_map(function ($value) {
184+
return "\"$value\"";
185+
}, $parameter['enumValues']));
187186
}
188187
switch ($parameter['type']) {
189188
case self::TYPE_INTEGER:
@@ -341,7 +340,7 @@ public function getFilters(): array
341340
return implode("\n", $value);
342341
}, ['is_safe' => ['html']]),
343342
new TwigFilter('caseEnumKey', function ($value) {
344-
return $this->toPascalCase($value);
343+
return $this->toUpperSnakeCase($value);
345344
}),
346345
];
347346
}

src/SDK/SDK.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Twig\TwigFilter;
1616
use MatthiasMullie\Minify;
1717
use Twig_Error_Loader;
18-
use Twig_Error_Runtime;
1918
use Twig_Error_Syntax;
2019

2120
class SDK
@@ -545,7 +544,7 @@ public function generate(string $target): void
545544
'contactURL' => $this->spec->getContactURL(),
546545
'contactEmail' => $this->spec->getContactEmail(),
547546
'services' => $this->spec->getServices(),
548-
'enums' => $this->spec->getEnumNames(),
547+
'enums' => $this->spec->getEnums(),
549548
'definitions' => $this->spec->getDefinitions(),
550549
'global' => [
551550
'headers' => $this->spec->getGlobalHeaders(),
@@ -636,24 +635,10 @@ public function generate(string $target): void
636635
}
637636
break;
638637
case 'enum':
639-
foreach ($this->spec->getServices() as $key => $service) {
640-
$methods = $this->spec->getMethods($key);
638+
foreach ($this->spec->getEnums() as $key => $enum) {
639+
$params['enum'] = $enum;
641640

642-
foreach ($methods as $method) {
643-
$parameters = $method['parameters']['all'];
644-
645-
foreach ($parameters as $parameter) {
646-
// Check if the enum field is defined
647-
if (isset($parameter['enumValues'])) {
648-
$params['enum'] = [
649-
'name' => $parameter['enumName'] ?? $parameter['name'],
650-
'enum' => $parameter['enumValues'],
651-
'keys' => $parameter['enumKeys'],
652-
];
653-
$this->render($template, $destination, $block, $params, $minify);
654-
}
655-
}
656-
}
641+
$this->render($template, $destination, $block, $params, $minify);
657642
}
658643
break;
659644
}

src/Spec/Spec.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ public function setAttribute($key, $value, $type = self::SET_TYPE_ASSIGN)
166166
}
167167

168168
/**
169-
* Get EnumNames
169+
* Get Enums
170170
*
171171
* @return array
172172
*/
173-
abstract public function getEnumNames();
173+
abstract public function getEnums();
174174
}

src/Spec/Swagger2.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,21 +357,28 @@ public function getDefinitions()
357357
/**
358358
* @return array
359359
*/
360-
public function getEnumNames(): array
360+
public function getEnums(): array
361361
{
362362
$list = [];
363363

364364
foreach ($this->getServices() as $key => $service) {
365365
foreach ($this->getMethods($key) as $method) {
366366
if (isset($method['parameters']) && is_array($method['parameters'])) {
367367
foreach ($method['parameters']['all'] as $parameter) {
368-
if (isset($parameter['enumValues'])) {
369-
$list[] = $parameter['enumName'] ?? $parameter['name'];
368+
$enumName = $parameter['enumName'] ?? $parameter['name'];
369+
370+
if (isset($parameter['enumValues']) && !\in_array($enumName, $list)) {
371+
$list[$enumName] = [
372+
'name' => $enumName,
373+
'enum' => $parameter['enumValues'],
374+
'keys' => $parameter['enumKeys'],
375+
];
370376
}
371377
}
372378
}
373379
}
374380
}
375-
return \array_values(\array_unique($list));
381+
382+
return \array_values($list);
376383
}
377384
}

templates/dart/lib/enums.dart.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
library {{ language.params.packageName }}.enums;
33

44
{% for enum in spec.enums %}
5-
part 'src/enums/{{enum | caseSnake}}.dart';
5+
part 'src/enums/{{enum.name | caseSnake}}.dart';
66
{% endfor %}

templates/deno/mod.ts.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { {{spec.title | caseUcfirst}}Exception } from "./src/exception.ts";
99
import { {{service.name | caseUcfirst}} } from "./src/services/{{service.name | caseDash}}.ts";
1010
{% endfor %}
1111
{% for enum in spec.enums %}
12-
import { {{enum | caseUcfirst}} } from "./src/enums/{{enum | caseDash}}.ts";
12+
import { {{enum.name | caseUcfirst}} } from "./src/enums/{{enum.name | caseDash}}.ts";
1313
{% endfor %}
1414

1515
export {
@@ -24,7 +24,7 @@ export {
2424
{{service.name | caseUcfirst}},
2525
{% endfor %}
2626
{% for enum in spec.enums %}
27-
{{enum | caseUcfirst}},
27+
{{enum.name | caseUcfirst}},
2828
{% endfor %}
2929
};
3030

0 commit comments

Comments
 (0)