From 560c892f926e3f7bbd114102fdc24abe5e3fc049 Mon Sep 17 00:00:00 2001 From: Michal Gregorczyk Date: Wed, 6 Dec 2023 13:23:49 +0100 Subject: [PATCH] [jdwp] fix names of fields containing spaces --- projects/jdwp/codegen/dataclass_generator.py | 16 ++++++++++++---- projects/jdwp/tests/test_dataclass_generator.py | 6 +++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/projects/jdwp/codegen/dataclass_generator.py b/projects/jdwp/codegen/dataclass_generator.py index 9fea9a1..12276ba 100644 --- a/projects/jdwp/codegen/dataclass_generator.py +++ b/projects/jdwp/codegen/dataclass_generator.py @@ -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) ) @@ -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 diff --git a/projects/jdwp/tests/test_dataclass_generator.py b/projects/jdwp/tests/test_dataclass_generator.py index aa72fec..d69edd9 100644 --- a/projects/jdwp/tests/test_dataclass_generator.py +++ b/projects/jdwp/tests/test_dataclass_generator.py @@ -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, @@ -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)