Files
chatterino2/lib/twitch-eventsub-ws/ast/lib/templates/struct-implementation.tmpl

55 lines
2.0 KiB
Cheetah

boost::json::result_for<{{struct.full_name}}, boost::json::value>::type tag_invoke(
boost::json::try_value_to_tag<{{struct.full_name}}>, const boost::json::value &jvRoot)
{
{% if struct.inner_root %}
if (!jvRoot.is_object())
{
EVENTSUB_BAIL_HERE(error::Kind::ExpectedObject);
}
const auto &outerRoot = jvRoot.get_object();
const auto *jvInnerRoot = outerRoot.if_contains("{{struct.inner_root}}");
if (jvInnerRoot == nullptr)
{
EVENTSUB_BAIL_HERE(error::Kind::InnerRootMissing);
}
if (!jvInnerRoot->is_object())
{
EVENTSUB_BAIL_HERE(error::Kind::ExpectedObject);
}
const auto &root = jvInnerRoot->get_object();
{% else %}
if (!jvRoot.is_object())
{
EVENTSUB_BAIL_HERE(error::Kind::ExpectedObject);
}
const auto &root = jvRoot.get_object();
{% endif %}
{% for field in struct.members %}
{% if field.member_type == MemberType.BASIC -%}
{% include 'field-basic.tmpl' indent content %}
{%- elif field.member_type == MemberType.VECTOR -%}
{% include 'field-vector.tmpl' indent content %}
{%- elif field.member_type == MemberType.OPTIONAL -%}
{% include 'field-optional.tmpl' indent content %}
{%- elif field.member_type == MemberType.OPTIONAL_VECTOR -%}
{% include 'field-optional-vector.tmpl' indent content %}
{%- endif -%}
{% endfor %}
return {{struct.full_name}}{
{%- for field in struct.members %}
{% if field.member_type == MemberType.BASIC -%}
{% include 'initializer-basic.tmpl' indent content %}
{%- elif field.member_type == MemberType.VECTOR -%}
{% include 'initializer-vector.tmpl' indent content %}
{%- elif field.member_type == MemberType.OPTIONAL -%}
{% include 'initializer-optional.tmpl' indent content %}
{%- elif field.member_type == MemberType.OPTIONAL_VECTOR -%}
{% include 'initializer-optional-vector.tmpl' indent content %}
{%- endif -%}
{% endfor %}
};
}