Skip to content

Commit

Permalink
Prettify template output (#131)
Browse files Browse the repository at this point in the history
* Prettify template output


Signed-off-by: Andreas Heinrich <[email protected]>
  • Loading branch information
andistorm authored Apr 25, 2024
1 parent 16c3681 commit 9613ade
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 44 deletions.
18 changes: 9 additions & 9 deletions ev-dev-tools/src/ev_cli/templates/helper_macros.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ std::variant<{{ type.cpp_type|join(', ') }}>
{% if interface is not none and 'object_type' in type %}
{{ interface + '::' + type.object_type }}
{% else %}
{% if 'object_type' in type %}
{% if 'object_type' in type -%}
{{ type.object_type }}
{% elif 'enum_type' in type %}
{%- elif 'enum_type' in type -%}
{{ type.enum_type }}
{% elif 'array_type' in type %}
{%- elif 'array_type' in type -%}
std::vector<{{ type.array_type }}>
{% else %}
{%- else -%}
{{ type.cpp_type }}
{% endif %}
{%- endif %}
{%- endif %}
{%- endif %}
{%- endmacro %}
Expand Down Expand Up @@ -99,16 +99,16 @@ void publish_{{ var.name }}({% if var.json_type != 'null' %}{{ cpp_type(var) }}

{% macro string_to_enum(enum_type) %}
{% if '::' not in enum_type %}
string_to_{{ enum_type | snake_case }}
string_to_{{ enum_type | snake_case -}}
{% else %}
{{ enum_type[:enum_type.rfind('::')] }}::string_to_{{ enum_type[(enum_type.rfind('::')+2):] | snake_case }}
{{ enum_type[:enum_type.rfind('::')] }}::string_to_{{ enum_type[(enum_type.rfind('::')+2):] | snake_case -}}
{% endif %}
{%- endmacro %}

{% macro enum_to_string(enum_type) %}
{% if '::' not in enum_type %}
{{ enum_type | snake_case }}_to_string
{% else %}
{%- else %}
{{ enum_type[:enum_type.rfind('::')] }}::{{ enum_type[(enum_type.rfind('::')+2):] | snake_case }}_to_string
{% endif %}
{%- endif %}
{%- endmacro %}
67 changes: 33 additions & 34 deletions ev-dev-tools/src/ev_cli/templates/interface-Base.hpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public:
// publish functions for variables
{% for var in vars %}
void publish_{{ var.name }}(
{% if 'object_type' in var %}
{{ var.object_type }}
{% elif 'enum_type' in var %}
{{ var.enum_type }}
{% else %}
{{ var.cpp_type }}
{% endif %}
value) {
{%- if 'object_type' in var %}
{{- var.object_type }}
{%- elif 'enum_type' in var %}
{{- var.enum_type }}
{%- else %}
{{- var.cpp_type }}
{%- endif -%}
{{ ' value) {' }}
{% if 'object_type' in var %}
json json_value = value;
Object object_value = json_value;
Expand Down Expand Up @@ -105,41 +105,40 @@ private:
};
{% endif %}
{{ cmd.name }}_cmd.cmd = [this](Parameters args) -> Result {
{% for arg in cmd.args %}
{% if 'object_type' in arg %}
{{ arg.object_type }} {{ arg.name }} = args["{{ arg.name }}"];
{% elif 'enum_type' in arg %}
auto {{ arg.name }} = {{ string_to_enum(arg.enum_type) }}(args["{{ arg.name }}"]);
{% elif 'array_type' in arg %}
json {{ arg.name }}_json_array = args["{{ arg.name }}"];
{% if 'array_type_contains_enum' in arg %}
std::vector<{{ arg.array_type }}> {{ arg.name }};
for (auto entry : {{ arg.name }}_json_array) {
{{ arg.name }}.push_back({{ string_to_enum(arg.array_type) }}(entry));
}
{% else %}
std::vector<{{ arg.array_type }}> {{ arg.name }} = args["{{ arg.name }}"];
{% endif %}
{% for arg in cmd.args %}
{% if 'object_type' in arg %}
{{ arg.object_type }} {{ arg.name }} = args["{{ arg.name }}"];
{% elif 'enum_type' in arg %}
auto {{ arg.name }} = {{ string_to_enum(arg.enum_type) }}(args["{{ arg.name }}"]);
{% elif 'array_type' in arg %}
json {{ arg.name }}_json_array = args["{{ arg.name }}"];
{% if 'array_type_contains_enum' in arg %}
std::vector<{{ arg.array_type }}> {{ arg.name }};
for (auto entry : {{ arg.name }}_json_array) {
{{ arg.name }}.push_back({{ string_to_enum(arg.array_type) }}(entry));
}
{% else %}
auto {{ arg.name }} = {{ var_to_cpp(arg) }}(args["{{ arg.name }}"]);
std::vector<{{ arg.array_type }}> {{ arg.name }} = args["{{ arg.name }}"];
{% endif %}
{% else %}
(void) args; // no arguments used for this callback
{% endfor %}
auto {{ arg.name }} = {{ var_to_cpp(arg) }}(args["{{ arg.name }}"]);
{% endif %}
{% else %}
(void) args; // no arguments used for this callback
{% endfor %}

{{ 'auto result = ' if cmd.result }}this->handle_{{ cmd.name }}(
{%- for arg in cmd.args -%}
{{ arg.name }}{{ ', ' if not loop.last }}
{%- endfor -%}
);
{% if cmd.result and 'object_type' in cmd.result %}
return result;
{% elif cmd.result and 'enum_type' in cmd.result %}
return {{ enum_to_string(cmd.result.enum_type) }}(result);
{% else %}
return {{ var_to_any(cmd.result, 'result') if cmd.result else 'nullptr'}};
{% endif %}

{% if cmd.result and 'object_type' in cmd.result %}
return result;
{% elif cmd.result and 'enum_type' in cmd.result %}
return {{ enum_to_string(cmd.result.enum_type) }}(result);
{% else %}
return {{ var_to_any(cmd.result, 'result') if cmd.result else 'nullptr'}};
{% endif %}
};
{% if cmd.result %}
{{ cmd.name }}_cmd.return_type = {{ list_json_types(cmd.result.json_type) }};
Expand Down
2 changes: 1 addition & 1 deletion ev-dev-tools/src/ev_cli/templates/interface-Exports.hpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public:
args["{{ arg.name }}"] = {{ var_to_any(arg, arg.name) }};
{% endif%}
{% endfor %}
{% if cmd.result %}Result result = {% endif %}_adapter->call(_req, "{{ cmd.name }}", args);
{{ '' }}{% if cmd.result %}Result result = {% endif %}_adapter->call(_req, "{{ cmd.name }}", args);
{% if cmd.result %}
{% if 'enum_type' in cmd.result %}
auto retval = {{ string_to_enum(cmd.result.enum_type) }}({{ var_to_cpp(cmd.result) }}(result.value()));
Expand Down

0 comments on commit 9613ade

Please sign in to comment.