Skip to content

Commit

Permalink
[jdwp] fix names of fields containing spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
michalgr committed Dec 6, 2023
1 parent 1415727 commit 560c892
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 12 additions & 4 deletions projects/jdwp/codegen/dataclass_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ def __get_python_type_for(self, struct: Struct, field: Field) -> str:
def __is_explicit_field(self, field: Field) -> bool:
return not isinstance(field.type, (ArrayLength, UnionTag))

def __get_field_name(self, field: Field) -> str:
words = field.name.split(" ")
words = [words[0]] + [word.capitalize() for word in words[1:]]
return "".join(words)

def __generate_dataclass(self, struct: Struct) -> str:
name = self.__struct_to_name[struct]
fields_def = "\n".join(
f" {field.name}: {self.__get_python_type_for(struct, field)}"
f" {self.__get_field_name(field)}: {self.__get_python_type_for(struct, field)}"
for field in struct.fields
if self.__is_explicit_field(field)
)
Expand Down Expand Up @@ -88,17 +93,20 @@ def nested_structs(root: Struct) -> typing.Generator[StructLink, None, None]:
def compute_struct_names(root: Struct, name: str) -> typing.Mapping[Struct, str]:
names = {root: name}
for parent, field, nested in nested_structs(root):
sanitized_field_name = "".join(
word.capitalize() for word in field.name.split(" ")
)
type = field.type
match type:
case Struct():
names[nested] = f"{names[parent]}{field.name.capitalize()}"
names[nested] = f"{names[parent]}{sanitized_field_name}"
case Array():
names[nested] = f"{names[parent]}{field.name.capitalize()}Element"
names[nested] = f"{names[parent]}{sanitized_field_name}Element"
case TaggedUnion():
tagged_union_type = typing.cast(TaggedUnion, type)
for case_value, case_struct in tagged_union_type.cases:
case_name = format_enum_name(case_value)
names[
case_struct
] = f"{names[parent]}{field.name.capitalize()}Case{case_name}"
] = f"{names[parent]}{sanitized_field_name}Case{case_name}"
return names
6 changes: 3 additions & 3 deletions projects/jdwp/tests/test_dataclass_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_struct_in_array(self):
fields=[
array_length,
Field(
name="array",
name="array of elements",
type=Array(
element_type=element_struct,
length=array_length,
Expand All @@ -94,11 +94,11 @@ def test_struct_in_array(self):

expected = [
"@dataclasses.dataclass(frozen=True)\n"
"class ArrayStructArrayElement:\n"
"class ArrayStructArrayOfElementsElement:\n"
" element_field: int",
"@dataclasses.dataclass(frozen=True)\n"
"class ArrayStruct:\n"
" array: typing.List[ArrayStructArrayElement]",
" arrayOfElements: typing.List[ArrayStructArrayOfElementsElement]",
]

self.assertEqual(result, expected)

0 comments on commit 560c892

Please sign in to comment.