Skip to content

Commit 36a1402

Browse files
authored
Fix issue that occurs with naming when proto is double nested (#21)
1 parent 04a2fcd commit 36a1402

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

betterproto/plugin.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,19 @@ def get_py_zero(type_num: int) -> str:
122122

123123

124124
def traverse(proto_file):
125-
def _traverse(path, items):
125+
def _traverse(path, items, prefix = ''):
126126
for i, item in enumerate(items):
127+
# Adjust the name since we flatten the heirarchy.
128+
item.name = next_prefix = prefix + item.name
127129
yield item, path + [i]
128130

129131
if isinstance(item, DescriptorProto):
130132
for enum in item.enum_type:
131-
enum.name = item.name + enum.name
133+
enum.name = next_prefix + enum.name
132134
yield enum, path + [i, 4]
133135

134136
if item.nested_type:
135-
for n, p in _traverse(path + [i, 3], item.nested_type):
136-
# Adjust the name since we flatten the heirarchy.
137-
n.name = item.name + n.name
137+
for n, p in _traverse(path + [i, 3], item.nested_type, next_prefix):
138138
yield n, p
139139

140140
return itertools.chain(

betterproto/tests/nestedtwice.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"root": {
3+
"name": "double-nested",
4+
"parent": {
5+
"child": [{"foo": "hello"}],
6+
"enumChild": ["A"],
7+
"rootParentChild": [{"a": "hello"}],
8+
"bar": true
9+
}
10+
}
11+
}

betterproto/tests/nestedtwice.proto

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
syntax = "proto3";
2+
3+
message Test {
4+
message Root {
5+
message Parent {
6+
message RootParentChild {
7+
string a = 1;
8+
}
9+
enum EnumChild{
10+
A = 0;
11+
B = 1;
12+
}
13+
message Child {
14+
string foo = 1;
15+
}
16+
reserved 1;
17+
repeated Child child = 2;
18+
repeated EnumChild enumChild=3;
19+
repeated RootParentChild rootParentChild=4;
20+
bool bar = 5;
21+
}
22+
string name = 1;
23+
Parent parent = 2;
24+
}
25+
Root root = 1;
26+
}

0 commit comments

Comments
 (0)