refactor(eventsub): use variants for sum types (#5930)
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
{% if not field.tag -%}
|
||||
static_assert(false, "{{field.name}} doesn't have a json_tag");
|
||||
{%- endif %}
|
||||
const auto *jv{{field.name}}Tag = root.if_contains("{{field.tag}}");
|
||||
if (jv{{field.name}}Tag == nullptr)
|
||||
{
|
||||
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
|
||||
}
|
||||
|
||||
auto {{field.name}}TagRes = boost::json::try_value_to<boost::json::string>(*jv{{field.name}}Tag);
|
||||
if ({{field.name}}TagRes.has_error())
|
||||
{
|
||||
return {{field.name}}TagRes.error();
|
||||
}
|
||||
std::string_view {{field.name}}Tag = *{{field.name}}TagRes;
|
||||
decltype(std::declval<{{struct.full_name}}>().{{field.name}}) {{field.name}};
|
||||
{%- for type in field.variant_types %}
|
||||
|
||||
{%- if not loop.first %}else{% endif %} if ({{field.name}}Tag == {{type.name}}::TAG)
|
||||
{
|
||||
{%- if type.empty -%}
|
||||
{{field.name}}.emplace<{{type.name}}>();
|
||||
{%- else -%}
|
||||
const auto *{{field.name}}Val = root.if_contains(detail::fieldFor<{{type.name}}>());
|
||||
if (!{{field.name}}Val)
|
||||
{
|
||||
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
|
||||
}
|
||||
auto {{field.name}}{{type.name}} = boost::json::try_value_to<{{type.name}}>(*{{field.name}}Val);
|
||||
if ({{field.name}}{{type.name}}.has_error())
|
||||
{
|
||||
return {{field.name}}{{type.name}}.error();
|
||||
}
|
||||
{% if type.trivial -%}
|
||||
{{field.name}}.emplace<{{type.name}}>({{field.name}}{{type.name}}.value());
|
||||
{%- else -%}
|
||||
{{field.name}}.emplace<{{type.name}}>(std::move({{field.name}}{{type.name}}.value()));
|
||||
{%- endif %}
|
||||
{%- endif -%}
|
||||
}
|
||||
{%- endfor -%}
|
||||
else
|
||||
{
|
||||
{%- if field.variant_fallback -%}
|
||||
{{field.name}}.emplace<{{field.variant_fallback}}>({{field.name}}Tag);
|
||||
{%- else -%}
|
||||
EVENTSUB_BAIL_HERE(error::Kind::UnknownVariant);
|
||||
{%- endif -%}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
.{{field.name}} = std::move({{field.name}}),
|
||||
@@ -1,5 +1,5 @@
|
||||
boost::json::result_for<{{struct.full_name}}, boost::json::value>::type tag_invoke(
|
||||
boost::json::try_value_to_tag<{{struct.full_name}}> /* tag */, const boost::json::value &jvRoot)
|
||||
boost::json::try_value_to_tag<{{struct.full_name}}> /* tag */, const boost::json::value &{%- if struct.members|length -%}jvRoot{%- else -%}/* jvRoot */{%- endif -%})
|
||||
{
|
||||
{% if struct.inner_root %}
|
||||
if (!jvRoot.is_object())
|
||||
@@ -38,6 +38,8 @@ boost::json::result_for<{{struct.full_name}}, boost::json::value>::type tag_invo
|
||||
{% include 'field-optional.tmpl' indent content %}
|
||||
{%- elif field.member_type == MemberType.OPTIONAL_VECTOR -%}
|
||||
{% include 'field-optional-vector.tmpl' indent content %}
|
||||
{%- elif field.member_type == MemberType.VARIANT -%}
|
||||
{% include 'field-variant.tmpl' indent content %}
|
||||
{%- endif -%}
|
||||
{% endfor %}
|
||||
|
||||
@@ -51,6 +53,8 @@ boost::json::result_for<{{struct.full_name}}, boost::json::value>::type tag_invo
|
||||
{% include 'initializer-optional.tmpl' indent content %}
|
||||
{%- elif field.member_type == MemberType.OPTIONAL_VECTOR -%}
|
||||
{% include 'initializer-optional-vector.tmpl' indent content %}
|
||||
{%- elif field.member_type == MemberType.VARIANT -%}
|
||||
{% include 'initializer-variant.tmpl' indent content %}
|
||||
{%- endif -%}
|
||||
{% endfor %}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user