Skip to content

Commit 490216e

Browse files
committed
fix: when enum member names are derived and there is only one such member, derive the stripped prefix from the enum name
1 parent 1cb6e15 commit 490216e

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

‎src/codegen/run.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,14 @@ def process_enum(fp: TextIO, spec) -> Optional[str]: # noqa: C901
171171

172172
entries = [entry.strip() for entry in spec.split(",")]
173173
entries = [entry for entry in entries if entry]
174-
plen = longest_common_prefix_length(entries)
174+
if len(entries) == 1:
175+
# No common prefix to infer from a single entry; strip the longest
176+
# prefix derived from the name of the enum type instead
177+
words = orig_name.upper().split("_")[:-1]
178+
prefixes = ("_".join(words[:i]) + "_" for i in range(len(words), 1, -1))
179+
plen = next((len(p) for p in prefixes if entries[0].startswith(p)), 0)
180+
else:
181+
plen = longest_common_prefix_length(entries)
175182
entries = [entry[plen:] for entry in entries]
176183

177184
remapped_name = ENUM_NAME_REMAPPING.get(name, name)

0 commit comments

Comments
 (0)