Skip to content

Commit 3476249

Browse files
committed
Merge remote-tracking branch 'origin/master' into feat-swift-generics
# Conflicts: # templates/swift/base/params.twig
2 parents 8ed594e + 2332ad0 commit 3476249

File tree

30 files changed

+168
-68
lines changed

30 files changed

+168
-68
lines changed

src/SDK/Language/HTTP.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,23 @@ public function getParamExample(array $param): string
154154
case self::TYPE_FILE:
155155
case self::TYPE_NUMBER:
156156
case self::TYPE_INTEGER:
157-
case self::TYPE_STRING:
158-
case self::TYPE_ARRAY:
159157
$output .= $example;
160158
break;
159+
case self::TYPE_ARRAY:
160+
// If array of strings, make sure any sub-strings are escaped
161+
if (\substr($example, 1, 1) === '"') {
162+
$start = \substr($example, 0, 2);
163+
$end = \substr($example, -2);
164+
$contents = \substr($example, 2, -2);
165+
$contents = \addslashes($contents);
166+
$output .= $start . $contents . $end;
167+
} else {
168+
$output .= $example;
169+
}
170+
break;
171+
case self::TYPE_STRING:
172+
$output .= '"' . \addslashes($example) . '"';
173+
break;
161174
case self::TYPE_BOOLEAN:
162175
$output .= ($example) ? 'true' : 'false';
163176
break;
@@ -175,7 +188,7 @@ public function getFiles(): array
175188
return [
176189
[
177190
'scope' => 'method',
178-
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}',
191+
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
179192
'template' => '/http/docs/example.md.twig',
180193
],
181194
];

src/SDK/SDK.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ public function __construct(Language $language, Spec $spec)
103103
$this->twig->addFilter(new TwigFilter('caseUcfirst', function ($value) {
104104
return ucfirst($this->helperCamelCase($value));
105105
}));
106+
$this->twig->addFilter(new TwigFilter('caseUcwords', function ($value) {
107+
return ucwords($value, " -_");
108+
}));
106109
$this->twig->addFilter(new TwigFilter('caseLcfirst', function ($value) {
107110
return lcfirst((string)$value);
108111
}));
@@ -588,13 +591,21 @@ public function generate(string $target): void
588591
'methods' => $methods,
589592
];
590593

594+
if ($this->exclude($file, $params)) {
595+
continue;
596+
}
597+
591598
$this->render($template, $destination, $block, $params, $minify);
592599
}
593600
break;
594601
case 'definition':
595602
foreach ($this->spec->getDefinitions() as $key => $definition) {
596603
$params['definition'] = $definition;
597604

605+
if ($this->exclude($file, $params)) {
606+
continue;
607+
}
608+
598609
$this->render($template, $destination, $block, $params, $minify);
599610
}
600611
break;
@@ -614,6 +625,11 @@ public function generate(string $target): void
614625

615626
foreach ($methods as $method) {
616627
$params['method'] = $method;
628+
629+
if ($this->exclude($file, $params)) {
630+
continue;
631+
}
632+
617633
$this->render($template, $destination, $block, $params, $minify);
618634
}
619635
}
@@ -622,6 +638,76 @@ public function generate(string $target): void
622638
}
623639
}
624640

641+
/**
642+
* Determine if a file should be excluded from generation.
643+
*
644+
* Allows for files to be excluded based on:
645+
* - Service name or feature
646+
* - Method name or type
647+
* - Definition name
648+
*
649+
* @param $file
650+
* @param $params
651+
* @return bool
652+
*/
653+
protected function exclude($file, $params): bool
654+
{
655+
$exclude = $file['exclude'] ?? [];
656+
657+
$services = [];
658+
$features = [];
659+
foreach ($exclude['services'] ?? [] as $service) {
660+
if (isset($service['name'])) {
661+
$services[] = $service['name'];
662+
}
663+
if (isset($service['feature'])) {
664+
$features[] = $service['feature'];
665+
}
666+
}
667+
668+
$methods = [];
669+
$types = [];
670+
foreach ($exclude['methods'] ?? [] as $method) {
671+
if (isset($method['name'])) {
672+
$methods[] = $method['name'];
673+
}
674+
if (isset($method['type'])) {
675+
$types[] = $method['type'];
676+
}
677+
}
678+
679+
$definitions = [];
680+
foreach ($exclude['definitions'] ?? [] as $definition) {
681+
if (isset($definition['name'])) {
682+
$definitions[] = $definition['name'];
683+
}
684+
}
685+
686+
if (\in_array($params['service']['name'] ?? '', $services)) {
687+
return true;
688+
}
689+
690+
foreach ($features as $feature) {
691+
if ($params['service']['features'][$feature] ?? false) {
692+
return true;
693+
}
694+
}
695+
696+
if (\in_array($params['method']['name'] ?? '', $methods)) {
697+
return true;
698+
}
699+
700+
if (\in_array($params['method']['type'] ?? '', $types)) {
701+
return true;
702+
}
703+
704+
if (\in_array($params['definition']['name'] ?? '', $definitions)) {
705+
return true;
706+
}
707+
708+
return false;
709+
}
710+
625711
/**
626712
* @param array $methods
627713
* @return bool

src/Spec/Swagger2.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,15 @@ public function getMethods($service)
137137

138138
foreach ($paths as $pathName => $path) {
139139
foreach ($path as $methodName => $method) {
140-
$auth = $method['x-appwrite']['auth'] ?? [];
140+
$methodAuth = $method['x-appwrite']['auth'] ?? [];
141+
$methodSecurity = $method['security'][0] ?? [];
141142

142143
if (isset($method['tags']) && is_array($method['tags']) && in_array($service, $method['tags'])) {
143-
if (isset($auth) && is_array($auth)) {
144-
foreach ($auth as $i => $node) {
145-
$auth[$i] = (array_key_exists($i, $security)) ? $security[$i] : [];
146-
}
144+
foreach ($methodAuth as $i => $node) {
145+
$methodAuth[$i] = (array_key_exists($i, $security)) ? $security[$i] : [];
146+
}
147+
foreach ($methodSecurity as $i => $node) {
148+
$methodSecurity[$i] = (array_key_exists($i, $security)) ? $security[$i] : [];
147149
}
148150

149151
$responses = $method['responses'];
@@ -169,7 +171,8 @@ public function getMethods($service)
169171
'packaging' => $method['x-appwrite']['packaging'] ?? false,
170172
'title' => $method['summary'] ?? '',
171173
'description' => $method['description'] ?? '',
172-
'security' => [$auth] ?? [],
174+
'auth' => [$methodAuth] ?? [],
175+
'security' => [$methodSecurity] ?? [],
173176
'consumes' => $method['consumes'] ?? [],
174177
'cookies' => $method['x-appwrite']['cookies'] ?? false,
175178
'type' => $method['x-appwrite']['type'] ?? false,

templates/android/docs/java/example.md.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public class MainActivity extends AppCompatActivity {
1616
setContentView(R.layout.activity_main);
1717

1818
Client client = new Client(getApplicationContext())
19-
{% if method.security|length > 0 %}
19+
{% if method.auth|length > 0 %}
2020
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
21-
{% for node in method.security %}
21+
{% for node in method.auth %}
2222
{% for key,header in node|keys %}
2323
.set{{header | caseUcfirst}}("{{node[header]['x-appwrite']['demo']}}"){% if loop.last %};{% endif %} // {{node[header].description}}
2424
{% endfor %}

templates/android/docs/kotlin/example.md.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class MainActivity : AppCompatActivity() {
1414
setContentView(R.layout.activity_main)
1515

1616
val client = Client(applicationContext)
17-
{% if method.security|length > 0 %}
17+
{% if method.auth|length > 0 %}
1818
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
19-
{% for node in method.security %}
19+
{% for node in method.auth %}
2020
{% for key,header in node|keys %}
2121
.set{{header | caseUcfirst}}("{{node[header]['x-appwrite']['demo']}}") // {{node[header].description}}
2222
{% endfor %}

templates/android/library/src/main/java/io/appwrite/services/ServiceTemplate.kt.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{% if parameters.all|length > 0 %}{% for parameter in parameters.all %}{{ '\n\t\t' }}{{ _self.parameter(parameter) }}{% if not loop.last %}{{ ',' }}{% endif %}{% endfor %}{% if 'multipart/form-data' in consumes %}, onProgress: ((UploadProgress) -> Unit)? = null{% endif %}{% endif %}
44
{% endmacro %}
55
{% macro methodNeedsSecurityParameters(method) %}
6-
{% if (method.type == "webAuth" or method.type == "location") and method.security|length > 0 %}{{ true }}{% else %}{{false}}{% endif %}
6+
{% if (method.type == "webAuth" or method.type == "location") and method.auth|length > 0 %}{{ true }}{% else %}{{false}}{% endif %}
77
{% endmacro %}
88
{% macro resultType(namespace, method) %}
99
{% if method.type == "webAuth" %}Bool{% elseif method.type == "location" %}ByteArray{% elseif not method.responseModel or method.responseModel == 'any' %}Any{% else %}{{ namespace | caseDot}}.models.{{method.responseModel | caseUcfirst}}{% endif %}
@@ -55,7 +55,7 @@ class {{ service.name | caseUcfirst }} : Service {
5555

5656
{% endfor %}
5757
{% if _self.methodNeedsSecurityParameters(method) %}
58-
{% for node in method.security %}
58+
{% for node in method.auth %}
5959
{% for key,header in node|keys %}
6060
"{{header|caseLower}}" to client.config["{{header|caseLower}}"]{% if not loop.last %},{% endif %}
6161

templates/cli/Formula/formula.rb.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class {{ spec.title| caseUcfirst }} < Formula
44
desc "CLI is a Node based command-line tool for {{ spec.title| caseUcfirst }} API"
55
homepage "{{ sdk.url }}"
66
license "{{ sdk.license }}"
7-
head "https://github.com/{{ sdk.gitUserName }}/{{ sdk.gitRepoName | caseDash }}.git", branch: "main"
7+
head "https://github.com/{{ sdk.gitUserName }}/{{ sdk.gitRepoName | caseDash }}.git", branch: "master"
88

99
depends_on "node"
1010

templates/dart/base/requests/location.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
final Map<String, dynamic> params = {
33
{{ utils.map_parameter(method.parameters.query) }}
44
{{ utils.map_parameter(method.parameters.body) }}
5-
{% if method.security|length > 0 %}{% for node in method.security %}
5+
{% if method.auth|length > 0 %}{% for node in method.auth %}
66
{% for key,header in node|keys %}
77
'{{header|caseLower}}': client.config['{{header|caseLower}}'],
88
{% endfor %}

templates/dart/base/requests/oauth.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
final Map<String, dynamic> params = {
33
{{ utils.map_parameter(method.parameters.query) }}
44
{{ utils.map_parameter(method.parameters.body) }}
5-
{% if method.security|length > 0 %}
6-
{% for node in method.security %}
5+
{% if method.auth|length > 0 %}
6+
{% for node in method.auth %}
77
{% for key,header in node|keys %}
88
'{{header|caseLower}}': client.config['{{header|caseLower}}'],
99
{% endfor %}

templates/dart/docs/example.md.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ void main() { // Init SDK
77
Client client = Client();
88
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = {{service.name | caseUcfirst}}(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter.name | caseCamel | overrideIdentifier }}: {{ parameter | paramExample }}{% endfor %}{% endif %});
99

10-
{% if method.security|length > 0 %}
10+
{% if method.auth|length > 0 %}
1111
client
1212
.setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
13-
{% for node in method.security %}
13+
{% for node in method.auth %}
1414
{% for key,header in node|keys %}
1515
.set{{header}}('{{node[header]['x-appwrite']['demo']}}') // {{node[header].description}}
1616
{% endfor %}

0 commit comments

Comments
 (0)