Skip to content

Commit 9f60aa1

Browse files
authored
Merge pull request #72 from hit9/D-Walther-test-fuzzy-type-match
merging #70
2 parents e66308e + 1d7e94c commit 9f60aa1

File tree

6 files changed

+102
-20
lines changed

6 files changed

+102
-20
lines changed

changes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ Version 1.2.1 (Pending)
55

66
.. _version-1.2.1:
77

8+
Warning: May break some existing projects's generated names:
9+
810
- Bugfix: `pascal_case` formatter. ISSUE #68, PR #69.
11+
- Bugfix: Fixed naming style of generated code such as IntegerConstant (style lookup supports inheritance). ISSUE #70 PR #70
912

1013

1114
Version 1.2.0

compiler/bitproto/renderer/formatter.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
from typing import (
1212
Callable,
1313
Dict,
14+
Iterator,
1415
List,
16+
Mapping,
1517
Optional,
1618
Tuple,
1719
Type as T,
@@ -59,8 +61,33 @@
5961

6062
CaseStyleConverter = Callable[[str], str]
6163

62-
# Dict[DefinitionType => One or tuple of CaseStyle name OR CaseStyleConverter]
63-
CaseStyleMapping = Dict[T[Definition], Union[str, Tuple[str, ...], CaseStyleConverter]]
64+
65+
class CaseStyleMapping(Mapping):
66+
"""Maps DefinitionType's to one or tuple of CaseStyle-name OR CaseStyleConverter, while accounting for inheritance.
67+
68+
>>> CaseStyleMapping({Base: "upper"}).get(Derived, "keep") == "upper"
69+
"""
70+
71+
def __init__(
72+
self,
73+
entries: Dict[T[Definition], Union[str, Tuple[str, ...], CaseStyleConverter]],
74+
):
75+
self._dict = entries
76+
77+
def __getitem__(
78+
self, key: T[Definition]
79+
) -> Union[str, Tuple[str, ...], CaseStyleConverter]:
80+
for t in key.mro():
81+
value = self._dict.get(t, None)
82+
if value is not None:
83+
return value
84+
raise KeyError() # @IgnoreException
85+
86+
def __iter__(self) -> Iterator[T[Definition]]:
87+
return self._dict.__iter__()
88+
89+
def __len__(self) -> int:
90+
return len(self._dict)
6491

6592

6693
@unique

compiler/bitproto/renderer/impls/c/formatter.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ class CFormatter(Formatter):
3434

3535
@override(Formatter)
3636
def case_style_mapping(self) -> CaseStyleMapping:
37-
return {
38-
Constant: "upper",
39-
Alias: "pascal",
40-
Enum: "pascal",
41-
EnumField: ("snake", "upper"),
42-
Message: "pascal",
43-
}
37+
return CaseStyleMapping(
38+
{
39+
Constant: "upper",
40+
Alias: "pascal",
41+
Enum: "pascal",
42+
EnumField: ("snake", "upper"),
43+
Message: "pascal",
44+
}
45+
)
4446

4547
@override(Formatter)
4648
def indent_character(self) -> str:

compiler/bitproto/renderer/impls/go/formatter.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ class GoFormatter(Formatter):
3434

3535
@override(Formatter)
3636
def case_style_mapping(self) -> CaseStyleMapping:
37-
return {
38-
Alias: "pascal",
39-
Constant: "upper",
40-
EnumField: ("snake", "upper"),
41-
Message: "pascal",
42-
MessageField: "pascal",
43-
}
37+
return CaseStyleMapping(
38+
{
39+
Alias: "pascal",
40+
Constant: "upper",
41+
EnumField: ("snake", "upper"),
42+
Message: "pascal",
43+
MessageField: "pascal",
44+
}
45+
)
4446

4547
@override(Formatter)
4648
def indent_character(self) -> str:

compiler/bitproto/renderer/impls/py/formatter.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ class PyFormatter(Formatter):
2828

2929
@override(Formatter)
3030
def case_style_mapping(self) -> CaseStyleMapping:
31-
return {
32-
Constant: "upper",
33-
EnumField: ("snake", "upper"),
34-
}
31+
return CaseStyleMapping(
32+
{
33+
Constant: "upper",
34+
EnumField: ("snake", "upper"),
35+
}
36+
)
3537

3638
@override(Formatter)
3739
def indent_character(self) -> str:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from bitproto._ast import Constant, IntegerConstant, Alias, Enum, EnumField, Message
2+
from bitproto.renderer.formatter import CaseStyleMapping
3+
from bitproto.parser import parse_string
4+
from bitproto.renderer.impls import renderer_registry
5+
6+
7+
def test_case_style_mapping():
8+
d = CaseStyleMapping(
9+
{
10+
Constant: "upper",
11+
Alias: "pascal",
12+
Enum: "pascal",
13+
EnumField: ("snake", "upper"),
14+
Message: "pascal",
15+
}
16+
)
17+
18+
assert d[Constant] == "upper"
19+
assert d[IntegerConstant] == "upper"
20+
assert d[Message] == "pascal"
21+
22+
23+
def test_issue_70():
24+
proto_string = """
25+
proto example
26+
option c.name_prefix = "lowercase"
27+
const my_const = 42;
28+
"""
29+
proto = parse_string(proto_string)
30+
31+
for lang, expect in [
32+
("c", "LOWERCASEMY_CONST"),
33+
("go", "MY_CONST"), # before this fix: my_const
34+
("py", "MY_CONST"), # before this fix: my_const
35+
]:
36+
render_class_s = renderer_registry[lang]
37+
38+
found = False
39+
for render_class in render_class_s:
40+
renderer = render_class(proto)
41+
out_string = renderer.render_string()
42+
if expect in out_string:
43+
found = True
44+
break
45+
46+
assert found

0 commit comments

Comments
 (0)