Skip to content

Commit 25c784b

Browse files
authored
Merge pull request #53 from hit9/fix-issue-52
Fix bug: invalid generation of enum's field name importing from another bitproto
2 parents 5bf13d5 + 94529e6 commit 25c784b

File tree

21 files changed

+162
-20
lines changed

21 files changed

+162
-20
lines changed

.github/workflows/bench.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
strategy:
1818
matrix:
19-
os: [ubuntu-20.04, ubuntu-18.04, macos-latest]
19+
os: [ubuntu-22.04, macos-latest]
2020
python: ["3.11"]
2121
go: ["1.19"]
2222

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
strategy:
1818
matrix:
19-
os: [ubuntu-18.04]
19+
os: [ubuntu-22.04]
2020
python: [3.7, 3.8, "3.10", "3.11"]
2121
go: ["1.19", "1.16"]
2222

changes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
.. currentmodule:: bitproto
22

3+
Version 1.1.1
4+
-------------
5+
6+
.. _version-1.1.1
7+
8+
- Fix bug: enum importting other bitproto's field name generation bug. #53 #52
9+
- Fix bug: import statements of bitproto importings should be placed ahead of other declarations. #53
10+
311
Version 1.1.0
412
-------------
513

compiler/bitproto/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
55
Bit level data interchange format.
66
7-
:copyright: (c) 2020~2022 by Chao Wang <[email protected]>.
7+
:copyright: (c) 2020~2023 by Chao Wang <[email protected]>.
88
99
"""
1010

11-
__version__ = "1.1.0"
11+
__version__ = "1.1.1"
1212
__description__ = "bit level data interchange format."

compiler/bitproto/linter.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def lint(self, proto: Proto) -> int:
8080
"""
8181
warning_count: int = 0
8282
for definition_type in SUPPORTED_TYPES:
83-
8483
items: List[Tuple[str, Definition]]
8584
if definition_type is Proto: # proto is not a member of itself.
8685
items = [(proto.name, proto)]

compiler/bitproto/renderer/formatter.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464

6565
@unique
6666
class CaseStyle(Enum_):
67-
6867
KEEP = 0
6968
SNAKE = 1
7069
UPPER = 2
@@ -401,17 +400,23 @@ def format_definition_name(
401400
Joins a with a dot delimer if given definition is a definition imported.
402401
"""
403402
definition_name = self.format_definition_name_inner_proto(d, class_)
404-
protos = [scope for scope in d.scope_stack if isinstance(scope, Proto)]
405403

406-
if len(protos) <= 1:
404+
parent = d.scope_stack[-1]
405+
406+
if not isinstance(parent, Proto):
407+
# Member of Non-Proto scopes: message, enum etc.
407408
return definition_name
408409

409410
if not self.support_import_as_member():
410411
return definition_name
411412

412-
proto = protos[-1] # Last is the imported parent.
413-
items = [self._get_definition_name(proto), definition_name]
414-
return self.delimer_cross_proto().join(items)
413+
if not parent.scope_stack:
414+
# `parent` is the top proto.
415+
return definition_name
416+
# `parent` is imported in another proto.
417+
return self.delimer_cross_proto().join(
418+
[self._get_definition_name(parent), definition_name]
419+
)
415420

416421
@final
417422
def format_name_related_to_definition(

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ def render(self) -> None:
5454
self.push(f'bp "{GO_LIB_IMPORT_PATH}"', indent=1)
5555
self.push(f")")
5656

57-
self.push_empty_line()
58-
59-
self.push_comment("Avoid possible golang import not used error")
60-
self.push(f"var formatInt = strconv.FormatInt")
61-
self.push(f"var jsonMarshal = json.Marshal")
62-
self.push(f"var _ = bp.Useless")
63-
6457

6558
class BlockImportChildProto(BlockBindProto[F]):
6659
@override(Block)
@@ -81,6 +74,15 @@ def separator(self) -> str:
8174
return "\n"
8275

8376

77+
class BlockAvoidGeneralImportsNotUsed(Block):
78+
@override(Block)
79+
def render(self) -> None:
80+
self.push_comment("Avoid possible golang import not used error")
81+
self.push(f"var formatInt = strconv.FormatInt")
82+
self.push(f"var jsonMarshal = json.Marshal")
83+
self.push(f"var _ = bp.Useless")
84+
85+
8486
class BlockAliasMethodBase(BlockBindAlias[F]):
8587
@cached_property
8688
def method_receiver(self) -> str:
@@ -750,6 +752,7 @@ def blocks(self) -> List[Block[F]]:
750752
BlockPackageName(self.bound),
751753
BlockGeneralImports(),
752754
BlockImportChildProtoList(),
755+
BlockAvoidGeneralImportsNotUsed(),
753756
BlockBoundDefinitionList(),
754757
]
755758

compiler/bitproto/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ def __delattr__(self, name):
206206
setattr(class_, "__delattr__", __delattr__)
207207

208208
if post_init:
209-
210209
init_func = getattr(class_, "__init__")
211210

212211
def decorate_init(func):

lib/py/bitprotolib/bp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ class MessageBase(Accessor):
191191

192192
def to_dict(self) -> Dict[str, Any]:
193193
"""Converts this message to a dict."""
194-
return asdict(self, dict_factory=getattr(self, "dict_factory", dict))
194+
return asdict(
195+
self, dict_factory=getattr(self, "dict_factory", dict) # type:ignore
196+
)
195197

196198
def to_json(
197199
self, indent: Optional[int] = None, separators: Optional[Tuple[str, str]] = None
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
PY_SOURCE_FILE=main.py
2+
BIN=main
3+
4+
BP_LIB_DIR=../../../../../lib/c
5+
BP_LIC_C_PATH=$(BP_LIB_DIR)/bitproto.c
6+
7+
C_SOURCE_FILE=main.c
8+
C_SOURCE_FILE_LIST=$(C_SOURCE_FILE) a_bp.c b_bp.c $(BP_LIC_C_PATH)
9+
C_BIN=$(BIN)
10+
CC_OPTIMIZATION_ARG?=
11+
12+
GO_BIN=$(BIN)
13+
14+
15+
bp-c:
16+
@bitproto c a.bitproto c/
17+
@bitproto c b.bitproto c/
18+
19+
bp-py:
20+
@bitproto py a.bitproto py/
21+
@bitproto py b.bitproto py/
22+
23+
bp-go:
24+
@bitproto go a.bitproto go/bp/a $(OPTIMIZATION_MODE_ARGS)
25+
@bitproto go b.bitproto go/bp/b $(OPTIMIZATION_MODE_ARGS)
26+
27+
build-c: bp-c
28+
@cd c && $(CC) $(C_SOURCE_FILE_LIST) -I. -I$(BP_LIB_DIR) -o $(C_BIN) $(CC_OPTIMIZATION_ARG)
29+
30+
build-go: bp-go
31+
@cd go && go build -o $(GO_BIN)
32+
33+
build-py: bp-py
34+
35+
run-c: build-c
36+
@cd c && ./$(C_BIN)
37+
38+
run-go: build-go
39+
@cd go && ./$(GO_BIN)
40+
41+
run-py: build-py
42+
@cd py && python $(PY_SOURCE_FILE)
43+
44+
clean:
45+
@rm -fr c/$(C_BIN) go/$(GO_BIN) */*_bp.* */**/*_bp.* py/__pycache__
46+
47+
run: run-py run-c run-go
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
proto a
2+
3+
import bb "b.bitproto"
4+
5+
message A {
6+
bb.X x = 1;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
proto b;
2+
3+
option go.package_path = "github.com/hit9/bitproto/tests/test_encoding/encoding-cases/issue-52/go/bp/b";
4+
5+
enum X : uint1 {
6+
OK = 0;
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "a_bp.h"
2+
#include "b_bp.h"
3+
#include <assert.h>
4+
5+
int main(void) {
6+
struct A a = {.x = OK};
7+
assert(a.x == OK);
8+
return 0;
9+
}

tests/test_encoding/encoding-cases/issue-52/go/bp/a/.gitkeep

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/hit9/bitproto/tests/test_encoding/encoding-cases/issue-52/go/bp/a
2+
3+
go 1.15

tests/test_encoding/encoding-cases/issue-52/go/bp/b/.gitkeep

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module github.com/hit9/bitproto/tests/test_encoding/encoding-cases/issue-52/go/bp/b
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/hit9/bitproto/tests/test_encoding/encoding-cases/issue-52
2+
3+
replace github.com/hit9/bitproto/lib/go => ../../../../../lib/go
4+
5+
replace github.com/hit9/bitproto/tests/test_encoding/encoding-cases/issue-52/go/bp/a => ./bp/a
6+
replace github.com/hit9/bitproto/tests/test_encoding/encoding-cases/issue-52/go/bp/b => ./bp/b
7+
8+
go 1.15
9+
10+
require (
11+
github.com/hit9/bitproto/lib/go v0.0.0-00010101000000-000000000000 // indirect
12+
github.com/hit9/bitproto/tests/test_encoding/encoding-cases/issue-52/go/bp/a v0.0.0-00010101000000-000000000000
13+
github.com/hit9/bitproto/tests/test_encoding/encoding-cases/issue-52/go/bp/b v0.0.0-00010101000000-000000000000
14+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
a "github.com/hit9/bitproto/tests/test_encoding/encoding-cases/issue-52/go/bp/a"
5+
b "github.com/hit9/bitproto/tests/test_encoding/encoding-cases/issue-52/go/bp/b"
6+
)
7+
8+
func assert(condition bool) {
9+
if !condition {
10+
panic("assertion failed")
11+
}
12+
}
13+
14+
func main() {
15+
v := a.A{X: b.OK}
16+
assert(v.X == b.OK)
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import a_bp
2+
import b_bp
3+
4+
5+
def main():
6+
a = a_bp.A()
7+
assert a.x == b_bp.X.OK
8+
9+
10+
if __name__ == "__main__":
11+
main()

tests/test_encoding/test_encoding.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,13 @@ def test_encoding_signed() -> None:
135135

136136
def test_encoding_complexx() -> None:
137137
_TestCase("complexx", support_optimization_mode=False).run()
138+
139+
140+
def test_encoding_issue52() -> None:
141+
_TestCase(
142+
"issue-52",
143+
langs=["py", "c"],
144+
compare_output=False,
145+
compare_output_as_json=False,
146+
support_optimization_mode=False,
147+
)

0 commit comments

Comments
 (0)