Skip to content

Commit d2ff5eb

Browse files
authored
Merge pull request #700 from appwrite/feat-whitelist-enums
Feat whitelist enums
2 parents 8e71ca4 + 751c423 commit d2ff5eb

File tree

130 files changed

+2356
-799
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+2356
-799
lines changed

example.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ function getSSLPage($url) {
3737
}
3838

3939
// Leave the platform you want uncommented
40-
$platform = 'client';
41-
// $platform = 'console';
40+
// $platform = 'client';
41+
$platform = 'console';
4242
// $platform = 'server';
4343

4444
$spec = getSSLPage("https://raw.githubusercontent.com/appwrite/appwrite/master/app/config/specs/swagger2-latest-{$platform}.json");

src/SDK/Language.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ abstract public function getFiles(): array;
4141
* @param array $parameter
4242
* @return string
4343
*/
44-
abstract public function getTypeName(array $parameter): string;
44+
abstract public function getTypeName(array $parameter, array $spec = []): string;
4545

4646
/**
4747
* @param array $param
@@ -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/Android.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ public function getFiles(): array
387387
'destination' => 'library/src/main/java/io/appwrite/models/{{ definition.name | caseUcfirst }}.kt',
388388
'template' => '/android/library/src/main/java/io/appwrite/models/Model.kt.twig',
389389
],
390+
[
391+
'scope' => 'enum',
392+
'destination' => 'library/src/main/java/io/appwrite/enums/{{ enum.name | caseUcfirst }}.kt',
393+
'template' => '/android/library/src/main/java/io/appwrite/enums/Enum.kt.twig',
394+
],
390395
];
391396
}
392397
}

src/SDK/Language/Dart.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,28 @@ public function getKeywords(): array
112112
*/
113113
public function getIdentifierOverrides(): array
114114
{
115-
return ['Function' => 'Func', 'default' => 'xdefault', 'required' => 'xrequired', 'async' => 'xasync'];
115+
return [
116+
'Function' => 'Func',
117+
'default' => 'xdefault',
118+
'required' => 'xrequired',
119+
'async' => 'xasync',
120+
'enum' => 'xenum',
121+
];
116122
}
117123

118124
/**
119125
* @param array $parameter
120126
* @return string
121127
*/
122-
public function getTypeName(array $parameter): string
128+
public function getTypeName(array $parameter, array $spec = []): string
123129
{
124-
switch ($parameter['type']) {
130+
if (isset($parameter['enumName'])) {
131+
return 'enums.' . \ucfirst($parameter['enumName']);
132+
}
133+
if (!empty($parameter['enumValues'])) {
134+
return 'enums.' . \ucfirst($parameter['name']);
135+
}
136+
switch ($parameter['type'] ?? '') {
125137
case self::TYPE_INTEGER:
126138
return 'int';
127139
case self::TYPE_STRING:
@@ -131,17 +143,17 @@ public function getTypeName(array $parameter): string
131143
case self::TYPE_BOOLEAN:
132144
return 'bool';
133145
case self::TYPE_ARRAY:
134-
if (!empty($parameter['array']['type'])) {
146+
if (!empty(($parameter['array'] ?? [])['type']) && !\is_array($parameter['array']['type'])) {
135147
return 'List<' . $this->getTypeName($parameter['array']) . '>';
136148
}
137149
return 'List';
138150
case self::TYPE_OBJECT:
139151
return 'Map';
140152
case self::TYPE_NUMBER:
141153
return 'double';
154+
default:
155+
return $parameter['type'];
142156
}
143-
144-
return $parameter['type'];
145157
}
146158

147159
/**
@@ -386,6 +398,11 @@ public function getFiles(): array
386398
'destination' => '/lib/models.dart',
387399
'template' => 'dart/lib/models.dart.twig',
388400
],
401+
[
402+
'scope' => 'default',
403+
'destination' => '/lib/enums.dart',
404+
'template' => 'dart/lib/enums.dart.twig',
405+
],
389406
[
390407
'scope' => 'service',
391408
'destination' => '/lib/services/{{service.name | caseDash}}.dart',
@@ -466,6 +483,11 @@ public function getFiles(): array
466483
'destination' => 'lib/src/input_file.dart',
467484
'template' => 'dart/lib/src/input_file.dart.twig',
468485
],
486+
[
487+
'scope' => 'enum',
488+
'destination' => 'lib/src/enums/{{ enum.name | caseSnake }}.dart',
489+
'template' => 'dart/lib/src/enums/enum.dart.twig',
490+
],
469491
];
470492
}
471493

@@ -479,6 +501,9 @@ public function getFilters(): array
479501
}
480502
return implode("\n", $value);
481503
}, ['is_safe' => ['html']]),
504+
new TwigFilter('caseEnumKey', function (string $value) {
505+
return $this->toCamelCase($value);
506+
}),
482507
];
483508
}
484509
}

src/SDK/Language/Deno.php

Lines changed: 33 additions & 19 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
/**
@@ -93,34 +95,37 @@ public function getFiles(): array
9395
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
9496
'template' => 'deno/docs/example.md.twig',
9597
],
98+
[
99+
'scope' => 'enum',
100+
'destination' => 'src/enums/{{ enum.name | caseDash }}.ts',
101+
'template' => 'deno/src/enums/enum.ts.twig',
102+
],
96103
];
97104
}
98105

99106
/**
100107
* @param array $parameter
101108
* @return string
102109
*/
103-
public function getTypeName(array $parameter): string
110+
public function getTypeName(array $parameter, array $spec = []): string
104111
{
105-
switch ($parameter['type']) {
106-
case self::TYPE_INTEGER:
107-
return 'number';
108-
case self::TYPE_STRING:
109-
return 'string';
110-
case self::TYPE_FILE:
111-
return 'InputFile';
112-
case self::TYPE_BOOLEAN:
113-
return 'boolean';
114-
case self::TYPE_ARRAY:
115-
if (!empty($parameter['array']['type'])) {
116-
return $this->getTypeName($parameter['array']) . '[]';
117-
}
118-
return 'string[]';
119-
case self::TYPE_OBJECT:
120-
return 'object';
112+
if (isset($parameter['enumName'])) {
113+
return \ucfirst($parameter['enumName']);
121114
}
122-
123-
return $parameter['type'];
115+
if (!empty($parameter['enumValues'])) {
116+
return \ucfirst($parameter['name']);
117+
}
118+
return match ($parameter['type']) {
119+
self::TYPE_INTEGER => 'number',
120+
self::TYPE_STRING => 'string',
121+
self::TYPE_FILE => 'InputFile',
122+
self::TYPE_BOOLEAN => 'boolean',
123+
self::TYPE_ARRAY => (!empty(($parameter['array'] ?? [])['type']) && !\is_array($parameter['array']['type']))
124+
? $this->getTypeName($parameter['array']) . '[]'
125+
: 'string[]',
126+
self::TYPE_OBJECT => 'object',
127+
default => $parameter['type']
128+
};
124129
}
125130

126131
/**
@@ -176,4 +181,13 @@ public function getParamExample(array $param): string
176181

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

src/SDK/Language/DotNet.php

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -149,29 +149,26 @@ public function getIdentifierOverrides(): array
149149
* @param array $parameter
150150
* @return string
151151
*/
152-
public function getTypeName(array $parameter): string
152+
public function getTypeName(array $parameter, array $spec = []): string
153153
{
154-
switch ($parameter['type']) {
155-
case self::TYPE_INTEGER:
156-
return 'long';
157-
case self::TYPE_NUMBER:
158-
return 'double';
159-
case self::TYPE_STRING:
160-
return 'string';
161-
case self::TYPE_FILE:
162-
return 'InputFile';
163-
case self::TYPE_BOOLEAN:
164-
return 'bool';
165-
case self::TYPE_ARRAY:
166-
if (!empty($parameter['array']['type'])) {
167-
return 'List<' . $this->getTypeName($parameter['array']) . '>';
168-
}
169-
return 'List<object>';
170-
case self::TYPE_OBJECT:
171-
return 'object';
154+
if (isset($parameter['enumName'])) {
155+
return \ucfirst($parameter['enumName']);
172156
}
173-
174-
return $parameter['type'];
157+
if (!empty($parameter['enumValues'])) {
158+
return \ucfirst($parameter['name']);
159+
}
160+
return match ($parameter['type']) {
161+
self::TYPE_INTEGER => 'long',
162+
self::TYPE_NUMBER => 'double',
163+
self::TYPE_STRING => 'string',
164+
self::TYPE_BOOLEAN => 'bool',
165+
self::TYPE_FILE => 'InputFile',
166+
self::TYPE_ARRAY => (!empty(($parameter['array'] ?? [])['type']) && !\is_array($parameter['array']['type']))
167+
? 'List<' . $this->getTypeName($parameter['array']) . '>'
168+
: 'List<object>',
169+
self::TYPE_OBJECT => 'object',
170+
default => $parameter['type']
171+
};
175172
}
176173

177174
/**
@@ -361,6 +358,11 @@ public function getFiles(): array
361358
'destination' => '/src/{{ spec.title | caseUcfirst }}/Role.cs',
362359
'template' => 'dotnet/src/Appwrite/Role.cs.twig',
363360
],
361+
[
362+
'scope' => 'default',
363+
'destination' => '/src/{{ spec.title | caseUcfirst }}/Converters/ValueClassConverter.cs',
364+
'template' => 'dotnet/src/Appwrite/Converters/ValueClassConverter.cs.twig',
365+
],
364366
[
365367
'scope' => 'default',
366368
'destination' => '/src/{{ spec.title | caseUcfirst }}/Extensions/Extensions.cs',
@@ -395,6 +397,16 @@ public function getFiles(): array
395397
'scope' => 'definition',
396398
'destination' => '/src/{{ spec.title | caseUcfirst }}/Models/{{ definition.name | caseUcfirst | overrideIdentifier }}.cs',
397399
'template' => 'dotnet/src/Appwrite/Models/Model.cs.twig',
400+
],
401+
[
402+
'scope' => 'enum',
403+
'destination' => '/src/{{ spec.title | caseUcfirst }}/Enums/{{ enum.name | caseUcfirst | overrideIdentifier }}.cs',
404+
'template' => 'dotnet/src/Appwrite/Enums/Enum.cs.twig',
405+
],
406+
[
407+
'scope' => 'default',
408+
'destination' => '/src/{{ spec.title | caseUcfirst }}/Enums/IEnum.cs',
409+
'template' => 'dotnet/src/Appwrite/Enums/IEnum.cs.twig',
398410
]
399411
];
400412
}
@@ -408,7 +420,10 @@ public function getFilters(): array
408420
$value[$key] = " /// " . wordwrap($line, 75, "\n /// ");
409421
}
410422
return implode("\n", $value);
411-
}, ['is_safe' => ['html']])
423+
}, ['is_safe' => ['html']]),
424+
new TwigFilter('caseEnumKey', function (string $value) {
425+
return $this->toPascalCase($value);
426+
}),
412427
];
413428
}
414429
}

src/SDK/Language/Flutter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public function getFiles(): array
6060
'destination' => '/lib/models.dart',
6161
'template' => 'dart/lib/models.dart.twig',
6262
],
63+
[
64+
'scope' => 'default',
65+
'destination' => '/lib/enums.dart',
66+
'template' => 'dart/lib/enums.dart.twig',
67+
],
6368
[
6469
'scope' => 'default',
6570
'destination' => '/lib/permission.dart',
@@ -330,6 +335,11 @@ public function getFiles(): array
330335
'destination' => '.travis.yml',
331336
'template' => 'flutter/.travis.yml.twig',
332337
],
338+
[
339+
'scope' => 'enum',
340+
'destination' => 'lib/src/enums/{{ enum.name | caseSnake }}.dart',
341+
'template' => 'dart/lib/src/enums/enum.dart.twig',
342+
],
333343
];
334344
}
335345
}

src/SDK/Language/Go.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,17 @@ public function getFiles(): array
106106
* @param array $nestedTypes
107107
* @return string
108108
*/
109-
public function getTypeName(array $parameter): string
109+
public function getTypeName(array $parameter, array $spec = []): string
110110
{
111-
switch ($parameter['type']) {
112-
case self::TYPE_INTEGER:
113-
return 'int';
114-
case self::TYPE_NUMBER:
115-
return 'float64';
116-
case self::TYPE_FILE:
117-
case self::TYPE_STRING:
118-
return 'string';
119-
case self::TYPE_BOOLEAN:
120-
return 'bool';
121-
case self::TYPE_OBJECT:
122-
return 'interface{}';
123-
case self::TYPE_ARRAY:
124-
return '[]interface{}';
125-
}
126-
127-
return $parameter['type'];
111+
return match ($parameter['type']) {
112+
self::TYPE_INTEGER => 'int',
113+
self::TYPE_NUMBER => 'float64',
114+
self::TYPE_FILE, self::TYPE_STRING => 'string',
115+
self::TYPE_BOOLEAN => 'bool',
116+
self::TYPE_OBJECT => 'interface{}',
117+
self::TYPE_ARRAY => '[]interface{}',
118+
default => $parameter['type'],
119+
};
128120
}
129121

130122
/**
@@ -247,7 +239,10 @@ public function getFilters(): array
247239
$value[$key] = "// " . wordwrap($line, 75, "\n// ");
248240
}
249241
return implode("\n", $value);
250-
}, ['is_safe' => ['html']])
242+
}, ['is_safe' => ['html']]),
243+
new TwigFilter('caseEnumKey', function (string $value) {
244+
return $this->toUpperSnakeCase($value);
245+
}),
251246
];
252247
}
253248
}

0 commit comments

Comments
 (0)