Skip to content

Commit c3fd502

Browse files
committed
Add enum case key
1 parent 8263763 commit c3fd502

File tree

38 files changed

+281
-158
lines changed

38 files changed

+281
-158
lines changed

src/SDK/Language.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function getFilters(): array
8484
return [];
8585
}
8686

87-
protected function toUpperCaseWords(string $value): string
87+
protected function toPascalCase(string $value): string
8888
{
8989
return ucfirst($this->toCamelCase($value));
9090
}
@@ -97,4 +97,17 @@ protected function toCamelCase($str): string
9797
$str = str_replace(" ", "", $str);
9898
return lcfirst($str);
9999
}
100+
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);
107+
}
108+
109+
protected function toUpperSnakeCase($str): string
110+
{
111+
return \strtoupper($this->toSnakeCase($str));
112+
}
100113
}

src/SDK/Language/Dart.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,9 @@ public function getFilters(): array
501501
}
502502
return implode("\n", $value);
503503
}, ['is_safe' => ['html']]),
504+
new TwigFilter('caseEnumKey', function (string $value) {
505+
return $this->toCamelCase($value);
506+
}),
504507
];
505508
}
506509
}

src/SDK/Language/Deno.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Appwrite\SDK\Language;
44

5+
use Twig\TwigFilter;
6+
57
class Deno extends JS
68
{
79
/**
@@ -179,4 +181,13 @@ public function getParamExample(array $param): string
179181

180182
return $output;
181183
}
184+
185+
public function getFilters(): array
186+
{
187+
return [
188+
new TwigFilter('caseEnumKey', function (string $value) {
189+
return $this->toPascalCase($value);
190+
}),
191+
];
192+
}
182193
}

src/SDK/Language/DotNet.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,10 @@ public function getFilters(): array
415415
$value[$key] = " /// " . wordwrap($line, 75, "\n /// ");
416416
}
417417
return implode("\n", $value);
418-
}, ['is_safe' => ['html']])
418+
}, ['is_safe' => ['html']]),
419+
new TwigFilter('caseEnumKey', function (string $value) {
420+
return $this->toPascalCase($value);
421+
}),
419422
];
420423
}
421424
}

src/SDK/Language/Go.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ public function getFilters(): array
245245
$value[$key] = "// " . wordwrap($line, 75, "\n// ");
246246
}
247247
return implode("\n", $value);
248-
}, ['is_safe' => ['html']])
248+
}, ['is_safe' => ['html']]),
249+
new TwigFilter('caseEnumKey', function (string $value) {
250+
return $this->toUpperSnakeCase($value);
251+
}),
249252
];
250253
}
251254
}

src/SDK/Language/JS.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Appwrite\SDK\Language;
44

55
use Appwrite\SDK\Language;
6+
use Twig\TwigFilter;
67

78
abstract class JS extends Language
89
{
@@ -199,4 +200,13 @@ public function getParamDefault(array $param): string
199200

200201
return $output;
201202
}
203+
204+
public function getFilters(): array
205+
{
206+
return [
207+
new TwigFilter('caseEnumKey', function (string $value) {
208+
return $this->toUpperSnakeCase($value);
209+
}),
210+
];
211+
}
202212
}

src/SDK/Language/Kotlin.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,9 @@ public function getFilters(): array
439439
new TwigFilter('hasGenericType', function (string $model, array $spec) {
440440
return $this->hasGenericType($model, $spec);
441441
}),
442+
new TwigFilter('caseEnumKey', function (string $value) {
443+
return $this->toUpperSnakeCase($value);
444+
}),
442445
];
443446
}
444447

@@ -459,7 +462,7 @@ protected function getReturnType(array $method, array $spec, string $namespace,
459462
return 'Any';
460463
}
461464

462-
$ret = $this->toUpperCaseWords($method['responseModel']);
465+
$ret = $this->toPascalCase($method['responseModel']);
463466

464467
if ($this->hasGenericType($method['responseModel'], $spec)) {
465468
$ret .= '<' . $generic . '>';
@@ -471,15 +474,15 @@ protected function getReturnType(array $method, array $spec, string $namespace,
471474
protected function getModelType(array $definition, array $spec, string $generic = 'T'): string
472475
{
473476
if ($this->hasGenericType($definition['name'], $spec)) {
474-
return $this->toUpperCaseWords($definition['name']) . '<' . $generic . '>';
477+
return $this->toPascalCase($definition['name']) . '<' . $generic . '>';
475478
}
476-
return $this->toUpperCaseWords($definition['name']);
479+
return $this->toPascalCase($definition['name']);
477480
}
478481

479482
protected function getPropertyType(array $property, array $spec, string $generic = 'T'): string
480483
{
481484
if (\array_key_exists('sub_schema', $property)) {
482-
$type = $this->toUpperCaseWords($property['sub_schema']);
485+
$type = $this->toPascalCase($property['sub_schema']);
483486

484487
if ($this->hasGenericType($property['sub_schema'], $spec)) {
485488
$type .= '<' . $generic . '>';

src/SDK/Language/PHP.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ public function getFilters(): array
389389
new TwigFilter('deviceInfo', function ($value) {
390390
return php_uname('s') . '; ' . php_uname('v') . '; ' . php_uname('m');
391391
}),
392+
new TwigFilter('caseEnumKey', function (string $value) {
393+
$this->toUpperSnakeCase($value);
394+
}),
392395
];
393396
}
394397
}

src/SDK/Language/Python.php

Lines changed: 84 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Appwrite\SDK\Language;
66
use Exception;
7+
use Twig\TwigFilter;
78

89
class Python extends Language
910
{
@@ -90,119 +91,119 @@ public function getFiles(): array
9091
{
9192
return [
9293
[
93-
'scope' => 'default',
94-
'destination' => 'README.md',
95-
'template' => 'python/README.md.twig',
94+
'scope' => 'default',
95+
'destination' => 'README.md',
96+
'template' => 'python/README.md.twig',
9697
],
9798
[
98-
'scope' => 'default',
99-
'destination' => 'CHANGELOG.md',
100-
'template' => 'python/CHANGELOG.md.twig',
99+
'scope' => 'default',
100+
'destination' => 'CHANGELOG.md',
101+
'template' => 'python/CHANGELOG.md.twig',
101102
],
102103
[
103-
'scope' => 'default',
104-
'destination' => 'LICENSE',
105-
'template' => 'python/LICENSE.twig',
104+
'scope' => 'default',
105+
'destination' => 'LICENSE',
106+
'template' => 'python/LICENSE.twig',
106107
],
107108
[
108-
'scope' => 'default',
109-
'destination' => 'setup.py',
110-
'template' => 'python/setup.py.twig',
109+
'scope' => 'default',
110+
'destination' => 'setup.py',
111+
'template' => 'python/setup.py.twig',
111112
],
112113
[
113-
'scope' => 'default',
114-
'destination' => 'setup.cfg',
115-
'template' => 'python/setup.cfg.twig',
114+
'scope' => 'default',
115+
'destination' => 'setup.cfg',
116+
'template' => 'python/setup.cfg.twig',
116117
],
117118
[
118-
'scope' => 'default',
119-
'destination' => 'requirements.txt',
120-
'template' => 'python/requirements.txt.twig',
119+
'scope' => 'default',
120+
'destination' => 'requirements.txt',
121+
'template' => 'python/requirements.txt.twig',
121122
],
122123
[
123-
'scope' => 'default',
124-
'destination' => '{{ spec.title | caseSnake}}/__init__.py',
125-
'template' => 'python/package/__init__.py.twig',
124+
'scope' => 'default',
125+
'destination' => '{{ spec.title | caseSnake}}/__init__.py',
126+
'template' => 'python/package/__init__.py.twig',
126127
],
127128
[
128-
'scope' => 'default',
129-
'destination' => '{{ spec.title | caseSnake}}/client.py',
130-
'template' => 'python/package/client.py.twig',
129+
'scope' => 'default',
130+
'destination' => '{{ spec.title | caseSnake}}/client.py',
131+
'template' => 'python/package/client.py.twig',
131132
],
132133
[
133-
'scope' => 'default',
134-
'destination' => '{{ spec.title | caseSnake}}/permission.py',
135-
'template' => 'python/package/permission.py.twig',
134+
'scope' => 'default',
135+
'destination' => '{{ spec.title | caseSnake}}/permission.py',
136+
'template' => 'python/package/permission.py.twig',
136137
],
137138
[
138-
'scope' => 'default',
139-
'destination' => '{{ spec.title | caseSnake}}/role.py',
140-
'template' => 'python/package/role.py.twig',
139+
'scope' => 'default',
140+
'destination' => '{{ spec.title | caseSnake}}/role.py',
141+
'template' => 'python/package/role.py.twig',
141142
],
142143
[
143-
'scope' => 'default',
144-
'destination' => '{{ spec.title | caseSnake}}/id.py',
145-
'template' => 'python/package/id.py.twig',
144+
'scope' => 'default',
145+
'destination' => '{{ spec.title | caseSnake}}/id.py',
146+
'template' => 'python/package/id.py.twig',
146147
],
147148
[
148-
'scope' => 'default',
149-
'destination' => '{{ spec.title | caseSnake}}/query.py',
150-
'template' => 'python/package/query.py.twig',
149+
'scope' => 'default',
150+
'destination' => '{{ spec.title | caseSnake}}/query.py',
151+
'template' => 'python/package/query.py.twig',
151152
],
152153
[
153-
'scope' => 'default',
154-
'destination' => '{{ spec.title | caseSnake}}/exception.py',
155-
'template' => 'python/package/exception.py.twig',
154+
'scope' => 'default',
155+
'destination' => '{{ spec.title | caseSnake}}/exception.py',
156+
'template' => 'python/package/exception.py.twig',
156157
],
157158
[
158-
'scope' => 'default',
159-
'destination' => '{{ spec.title | caseSnake}}/input_file.py',
160-
'template' => 'python/package/input_file.py.twig',
159+
'scope' => 'default',
160+
'destination' => '{{ spec.title | caseSnake}}/input_file.py',
161+
'template' => 'python/package/input_file.py.twig',
161162
],
162163
[
163-
'scope' => 'default',
164-
'destination' => '{{ spec.title | caseSnake}}/service.py',
165-
'template' => 'python/package/service.py.twig',
164+
'scope' => 'default',
165+
'destination' => '{{ spec.title | caseSnake}}/service.py',
166+
'template' => 'python/package/service.py.twig',
166167
],
167168
[
168-
'scope' => 'default',
169-
'destination' => '{{ spec.title | caseSnake}}/services/__init__.py',
170-
'template' => 'python/package/services/__init__.py.twig',
169+
'scope' => 'default',
170+
'destination' => '{{ spec.title | caseSnake}}/services/__init__.py',
171+
'template' => 'python/package/services/__init__.py.twig',
171172
],
172173
[
173-
'scope' => 'default',
174-
'destination' => '{{ spec.title | caseSnake}}/encoders/__init__.py',
175-
'template' => 'python/package/services/__init__.py.twig',
174+
'scope' => 'default',
175+
'destination' => '{{ spec.title | caseSnake}}/encoders/__init__.py',
176+
'template' => 'python/package/services/__init__.py.twig',
176177
],
177178
[
178-
'scope' => 'default',
179-
'destination' => '{{ spec.title | caseSnake}}/enums/__init__.py',
180-
'template' => 'python/package/services/__init__.py.twig',
179+
'scope' => 'default',
180+
'destination' => '{{ spec.title | caseSnake}}/enums/__init__.py',
181+
'template' => 'python/package/services/__init__.py.twig',
181182
],
182183
[
183-
'scope' => 'default',
184-
'destination' => '{{ spec.title | caseSnake}}/encoders/value_class_encoder.py',
185-
'template' => 'python/package/encoders/value_class_encoder.py.twig',
184+
'scope' => 'default',
185+
'destination' => '{{ spec.title | caseSnake}}/encoders/value_class_encoder.py',
186+
'template' => 'python/package/encoders/value_class_encoder.py.twig',
186187
],
187188
[
188-
'scope' => 'service',
189-
'destination' => '{{ spec.title | caseSnake}}/services/{{service.name | caseSnake}}.py',
190-
'template' => 'python/package/services/service.py.twig',
189+
'scope' => 'service',
190+
'destination' => '{{ spec.title | caseSnake}}/services/{{service.name | caseSnake}}.py',
191+
'template' => 'python/package/services/service.py.twig',
191192
],
192193
[
193-
'scope' => 'method',
194-
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
195-
'template' => 'python/docs/example.md.twig',
194+
'scope' => 'method',
195+
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
196+
'template' => 'python/docs/example.md.twig',
196197
],
197198
[
198-
'scope' => 'default',
199-
'destination' => '.travis.yml',
200-
'template' => 'python/.travis.yml.twig',
199+
'scope' => 'default',
200+
'destination' => '.travis.yml',
201+
'template' => 'python/.travis.yml.twig',
201202
],
202203
[
203-
'scope' => 'enum',
204-
'destination' => '{{ spec.title | caseSnake}}/enums/{{ enum.name | caseSnake }}.py',
205-
'template' => 'python/package/enums/enum.py.twig',
204+
'scope' => 'enum',
205+
'destination' => '{{ spec.title | caseSnake}}/enums/{{ enum.name | caseSnake }}.py',
206+
'template' => 'python/package/enums/enum.py.twig',
206207
],
207208
];
208209
}
@@ -238,9 +239,9 @@ public function getTypeName(array $parameter, array $spec = []): string
238239
*/
239240
public function getParamDefault(array $param): string
240241
{
241-
$type = $param['type'] ?? '';
242-
$default = $param['default'] ?? '';
243-
$required = $param['required'] ?? '';
242+
$type = $param['type'] ?? '';
243+
$default = $param['default'] ?? '';
244+
$required = $param['required'] ?? '';
244245

245246
if ($required) {
246247
return '';
@@ -295,8 +296,8 @@ public function getParamDefault(array $param): string
295296
*/
296297
public function getParamExample(array $param): string
297298
{
298-
$type = $param['type'] ?? '';
299-
$example = $param['example'] ?? '';
299+
$type = $param['type'] ?? '';
300+
$example = $param['example'] ?? '';
300301

301302
$output = '';
302303

@@ -342,4 +343,13 @@ public function getParamExample(array $param): string
342343

343344
return $output;
344345
}
346+
347+
public function getFilters(): array
348+
{
349+
return [
350+
new TwigFilter('caseEnumKey', function (string $value) {
351+
return $this->toUpperSnakeCase($value);
352+
}),
353+
];
354+
}
345355
}

0 commit comments

Comments
 (0)