@dryv/crow-backendv0.1.0
Crow route registration with typed wire models and separated service interfaces.
dryv pack add @dryv/crow-backend#pragma once
#include <cstdint>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include <crow.h>
#include <nlohmann/json.hpp>
#include "dryv_backend/http.hpp"
{%- for dependency in file.dependencies %}{% if dependency.symbol and not dependency.is_self %}
#include "{{ dependency.path.relative | replace('include/', '') }}"
{%- endif %}{% endfor %}
namespace {{ pack.inputs.namespace }} {
{%- macro cpp_ident(name) -%}
{%- if name in ['alignas','alignof','and','and_eq','asm','atomic_cancel','atomic_commit','atomic_noexcept','auto','bitand','bitor','bool','break','case','catch','char','char8_t','char16_t','char32_t','class','compl','concept','const','consteval','constexpr','constinit','const_cast','continue','co_await','co_return','co_yield','decltype','default','delete','do','double','dynamic_cast','else','enum','explicit','export','extern','false','float','for','friend','goto','if','inline','int','long','mutable','namespace','new','noexcept','not','not_eq','nullptr','operator','or','or_eq','private','protected','public','reflexpr','register','reinterpret_cast','requires','return','short','signed','sizeof','static','static_assert','static_cast','struct','switch','synchronized','template','this','thread_local','throw','true','try','typedef','typeid','typename','union','unsigned','using','virtual','void','volatile','wchar_t','while','xor','xor_eq'] -%}{{ name }}_{%- else -%}{{ name }}{%- endif -%}
{%- endmacro -%}
{%- macro symbol_for(link) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == link.id and dependency.symbol -%}{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{%- macro base_type(value) -%}
{%- if value is none or value.is.unknown or value.is.dynamic or value.is.generic or value.is.composite or value.is.union or value.is.tuple or value.is.literal -%}nlohmann::json
{%- elif value.is.schema -%}{{ symbol_for(value.schema) or 'nlohmann::json' }}
{%- elif value.is.enum -%}{{ symbol_for(value.property.enum) or 'nlohmann::json' }}
{%- elif value.is.boolean -%}bool
{%- elif value.is.integer -%}std::int64_t
{%- elif value.is.number -%}double
{%- elif value.is.decimal or value.is.money -%}std::string
{%- elif value.is.record -%}nlohmann::json
{%- elif value.is.string or value.is.temporal -%}std::string
{%- else -%}nlohmann::json
{%- endif -%}
{%- endmacro -%}
{%- macro inner_type(field) -%}
{%- if field.array is not none -%}std::vector<{{ base_type(field.value) }}>{%- else -%}{{ base_type(field.value) }}{%- endif -%}
{%- endmacro -%}
{%- macro field_type(field) -%}
{%- if field.optional or field.nullable -%}std::optional<{{ inner_type(field) }}>{%- else -%}{{ inner_type(field) }}{%- endif -%}
{%- endmacro -%}
{%- macro input_type(operation) -%}
{%- if operation.input.kind == 'schema' -%}{{ base_type(operation.input.value) }}{%- else -%}{{ operation.name.pascal }}Input{%- endif -%}
{%- endmacro -%}
{%- macro method_enum(http) -%}
{%- if http.is.get -%}crow::HTTPMethod::Get
{%- elif http.is.post -%}crow::HTTPMethod::Post
{%- elif http.is.put -%}crow::HTTPMethod::Put
{%- elif http.is.patch -%}crow::HTTPMethod::Patch
{%- elif http.is.delete -%}crow::HTTPMethod::Delete
{%- elif http.is.head -%}crow::HTTPMethod::Head
{%- elif http.is.options -%}crow::HTTPMethod::Options
{%- else -%}crow::HTTPMethod::Get
{%- endif -%}
{%- endmacro -%}
{%- macro crow_path(http) -%}
{%- set state = namespace(path=http.path.local) -%}
{%- for binding in http.input_groups.path -%}
{%- set marker = '{' ~ (binding.name or binding.port.name.raw) ~ '}' -%}
{%- set state.path = state.path | replace(marker, '<string>') -%}
{%- endfor -%}
{{- state.path -}}
{%- endmacro -%}
{%- macro success_status(http) -%}
{%- set result = namespace(code=200) -%}
{%- for binding in http.outputs -%}{%- set result.code = binding.status -%}{%- endfor -%}
{{- result.code -}}
{%- endmacro -%}
{%- macro whole_binding(http, kind) -%}
{%- for binding in http.inputs -%}
{%- if binding.port.field is none and ((kind == 'body' and binding.is.body) or (kind == 'query_object' and binding.is.query_object) or (kind == 'form' and binding.is.form)) -%}x{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{%- macro matching_binding(operation, field) -%}
{%- for binding in operation.facets.http.inputs -%}
{%- if binding.port.field is not none and binding.port.field.raw == field.name.raw -%}{{ loop.index0 }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{%- macro assign_string(field, raw, target) -%}
{%- if field.optional or field.nullable %}
if ({{ raw }}.has_value()) {
{{ target }} = parse_wire_value<{{ inner_type(field) }}>(*{{ raw }});
} else {
{{ target }} = std::nullopt;
}
{%- else %}
if (!{{ raw }}.has_value()) {
throw std::invalid_argument("missing required field {{ field.name.raw }}");
}
{{ target }} = parse_wire_value<{{ inner_type(field) }}>(*{{ raw }});
{%- endif %}
{%- endmacro -%}
{%- macro assign_list(field, raw, target) -%}
{%- if field.optional or field.nullable %}
if (!{{ raw }}.empty()) {
std::vector<{{ base_type(field.value) }}> parsed;
parsed.reserve({{ raw }}.size());
for (const auto& item : {{ raw }}) {
parsed.push_back(parse_wire_value<{{ base_type(field.value) }}>(item));
}
{{ target }} = std::move(parsed);
} else {
{{ target }} = std::nullopt;
}
{%- else %}
if ({{ raw }}.empty()) {
throw std::invalid_argument("missing required field {{ field.name.raw }}");
}
{{ target }}.clear();
{{ target }}.reserve({{ raw }}.size());
for (const auto& item : {{ raw }}) {
{{ target }}.push_back(parse_wire_value<{{ base_type(field.value) }}>(item));
}
{%- endif %}
{%- endmacro -%}
{%- macro assign_field(operation, field, target) -%}
{%- set state = namespace(found=false) -%}
{%- for binding in operation.facets.http.inputs -%}
{%- if binding.port.field is not none and binding.port.field.raw == field.name.raw -%}
{%- set state.found=true -%}
{%- if binding.is.path %}
{
const std::optional<std::string> raw = path_{{ cpp_ident(binding.port.name.snake) }};
{{ assign_string(field, 'raw', target) }}
}
{%- elif binding.is.query %}
{
{%- if field.array is not none %}
const auto raw = query_values(request, {{ (binding.name or binding.port.name.raw) | tojson }});
{{ assign_list(field, 'raw', target) }}
{%- else %}
const auto raw = first_query_value(request, {{ (binding.name or binding.port.name.raw) | tojson }});
{{ assign_string(field, 'raw', target) }}
{%- endif %}
}
{%- elif binding.is.header %}
{
const auto raw = header_value(request, {{ (binding.name or binding.port.name.raw) | tojson }});
{{ assign_string(field, 'raw', target) }}
}
{%- elif binding.is.cookie %}
{
const auto raw = cookie_value(request, {{ (binding.name or binding.port.name.raw) | tojson }});
{{ assign_string(field, 'raw', target) }}
}
{%- elif binding.is.form %}
{
{%- if field.array is not none %}
const auto raw = form_list(form, {{ (binding.name or binding.port.name.raw) | tojson }});
{{ assign_list(field, 'raw', target) }}
{%- else %}
const auto raw = form_value(form, {{ (binding.name or binding.port.name.raw) | tojson }});
{{ assign_string(field, 'raw', target) }}
{%- endif %}
}
{%- elif binding.is.body %}
{
if (!body_json.is_object() || !body_json.contains({{ field.name.raw | tojson }})) {
{%- if field.optional %}
{{ target }} = std::nullopt;
{%- else %}
throw std::invalid_argument("missing required body field {{ field.name.raw }}");
{%- endif %}
} else if (body_json.at({{ field.name.raw | tojson }}).is_null()) {
{%- if field.optional or field.nullable %}
{{ target }} = std::nullopt;
{%- else %}
throw std::invalid_argument("body field {{ field.name.raw }} cannot be null");
{%- endif %}
} else {
{{ target }} = body_json.at({{ field.name.raw | tojson }}).get<{{ inner_type(field) }}>();
}
}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- if not state.found and field.optional %}
{{ target }} = std::nullopt;
{%- endif %}
{%- endmacro -%}
{%- for operation in operations %}
{%- if operation.input and operation.input.kind != 'schema' %}
struct {{ operation.name.pascal }}Input {
{%- for field in operation.input.fields %}
{{ field_type(field) }} {{ cpp_ident(field.name.snake) }}{};
{%- endfor %}
};
{%- endif %}
{%- endfor %}
class {{ file.symbols.service }} {
public:
virtual ~{{ file.symbols.service }}() = default;
{%- for operation in operations %}
virtual {% if operation.output %}{{ base_type(operation.output.value) }}{% else %}void{% endif %} {{ cpp_ident(operation.name.snake) }}(
{%- if operation.input %}
const {{ input_type(operation) }}& input
{%- endif %}
) = 0;
{%- endfor %}
};
inline void {{ file.symbols.register }}(crow::SimpleApp& app, {{ file.symbols.service }}& service) {
{%- for operation in operations %}
{%- set http = operation.facets.http %}
app.route_dynamic({{ crow_path(http) | tojson }})
.methods({{ method_enum(http) }})
([&service](const crow::request& request{% for binding in http.input_groups.path %}, std::string path_{{ cpp_ident(binding.port.name.snake) }}{% endfor %}) -> crow::response {
{%- if http.input_groups.multipart %}
return json_response(501, nlohmann::json{{"message", "multipart bindings require an explicit C++ semantic mapping"}});
{%- else %}
try {
{%- if http.input_groups.form %}
const auto form = form_values(request);
{%- endif %}
{%- if http.input_groups.body %}
const auto body_json = parse_json_body(request);
{%- endif %}
{%- if operation.input %}
{%- if operation.input.kind == 'schema' and whole_binding(http, 'body') %}
auto input = body_json.get<{{ input_type(operation) }}>();
{%- elif operation.input.kind == 'schema' and whole_binding(http, 'query_object') %}
nlohmann::json input_json = nlohmann::json::object();
{%- for field in operation.input.fields %}
{%- if field.array is not none %}
{
const auto raw = query_values(request, {{ field.name.raw | tojson }});
if (!raw.empty()) {
nlohmann::json values = nlohmann::json::array();
for (const auto& item : raw) {
values.push_back(parse_wire_value<{{ base_type(field.value) }}>(item));
}
input_json[{{ field.name.raw | tojson }}] = std::move(values);
}{% if not field.optional %} else {
throw std::invalid_argument("missing required query field {{ field.name.raw }}");
}{% endif %}
}
{%- else %}
if (const auto raw = first_query_value(request, {{ field.name.raw | tojson }}); raw.has_value()) {
input_json[{{ field.name.raw | tojson }}] = parse_wire_value<{{ base_type(field.value) }}>(*raw);
}{% if not field.optional %} else {
throw std::invalid_argument("missing required query field {{ field.name.raw }}");
}{% endif %}
{%- endif %}
{%- endfor %}
auto input = input_json.get<{{ input_type(operation) }}>();
{%- elif operation.input.kind == 'schema' and whole_binding(http, 'form') %}
nlohmann::json input_json = nlohmann::json::object();
{%- for field in operation.input.fields %}
{%- if field.array is not none %}
{
const auto raw = form_list(form, {{ field.name.raw | tojson }});
if (!raw.empty()) {
nlohmann::json values = nlohmann::json::array();
for (const auto& item : raw) {
values.push_back(parse_wire_value<{{ base_type(field.value) }}>(item));
}
input_json[{{ field.name.raw | tojson }}] = std::move(values);
}{% if not field.optional %} else {
throw std::invalid_argument("missing required form field {{ field.name.raw }}");
}{% endif %}
}
{%- else %}
if (const auto raw = form_value(form, {{ field.name.raw | tojson }}); raw.has_value()) {
input_json[{{ field.name.raw | tojson }}] = parse_wire_value<{{ base_type(field.value) }}>(*raw);
}{% if not field.optional %} else {
throw std::invalid_argument("missing required form field {{ field.name.raw }}");
}{% endif %}
{%- endif %}
{%- endfor %}
auto input = input_json.get<{{ input_type(operation) }}>();
{%- else %}
{{ input_type(operation) }} input{};
{%- for field in operation.input.fields %}
{{ assign_field(operation, field, 'input.' ~ cpp_ident(field.name.snake)) }}
{%- endfor %}
{%- endif %}
{%- endif %}
{%- if operation.output %}
auto output = service.{{ cpp_ident(operation.name.snake) }}({% if operation.input %}input{% endif %});
return json_response({{ success_status(http) }}, nlohmann::json(output));
{%- else %}
service.{{ cpp_ident(operation.name.snake) }}({% if operation.input %}input{% endif %});
return crow::response({{ success_status(http) }});
{%- endif %}
} catch (const std::invalid_argument& error) {
return bad_request(error);
} catch (const std::out_of_range& error) {
return bad_request(error);
}
{%- endif %}
});
{%- endfor %}
}
} // namespace {{ pack.inputs.namespace }}