Skip to content

Commit 5555adf

Browse files
committed
Merge branch 'master' of github.com:Maatteogekko/sdk-generator
2 parents 1aeb369 + 11e9f5c commit 5555adf

File tree

7 files changed

+108
-20
lines changed

7 files changed

+108
-20
lines changed

src/SDK/Language/Dart.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,24 @@ public function getFiles()
363363
'template' => 'dart/lib/src/enums.dart.twig',
364364
'minify' => false,
365365
],
366+
[
367+
'scope' => 'default',
368+
'destination' => '/lib/models.dart',
369+
'template' => 'dart/lib/models.dart.twig',
370+
'minify' => false,
371+
],
366372
[
367373
'scope' => 'service',
368374
'destination' => '/lib/services/{{service.name | caseDash}}.dart',
369375
'template' => 'dart/lib/services/service.dart.twig',
370376
'minify' => false,
371377
],
378+
[
379+
'scope' => 'definition',
380+
'destination' => '/lib/src/models/{{definition.name | caseSnake }}.dart',
381+
'template' => 'dart/lib/src/models/model.dart.twig',
382+
'minify' => false,
383+
],
372384
[
373385
'scope' => 'method',
374386
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',

src/SDK/SDK.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ public function __construct(Language $language, Spec $spec)
197197

198198
return $value;
199199
}, ['is_safe' => ['html']]));
200+
$this->twig->addFilter(new TwigFilter('ucFirstAndEscape', function ($value) use ($language) {
201+
$value = ucfirst((string)$this->helperCamelCase($value));
202+
if(in_array($value, $language->getKeywords())) {
203+
$value = 'x' . $value;
204+
}
205+
206+
return ucfirst((string)$this->helperCamelCase($value));
207+
}, ['is_safe' => ['html']]));
200208
$this->twig->addFilter(new TwigFilter('caseHTML', function ($value) {
201209
return $value;
202210
}, ['is_safe' => ['html']]));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library {{ language.params.packageName }}.models;
2+
3+
{% for definition in spec.definitions %}
4+
part 'src/models/{{definition.name | caseSnake}}.dart';
5+
{% endfor %}

templates/dart/lib/package.dart.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
library {{ language.params.packageName }};
22

33
import 'dart:async';
4+
import 'dart:typed_data';
45
import 'package:http/http.dart' as http;
56
import 'src/enums.dart';
67
import 'src/client.dart';
7-
import 'src/response.dart';
88
import 'src/service.dart';
9+
import 'models.dart' as models;
910

1011
export 'src/response.dart';
1112
export 'src/client.dart';

templates/dart/lib/services/service.dart.twig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class {{ service.name | caseUcfirst }} extends Service {
1616
{{ method.description|dartComment }}
1717
///
1818
{% endif %}
19-
Future<Response> {{ method.name | caseCamel }}({{ _self.method_parameters(method.parameters) }}) {
19+
{% if method.type == 'webAuth' %}Future{% elseif method.type == 'location' %} Future<Uint8List> {% else %} {% if method.responseModel and method.responseModel != 'any' %}Future<models.{{method.responseModel | ucFirstAndEscape}}>{% else %}Future{% endif %}{% endif %} {{ method.name | caseCamel }}({{ _self.method_parameters(method.parameters) }}) async {
2020
final String path = '{{ method.path }}'{% for parameter in method.parameters.path %}.replaceAll(RegExp('{{ '{' }}{{ parameter.name | caseCamel }}{{ '}' }}'), {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %};
2121

2222
final Map<String, dynamic> params = {
@@ -42,15 +42,17 @@ class {{ service.name | caseUcfirst }} extends Service {
4242
params[key] = params[key].toString();
4343
}});
4444

45-
return client.call(HttpMethod.{{ method.method | caseLower }}, path: path, params: params, responseType: ResponseType.bytes);
45+
final res = await client.call(HttpMethod.{{ method.method | caseLower }}, path: path, params: params, responseType: ResponseType.bytes);
46+
return res.data;
4647
{% else %}
4748
final Map<String, String> headers = {
4849
{% for key, header in method.headers %}
4950
'{{ key }}': '{{ header }}',
5051
{% endfor %}
5152
};
5253

53-
return client.call(HttpMethod.{{ method.method | caseLower }}, path: path, params: params, headers: headers);
54+
final res = await client.call(HttpMethod.{{ method.method | caseLower }}, path: path, params: params, headers: headers);
55+
return {% if method.responseModel and method.responseModel != 'any' %}models.{{method.responseModel | ucFirstAndEscape}}.fromMap(res.data){% else %} res.data{% endif %};
5456
{% endif %}
5557
}
5658
{% endfor %}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | ucFirstAndEscape}}>{% else %}{{property.sub_schema | ucFirstAndEscape }}{% endif %}{% else %}{{property.type | typeName}}{% endif %}{% endmacro %}
2+
part of {{ language.params.packageName }}.models;
3+
4+
/// {{ definition.description }}
5+
class {{ definition.name | ucFirstAndEscape }} {
6+
{% for property in definition.properties %}
7+
/// {{ property.description }}
8+
final {% if not property.required %}{{_self.sub_schema(property)}}? {{ property.name | escapeKeyword }}{% else %}{{_self.sub_schema(property)}} {{ property.name | escapeKeyword }}{% endif %};
9+
{% endfor %}
10+
{% if definition.additionalProperties %}
11+
final Map<String, dynamic> data;
12+
{% endif %}
13+
14+
{{ definition.name | ucFirstAndEscape}}({
15+
{% for property in definition.properties %}{% if property.required %}
16+
required {% endif %}this.{{ property.name | escapeKeyword }},
17+
{% endfor %}
18+
{% if definition.additionalProperties %}
19+
required this.data,
20+
{% endif %}
21+
});
22+
23+
factory {{ definition.name | ucFirstAndEscape}}.fromMap(Map<String, dynamic> map) {
24+
return {{ definition.name | ucFirstAndEscape }}(
25+
{% for property in definition.properties %}
26+
{{ property.name | escapeKeyword }}: {% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | ucFirstAndEscape}}>.from(map['{{property.name | escapeDollarSign }}'].map((p) => {{property.sub_schema | ucFirstAndEscape}}.fromMap(p))){% else %}{{property.sub_schema | ucFirstAndEscape}}.fromMap(map['{{property.name | escapeDollarSign }}']){% endif %}{% else %}map['{{property.name | escapeDollarSign }}']{% if property.type == "number" %}.toDouble(){% endif %}{% if property.type == "string" %}.toString(){% endif %}{% endif %},
27+
{% endfor %}
28+
{% if definition.additionalProperties %}
29+
data: map,
30+
{% endif %}
31+
);
32+
}
33+
34+
Map<String, dynamic> toMap() {
35+
return {
36+
{% for property in definition.properties %}
37+
"{{ property.name | escapeDollarSign }}": {% if property.sub_schema %}{% if property.type == 'array' %}{{property.name | escapeKeyword}}.map((p) => p.toMap()){% else %}{{property.name | escapeKeyword}}.toMap(){% endif %}{% else %}{{property.name | escapeKeyword }}{% endif %},
38+
{% endfor %}
39+
{% if definition.additionalProperties %}
40+
"data": data,
41+
{% endif %}
42+
};
43+
}
44+
{% if definition.additionalProperties %}
45+
46+
T convertTo<T>(T Function(Map) fromJson) => fromJson(data);
47+
{% endif %}
48+
{% for property in definition.properties %}
49+
{% if property.sub_schema %}
50+
{% for def in spec.definitions %}
51+
{% if def.name == property.sub_schema and def.additionalProperties and property.type == 'array' %}
52+
53+
List<T> convertTo<T>(T Function(Map) fromJson) =>
54+
{{property.name}}.map((d) => d.convertTo<T>(fromJson)).toList();
55+
{% endif %}
56+
{% endfor %}
57+
{% endif %}
58+
{% endfor %}
59+
}

tests/languages/dart/tests.dart

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import '../lib/packageName.dart';
2+
import '../lib/models.dart';
23

34
void main() async {
45
Client client = Client();
@@ -12,49 +13,49 @@ void main() async {
1213
print('\nTest Started');
1314

1415
// Foo Tests
15-
Response response;
16+
Mock response;
1617
response = await foo.get(x: 'string', y: 123, z: ['string in array']);
17-
print(response.data['result']);
18+
print(response.result);
1819

1920
response = await foo.post(x: 'string', y: 123, z: ['string in array']);
20-
print(response.data['result']);
21+
print(response.result);
2122

2223
response = await foo.put(x: 'string', y: 123, z: ['string in array']);
23-
print(response.data['result']);
24+
print(response.result);
2425

2526
response = await foo.patch(x: 'string', y: 123, z: ['string in array']);
26-
print(response.data['result']);
27+
print(response.result);
2728

2829
response = await foo.delete(x: 'string', y: 123, z: ['string in array']);
29-
print(response.data['result']);
30+
print(response.result);
3031

3132
// Bar Tests
3233

3334
response = await bar.get(xrequired: 'string', xdefault: 123, z: ['string in array']);
34-
print(response.data['result']);
35+
print(response.result);
3536

3637
response = await bar.post(xrequired: 'string', xdefault: 123, z: ['string in array']);
37-
print(response.data['result']);
38+
print(response.result);
3839

3940
response = await bar.put(xrequired: 'string', xdefault: 123, z: ['string in array']);
40-
print(response.data['result']);
41+
print(response.result);
4142

4243
response = await bar.patch(xrequired: 'string', xdefault: 123, z: ['string in array']);
43-
print(response.data['result']);
44+
print(response.result);
4445

4546
response = await bar.delete(xrequired: 'string', xdefault: 123, z: ['string in array']);
46-
print(response.data['result']);
47+
print(response.result);
4748

4849
// General Tests
4950

50-
response = await general.redirect();
51-
print(response.data['result']);
51+
final res = await general.redirect();
52+
print(res['result']);
5253

5354
final file = await MultipartFile.fromPath('file', '../../resources/file.png',
5455
filename: 'file.png');
5556
response = await general.upload(
5657
x: 'string', y: 123, z: ['string in array'], file: file);
57-
print(response.data['result']);
58+
print(response.result);
5859

5960
try {
6061
await general.error400();
@@ -75,8 +76,8 @@ void main() async {
7576
}
7677

7778
// response = await general.setCookie();
78-
// print(response.data['result']);
79+
// print(response.result);
7980

8081
// response = await general.getCookie();
81-
// print(response.data['result']);
82+
// print(response.result);
8283
}

0 commit comments

Comments
 (0)