From 62da35b3eaae7ad31ef41411d9814e66d13862d1 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Sat, 12 Mar 2022 10:08:03 +0100 Subject: [PATCH] parser: ensure prefix is separated when traversing (#353) --- src/betterproto/plugin/parser.py | 4 ++-- .../casing_inner_class/casing_inner_class.proto | 11 +++++++++++ .../casing_inner_class/test_casing_inner_class.py | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 tests/inputs/casing_inner_class/casing_inner_class.proto create mode 100644 tests/inputs/casing_inner_class/test_casing_inner_class.py diff --git a/src/betterproto/plugin/parser.py b/src/betterproto/plugin/parser.py index 21a2caf..85332d2 100644 --- a/src/betterproto/plugin/parser.py +++ b/src/betterproto/plugin/parser.py @@ -44,12 +44,12 @@ def traverse( for i, item in enumerate(items): # Adjust the name since we flatten the hierarchy. # Todo: don't change the name, but include full name in returned tuple - item.name = next_prefix = prefix + item.name + item.name = next_prefix = f"{prefix}_{item.name}" yield item, path + [i] if isinstance(item, DescriptorProto): for enum in item.enum_type: - enum.name = next_prefix + enum.name + enum.name = f"{next_prefix}_{enum.name}" yield enum, path + [i, 4] if item.nested_type: diff --git a/tests/inputs/casing_inner_class/casing_inner_class.proto b/tests/inputs/casing_inner_class/casing_inner_class.proto new file mode 100644 index 0000000..7d231be --- /dev/null +++ b/tests/inputs/casing_inner_class/casing_inner_class.proto @@ -0,0 +1,11 @@ +// https://github.com/danielgtaylor/python-betterproto/issues/344 +syntax = "proto3"; + +package casing_inner_class; + +message Test { + message inner_class { + sint32 old_exp = 1; + } + inner_class inner = 2; +} \ No newline at end of file diff --git a/tests/inputs/casing_inner_class/test_casing_inner_class.py b/tests/inputs/casing_inner_class/test_casing_inner_class.py new file mode 100644 index 0000000..267b105 --- /dev/null +++ b/tests/inputs/casing_inner_class/test_casing_inner_class.py @@ -0,0 +1,14 @@ +import tests.output_betterproto.casing_inner_class as casing_inner_class + + +def test_message_casing_inner_class_name(): + assert hasattr( + casing_inner_class, "TestInnerClass" + ), "Inline defined Message is correctly converted to CamelCase" + + +def test_message_casing_inner_class_attributes(): + message = casing_inner_class.Test() + assert hasattr( + message.inner, "old_exp" + ), "Inline defined Message attribute is snake_case"