Skip to content

Commit 4c8887d

Browse files
committed
refactor(protobuf): single-pass enum emit, use Datatypes.STRING[0]
- Collapse two-pass loop into one: _write_nested_enum is called immediately before the field declaration, removing the redundant pre-iteration. - Replace "string" literal with Datatypes.STRING[0] from datatypes module. - Update expected.proto: enum definitions now appear inline before their respective fields rather than grouped at the top of the message. Co-Authored-By: Claude and aki1770-del <aki1770@gmail.com>
1 parent 2b6ca8b commit 4c8887d

File tree

2 files changed

+4
-11
lines changed

2 files changed

+4
-11
lines changed

src/vss_tools/exporters/protobuf.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
VSSDataDatatype,
2626
VSSDataStruct,
2727
)
28+
from vss_tools.datatypes import Datatypes
2829
from vss_tools.tree import VSSNode
2930

3031
PATH_DELIMITER = "."
@@ -183,21 +184,13 @@ def _write_nested_enum(fd: TextIOWrapper, field_name: str, allowed: list, indent
183184
def print_messages(
184185
nodes: tuple[VSSNode], fd: TextIOWrapper, static_uid: bool, add_optional: bool, include_comments: bool
185186
):
186-
# Pass 1: write nested enum definitions for every string field with allowed values.
187-
for node in nodes:
188-
if isinstance(node.data, VSSDataDatatype):
189-
base = node.data.datatype.strip("[]")
190-
if base == "string" and node.data.allowed:
191-
_write_nested_enum(fd, node.name, node.data.allowed)
192-
193-
# Pass 2: write field declarations.
194187
usedKeys: dict[int, str] = {}
195188
for i, node in enumerate(nodes, 1):
196189
if isinstance(node.data, VSSDataDatatype):
197190
dt_val = node.data.datatype
198191
base = dt_val.strip("[]")
199-
if base == "string" and node.data.allowed:
200-
# Replace plain string with the nested enum type.
192+
if base == Datatypes.STRING[0] and node.data.allowed:
193+
_write_nested_enum(fd, node.name, node.data.allowed)
201194
data_type = _enum_type_name(node.name)
202195
else:
203196
data_type = mapped.get(base, base)

tests/vspec/test_protobuf_enum_allowed/expected.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ message A {
88
FM = 2;
99
BLUETOOTH = 3;
1010
}
11+
SourceEnum Source = 1;
1112
enum SurfaceEnum {
1213
DRY = 0;
1314
WET = 1;
1415
SNOW = 2;
1516
ICE = 3;
1617
}
17-
SourceEnum Source = 1;
1818
repeated SurfaceEnum Surface = 2;
1919
float Speed = 3;
2020
}

0 commit comments

Comments
 (0)