Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for x-enum-varnames in chaotic #712

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions chaotic/chaotic/back/cpp/templates/type.hpp.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
{% elif type.get_py_type() == 'CppIntEnum' %}
enum class {{ type.cpp_local_name() }} {
{%- for item in type.enums %}
k{{ item }} = {{ item }},
k{{ item.cpp_name }} = {{ item.value }},
{%- endfor -%}
};

static constexpr {{ type.cpp_local_name() }} k{{ type.cpp_local_name() }}Values [] = {
{%- for item in type.enums %}
{{ type.cpp_local_name() }}::k{{ item }},
{{ type.cpp_local_name() }}::k{{ item.cpp_name }},
{%- endfor -%}
};
{% elif type.get_py_type() == 'CppStringEnum' %}
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
27 changes: 25 additions & 2 deletions chaotic/chaotic/back/cpp/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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:
def __init__(self, types: List[cpp_types.CppType]) -> None:
Expand Down Expand Up @@ -262,13 +263,35 @@ def _gen_integer(
assert schema.format is None
assert user_cpp_type is None

enum_names = []

if hasattr(schema, 'x_properties') and 'x-enum-varnames' in schema.x_properties:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x_properties должны быть всегда

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

уберем

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 in range(len(schema.enum)):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enumerate(schema.enum)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

принял

raw_name = str(schema.enum[i])
if i < len(enum_names):
raw_name = enum_names[i]
segoon marked this conversation as resolved.
Show resolved Hide resolved
emum_items.append(cpp_types.CppIntEnumItem(value=schema.enum[i],
raw_name=raw_name,
cpp_name=to_camel_case(raw_name), ))
segoon marked this conversation as resolved.
Show resolved Hide resolved

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
11 changes: 10 additions & 1 deletion chaotic/chaotic/back/cpp/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,20 @@ def camel_case(string: str, no_lower_casing: bool = False) -> str:
class EnumItemName(str):
pass

@dataclasses.dataclass
class CppIntEnumItem:
value: int
segoon marked this conversation as resolved.
Show resolved Hide resolved
raw_name: str
segoon marked this conversation as resolved.
Show resolved Hide resolved
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
Loading