Skip to content

Commit d6bd6ff

Browse files
Merge pull request #580 from appwrite/feat-swift-generics
2 parents 3441fa9 + 3476249 commit d6bd6ff

File tree

16 files changed

+817
-198
lines changed

16 files changed

+817
-198
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ env:
4646
- SDK=Ruby27
4747
- SDK=Ruby30
4848
- SDK=Ruby31
49-
- SDK=SwiftClient55
50-
- SDK=SwiftServer55
49+
- SDK=AppleSwift55
50+
- SDK=Swift55
5151
- SDK=WebChromium
5252
- SDK=WebNode
5353

example.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Appwrite\SDK\Language\Deno;
1616
use Appwrite\SDK\Language\HTTP;
1717
use Appwrite\SDK\Language\Swift;
18-
use Appwrite\SDK\Language\SwiftClient;
18+
use Appwrite\SDK\Language\Apple;
1919
use Appwrite\SDK\Language\DotNet;
2020
use Appwrite\SDK\Language\Flutter;
2121
use Appwrite\SDK\Language\Android;
@@ -342,7 +342,7 @@ function getSSLPage($url) {
342342
$sdk->generate(__DIR__ . '/examples/swift-server');
343343

344344
// Swift (Client)
345-
$sdk = new SDK(new SwiftClient(), new Swagger2($spec));
345+
$sdk = new SDK(new Apple(), new Swagger2($spec));
346346

347347
$sdk
348348
->setName('NAME')
@@ -363,7 +363,7 @@ function getSSLPage($url) {
363363
])
364364
;
365365

366-
$sdk->generate(__DIR__ . '/examples/swift-client');
366+
$sdk->generate(__DIR__ . '/examples/apple');
367367

368368
// DotNet
369369
$sdk = new SDK(new DotNet(), new Swagger2($spec));

src/SDK/Language.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,18 @@ public function getFilters(): array
8383
{
8484
return [];
8585
}
86+
87+
protected function toUpperCaseWords(string $value): string
88+
{
89+
return ucfirst($this->toCamelCase($value));
90+
}
91+
92+
protected function toCamelCase($str): string
93+
{
94+
$str = preg_replace('/[^a-z0-9' . implode("", []) . ']+/i', ' ', $str);
95+
$str = trim($str);
96+
$str = ucwords($str);
97+
$str = str_replace(" ", "", $str);
98+
return lcfirst($str);
99+
}
86100
}

src/SDK/Language/SwiftClient.php renamed to src/SDK/Language/Apple.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace Appwrite\SDK\Language;
44

5-
class SwiftClient extends Swift
5+
class Apple extends Swift
66
{
77
/**
88
* @return string
99
*/
1010
public function getName(): string
1111
{
12-
return 'SwiftClient';
12+
return 'Apple';
1313
}
1414

1515
public function getFiles(): array

src/SDK/Language/Swift.php

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,99 @@ public function getFilters(): array
433433
$value[$key] = " /// " . wordwrap($line, 75, "\n /// ");
434434
}
435435
return implode("\n", $value);
436-
}, ['is_safe' => ['html']])
436+
}, ['is_safe' => ['html']]),
437+
new TwigFilter('returnType', function (array $method, array $spec, string $generic = 'T') {
438+
return $this->getReturnType($method, $spec, $generic);
439+
}),
440+
new TwigFilter('modelType', function (array $property, array $spec, string $generic = 'T : Codable') {
441+
return $this->getModelType($property, $spec, $generic);
442+
}),
443+
new TwigFilter('propertyType', function (array $property, array $spec, string $generic = 'T') {
444+
return $this->getPropertyType($property, $spec, $generic);
445+
}),
446+
new TwigFilter('hasGenericType', function (string $model, array $spec) {
447+
return $this->hasGenericType($model, $spec);
448+
}),
437449
];
438450
}
451+
452+
protected function getReturnType(array $method, array $spec, string $generic): string
453+
{
454+
if ($method['type'] === 'webAuth') {
455+
return 'Bool';
456+
}
457+
if ($method['type'] === 'location') {
458+
return 'ByteBuffer';
459+
}
460+
461+
if (
462+
!\array_key_exists('responseModel', $method)
463+
|| empty($method['responseModel'])
464+
|| $method['responseModel'] === 'any'
465+
) {
466+
return 'Any';
467+
}
468+
469+
$ret = $this->toUpperCaseWords($method['responseModel']);
470+
471+
if ($this->hasGenericType($method['responseModel'], $spec)) {
472+
$ret .= '<' . $generic . '>';
473+
}
474+
475+
return \ucfirst($spec['title']) . 'Models.' . $ret;
476+
}
477+
478+
protected function getModelType(array $definition, array $spec, string $generic): string
479+
{
480+
if ($this->hasGenericType($definition['name'], $spec)) {
481+
return $this->toUpperCaseWords($definition['name']) . '<' . $generic . '>';
482+
}
483+
return $this->toUpperCaseWords($definition['name']);
484+
}
485+
486+
protected function getPropertyType(array $property, array $spec, string $generic): string
487+
{
488+
if (\array_key_exists('sub_schema', $property)) {
489+
$type = $this->toUpperCaseWords($property['sub_schema']);
490+
491+
if ($this->hasGenericType($property['sub_schema'], $spec)) {
492+
$type .= '<' . $generic . '>';
493+
}
494+
495+
if ($property['type'] === 'array') {
496+
$type = '[' . $type . ']';
497+
}
498+
} else {
499+
$type = $this->getTypeName($property);
500+
}
501+
502+
if (!$property['required']) {
503+
$type .= '?';
504+
}
505+
506+
return $type;
507+
}
508+
509+
protected function hasGenericType(?string $model, array $spec): string
510+
{
511+
if (empty($model) || $model === 'any') {
512+
return false;
513+
}
514+
515+
$model = $spec['definitions'][$model];
516+
517+
if ($model['additionalProperties']) {
518+
return true;
519+
}
520+
521+
foreach ($model['properties'] as $property) {
522+
if (!\array_key_exists('sub_schema', $property) || !$property['sub_schema']) {
523+
continue;
524+
}
525+
526+
return $this->hasGenericType($property['sub_schema'], $spec);
527+
}
528+
529+
return false;
530+
}
439531
}

src/SDK/Language/Web.php

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public function getTypeName(array $parameter, array $method = []): string
208208
protected function populateGenerics(string $model, array $spec, array &$generics, bool $skipFirst = false)
209209
{
210210
if (!$skipFirst && $spec['definitions'][$model]['additionalProperties']) {
211-
$generics[] = $this->toUpperCase($model);
211+
$generics[] = $this->toUpperCaseWords($model);
212212
}
213213

214214
$properties = $spec['definitions'][$model]['properties'];
@@ -257,14 +257,14 @@ public function getReturn(array $method, array $spec): string
257257
$ret .= 'Models.';
258258
}
259259

260-
$ret .= $this->toUpperCase($method['responseModel']);
260+
$ret .= $this->toUpperCaseWords($method['responseModel']);
261261

262262
$models = [];
263263

264264
$this->populateGenerics($method['responseModel'], $spec, $models);
265265

266266
$models = array_unique($models);
267-
$models = array_filter($models, fn ($model) => $model != $this->toUpperCase($method['responseModel']));
267+
$models = array_filter($models, fn ($model) => $model != $this->toUpperCaseWords($method['responseModel']));
268268

269269
if (!empty($models)) {
270270
$ret .= '<' . implode(', ', $models) . '>';
@@ -277,32 +277,16 @@ public function getReturn(array $method, array $spec): string
277277
return 'Promise<{}>';
278278
}
279279

280-
public function toUpperCase(string $value): string
281-
{
282-
return ucfirst($this->helperCamelCase($value));
283-
}
284-
285-
protected function helperCamelCase($str)
286-
{
287-
$str = preg_replace('/[^a-z0-9' . implode("", []) . ']+/i', ' ', $str);
288-
$str = trim($str);
289-
$str = ucwords($str);
290-
$str = str_replace(" ", "", $str);
291-
$str = lcfirst($str);
292-
293-
return $str;
294-
}
295-
296280
public function getSubSchema(array $property, array $spec): string
297281
{
298282
if (array_key_exists('sub_schema', $property)) {
299283
$ret = '';
300284
$generics = [];
301285
$this->populateGenerics($property['sub_schema'], $spec, $generics);
302286

303-
$generics = array_filter($generics, fn ($model) => $model != $this->toUpperCase($property['sub_schema']));
287+
$generics = array_filter($generics, fn ($model) => $model != $this->toUpperCaseWords($property['sub_schema']));
304288

305-
$ret .= $this->toUpperCase($property['sub_schema']);
289+
$ret .= $this->toUpperCaseWords($property['sub_schema']);
306290
if (!empty($generics)) {
307291
$ret .= '<' . implode(', ', $generics) . '>';
308292
}

0 commit comments

Comments
 (0)