Skip to content
Open
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
16 changes: 16 additions & 0 deletions chaotic/chaotic/back/cpp/templates/type.cpp.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@
{% endif %}
{% endmacro %}

{% macro generate_isconvertible_definition(type) %}
{# handle subtypes #}
{%- for schema in type.subtypes() -%}
{{ generate_isconvertible_definition(schema) }}
{% endfor %}

{% if type.get_py_type() == 'CppStringEnum' %}
bool IsConvertible(std::string_view value,
{{ userver }}::formats::parse::To<{{ shortest_cpp_name(type) }}>)
{
return k{{ type.cpp_global_struct_field_name() }}_Mapping.TryFindBySecond(value).has_value();
}
{% endif %}
{% endmacro %}

{% macro generate_operator_eq_definition(type) %}
{% set local_name = shortest_cpp_name(type) %}
Expand Down Expand Up @@ -312,6 +326,8 @@
{% endif %}

{{ generate_tostring_definition(type) }}

{{ generate_isconvertible_definition(type) }}
{% endfor %}

{{ common.switch_namespace('') }}
Expand Down
15 changes: 15 additions & 0 deletions chaotic/chaotic/back/cpp/templates/type.hpp.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,19 @@
{% endif %}
{% endmacro %}

{% macro generate_isconvertible_declaration(type) %}
{# handle subtypes #}
{%- for schema in type.subtypes() -%}
{{ generate_isconvertible_declaration(schema) }}
{% endfor %}

{% if type.get_py_type() == 'CppStringEnum' %}
bool IsConvertible(std::string_view value,
{{ userver }}::formats::parse::To<{{ shortest_cpp_name(type) }}>);
{% endif %}
{% endmacro %}


{% macro generate_fmt_formatter_declaration(type) %}
{# handle subtypes #}
{%- for schema in type.subtypes() -%}
Expand Down Expand Up @@ -328,6 +341,8 @@
{% endif %}

{{ generate_tostring_declaration(type) }}

{{ generate_isconvertible_declaration(type) }}
{% endfor %}

{{ common.switch_namespace('') }}
Expand Down
4 changes: 4 additions & 0 deletions chaotic/golden_tests/output/schemas/enum/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ std::string ToString(Enum::Foo value) {
throw std::runtime_error(fmt::format("Invalid enum value: {}", static_cast<int>(value)));
}

bool IsConvertible(std::string_view value, USERVER_NAMESPACE::formats::parse::To<Enum::Foo>) {
return k__ns__Enum__Foo_Mapping.TryFindBySecond(value).has_value();
}

} // namespace ns

fmt::format_context::iterator fmt::formatter<ns::Enum::Foo>::format(const ns::Enum::Foo& value,
Expand Down
2 changes: 2 additions & 0 deletions chaotic/golden_tests/output/schemas/enum/enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ USERVER_NAMESPACE::formats::json::Value Serialize(

std::string ToString(Enum::Foo value);

bool IsConvertible(std::string_view value, USERVER_NAMESPACE::formats::parse::To<Enum::Foo>);

} // namespace ns

template <>
Expand Down
5 changes: 5 additions & 0 deletions chaotic/integration_tests/tests/render/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ TEST(Simple, StringEnum) {
EXPECT_EQ("bar", ToString(ns::StringEnum::kBar));
EXPECT_EQ("some!thing", ToString(ns::StringEnum::kSomeThing));

EXPECT_TRUE(IsConvertible("foo", formats::parse::To<ns::StringEnum>{}));
EXPECT_TRUE(IsConvertible("bar", formats::parse::To<ns::StringEnum>{}));
EXPECT_TRUE(IsConvertible("some!thing", formats::parse::To<ns::StringEnum>{}));
EXPECT_FALSE(IsConvertible("invalid", formats::parse::To<ns::StringEnum>{}));

EXPECT_EQ(FromString("foo", formats::parse::To<ns::StringEnum>{}), ns::StringEnum::kFoo);
UEXPECT_THROW_MSG(
FromString("zoo", formats::parse::To<ns::StringEnum>{}),
Expand Down
Loading