Skip to content

Commit

Permalink
feat chaotic: add tests for integer enum
Browse files Browse the repository at this point in the history
support for x-enum-varnames in chaotic

Tests: протестировано CI

Pull Request resolved: #712
commit_hash:cf82a62367ac245be2fe89babe7f2fdd3b8597d4
  • Loading branch information
serlex777 authored and aabagdasaryan committed Feb 14, 2025
1 parent 8a98bed commit bb5f707
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
8 changes: 4 additions & 4 deletions chaotic/chaotic/back/cpp/templates/type.hpp.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@
{% elif type.get_py_type() == 'CppIntEnum' %}
enum class {{ type.cpp_local_name() }} {
{%- for item in type.enums %}
k{{ item }} = {{ item }},
{%- endfor -%}
k{{ item.cpp_name }} = {{ item.value }}, // {{ item.raw_name }}
{%- endfor %}
};

static constexpr {{ type.cpp_local_name() }} k{{ type.cpp_local_name() }}Values [] = {
{%- for item in type.enums %}
{{ type.cpp_local_name() }}::k{{ item }},
{%- endfor -%}
{{ type.cpp_local_name() }}::k{{ item.cpp_name }},
{%- endfor %}
};
{% elif type.get_py_type() == 'CppStringEnum' %}
enum class {{ type.cpp_local_name() }} {
Expand Down
2 changes: 1 addition & 1 deletion chaotic/chaotic/back/cpp/templates/type_parsers.ipp.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
[](auto selector) {
return selector().template Type<{{ type.cpp_global_name() }}, int>()
{%- for item in type.enums %}
.Case({{ type.cpp_global_name() }}::k{{ item }}, {{ item }})
.Case({{ type.cpp_global_name() }}::k{{ item.cpp_name }}, {{ item.value }})
{%- endfor -%}
;
};
Expand Down
30 changes: 29 additions & 1 deletion chaotic/chaotic/back/cpp/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class GeneratorState:


NON_NAME_SYMBOL_RE = re.compile('[^_0-9a-zA-Z]')
SPLIT_RE = re.compile(r'[a-zA-Z0-9]+')
SPLIT_WORDS_RE = re.compile(r'[A-Z]+(?=[A-Z][a-z0-9])|[A-Z][a-z0-9]+|[a-z0-9]+|[A-Z]+')


class FormatChooser:
Expand Down Expand Up @@ -270,13 +272,39 @@ def _gen_integer(
assert schema.format is None
assert user_cpp_type is None

enum_names = []

if 'x-enum-varnames' in schema.x_properties:
enum_names = schema.x_properties['x-enum-varnames']

emum_items: List[cpp_types.CppIntEnumItem] = []

def to_camel_case(text: str) -> str:
words = SPLIT_RE.findall(text)
result = []
for word in words:
result.extend([part.capitalize() for part in SPLIT_WORDS_RE.findall(word)])
return ''.join(result)

for i, val in enumerate(schema.enum):
raw_name = str(val)
if i < len(enum_names):
raw_name = enum_names[i]
emum_items.append(
cpp_types.CppIntEnumItem(
value=val,
raw_name=raw_name,
cpp_name=to_camel_case(raw_name),
)
)

return cpp_types.CppIntEnum(
json_schema=schema,
nullable=schema.nullable,
raw_cpp_type=name,
user_cpp_type=None,
name=name.in_global_scope(),
enums=schema.enum,
enums=emum_items,
)

if schema.format is None:
Expand Down
12 changes: 11 additions & 1 deletion chaotic/chaotic/back/cpp/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,20 @@ class EnumItemName(str):
pass


@dataclasses.dataclass
class CppIntEnumItem:
value: int
raw_name: str
cpp_name: str

def definition_includes(self) -> List[str]:
return ['fmt/format.h']


@dataclasses.dataclass
class CppIntEnum(CppType):
name: str
enums: List[int]
enums: List[CppIntEnumItem]

__hash__ = CppType.__hash__

Expand Down
15 changes: 13 additions & 2 deletions chaotic/tests/back/cpp/test_tr_int.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from chaotic import error
from chaotic.back.cpp import type_name
from chaotic.back.cpp.types import CppIntEnum
from chaotic.back.cpp.types import CppIntEnumItem
from chaotic.back.cpp.types import CppPrimitiveType
from chaotic.back.cpp.types import CppPrimitiveValidator

Expand Down Expand Up @@ -190,14 +191,24 @@ def test_int_format_int64(simple_gen):


def test_int_enum(simple_gen):
types = simple_gen({'type': 'integer', 'enum': [1, 2, 3]})
types = simple_gen({
'type': 'integer',
'enum': [0, 1, 2, 3, 5],
'x-enum-varnames': ['CamelCase', 'snake_case', 'UPPER', 'lower'],
})
assert types == {
'/definitions/type': CppIntEnum(
raw_cpp_type=type_name.TypeName('/definitions/type'),
user_cpp_type=None,
name='/definitions/type',
json_schema=None,
nullable=False,
enums=[1, 2, 3],
enums=[
CppIntEnumItem(value=0, raw_name='CamelCase', cpp_name='CamelCase'),
CppIntEnumItem(value=1, raw_name='snake_case', cpp_name='SnakeCase'),
CppIntEnumItem(value=2, raw_name='UPPER', cpp_name='Upper'),
CppIntEnumItem(value=3, raw_name='lower', cpp_name='Lower'),
CppIntEnumItem(value=5, raw_name='5', cpp_name='5'),
],
),
}

0 comments on commit bb5f707

Please sign in to comment.