Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use identifier overrides to override conflicting identifier name #327

Merged
merged 2 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ SDK Language name (JS, PHP…)
**getKeywords**
An array with language keywords to avoid using as param or function names, template engine will solve conflicts

**getIdentifierOverrides**
Returns an associative array that can be used to override keywords with pre-defined word using `overrideIdentifier` filter.

**getFiles**
An array with a list of language template files in [twig format](https://twig.symfony.com/).
Each file scope determines what template parameters will be available.
Expand Down
2 changes: 1 addition & 1 deletion src/SDK/Language/Dart.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function getKeywords()
*/
public function getIdentifierOverrides()
{
return [];
return ['Function' => 'Func'];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions templates/dart/lib/services/service.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class {{ service.name | caseUcfirst }} extends Service {
{{ method.description|dartComment }}
///
{% endif %}
{% 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 {
{% if method.type == 'webAuth' %}Future{% elseif method.type == 'location' %} Future<Uint8List> {% else %} {% if method.responseModel and method.responseModel != 'any' %}Future<models.{{method.responseModel | caseUcfirst | overrideIdentifier}}>{% else %}Future{% endif %}{% endif %} {{ method.name | caseCamel }}({{ _self.method_parameters(method.parameters) }}) async {
final String path = '{{ method.path }}'{% for parameter in method.parameters.path %}.replaceAll(RegExp('{{ '{' }}{{ parameter.name | caseCamel }}{{ '}' }}'), {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %};

final Map<String, dynamic> params = {
Expand Down Expand Up @@ -52,7 +52,7 @@ class {{ service.name | caseUcfirst }} extends Service {
};

final res = await client.call(HttpMethod.{{ method.method | caseLower }}, path: path, params: params, headers: headers);
return {% if method.responseModel and method.responseModel != 'any' %}models.{{method.responseModel | ucFirstAndEscape}}.fromMap(res.data){% else %} res.data{% endif %};
return {% if method.responseModel and method.responseModel != 'any' %}models.{{method.responseModel | caseUcfirst | overrideIdentifier}}.fromMap(res.data){% else %} res.data{% endif %};
{% endif %}
}
{% endfor %}
Expand Down
12 changes: 6 additions & 6 deletions templates/dart/lib/src/models/model.dart.twig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% 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 %}
{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>{% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier }}{% endif %}{% else %}{{property.type | typeName}}{% endif %}{% endmacro %}
part of {{ language.params.packageName }}.models;

/// {{ definition.description }}
class {{ definition.name | ucFirstAndEscape }} {
class {{ definition.name | caseUcfirst | overrideIdentifier }} {
{% for property in definition.properties %}
/// {{ property.description }}
final {% if not property.required %}{{_self.sub_schema(property)}}? {{ property.name | escapeKeyword }}{% else %}{{_self.sub_schema(property)}} {{ property.name | escapeKeyword }}{% endif %};
Expand All @@ -11,7 +11,7 @@ class {{ definition.name | ucFirstAndEscape }} {
final Map<String, dynamic> data;
{% endif %}

{{ definition.name | ucFirstAndEscape}}({
{{ definition.name | caseUcfirst | overrideIdentifier}}({
{% for property in definition.properties %}{% if property.required %}
required {% endif %}this.{{ property.name | escapeKeyword }},
{% endfor %}
Expand All @@ -20,10 +20,10 @@ class {{ definition.name | ucFirstAndEscape }} {
{% endif %}
});

factory {{ definition.name | ucFirstAndEscape}}.fromMap(Map<String, dynamic> map) {
return {{ definition.name | ucFirstAndEscape }}(
factory {{ definition.name | caseUcfirst | overrideIdentifier}}.fromMap(Map<String, dynamic> map) {
return {{ definition.name | caseUcfirst | overrideIdentifier }}(
{% for property in definition.properties %}
{{ 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 %},
{{ property.name | escapeKeyword }}: {% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>.from(map['{{property.name | escapeDollarSign }}'].map((p) => {{property.sub_schema | caseUcfirst | overrideIdentifier}}.fromMap(p))){% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}.fromMap(map['{{property.name | escapeDollarSign }}']){% endif %}{% else %}map['{{property.name | escapeDollarSign }}']{% if property.type == "number" %}.toDouble(){% endif %}{% if property.type == "string" %}.toString(){% endif %}{% endif %},
{% endfor %}
{% if definition.additionalProperties %}
data: map,
Expand Down