Skip to content

Commit 88f29b7

Browse files
committed
Simple choice evaluation
1 parent 38718bc commit 88f29b7

2 files changed

Lines changed: 41 additions & 24 deletions

File tree

test/test-config-enum.cfgdb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@
100100
"green",
101101
"blue"
102102
]
103+
},
104+
"TestMode": {
105+
"type": "string",
106+
"ctype": "Color",
107+
"@enum": {
108+
"SMING_RELEASE": [
109+
"silent",
110+
"error"
111+
],
112+
"1": [
113+
"silent",
114+
"error",
115+
"debug",
116+
"info"
117+
]
118+
}
103119
}
104120
}
105121
}

tools/dbgen.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,11 @@ def make_static_initializer(entries: list, term_str: str = '') -> list:
661661

662662
def load_schema(filename: str) -> Database:
663663
def evaluate(expr: Any) -> Any:
664+
if isinstance(expr, dict):
665+
for k, v in expr.items():
666+
if evaluate(k):
667+
return v
668+
raise ValueError("No choice match")
664669
if isinstance(expr, list):
665670
return [evaluate(v) for v in expr]
666671
if isinstance(expr, str):
@@ -671,18 +676,26 @@ def evaluate(expr: Any) -> Any:
671676

672677
def calculate_props(props: dict, path: str):
673678
new_props = {}
679+
keys_by_id = {}
674680
for key, value in props.items():
675-
if not key.startswith('@'):
676-
new_props[key] = value
677-
continue
678-
new_key = key[1:]
679-
new_path = f'{path}/{new_key}'
680-
try:
681-
new_value = evaluate(value)
682-
except Exception as e:
683-
raise ValueError(f'{new_path} in "{filename}"') from e
684-
new_props[new_key] = new_value
685-
calc_props[new_path] = new_value
681+
if key.startswith('@'):
682+
new_key = key[1:]
683+
new_path = f'{path}/{new_key}'
684+
try:
685+
new_value = evaluate(value)
686+
except Exception as e:
687+
raise ValueError(f'{new_path} in "{filename}"') from e
688+
calc_props[new_path] = new_value
689+
key = new_key
690+
value = new_value
691+
new_props[key] = value
692+
id = make_identifier(key)
693+
if not id:
694+
raise ValueError(f'Invalid key "{key}"')
695+
key_conflict = keys_by_id.get(id)
696+
if key_conflict:
697+
raise ValueError(f'Key "{key}" conflicts with "{key_conflict}"')
698+
keys_by_id[id] = key
686699
props.clear()
687700
props.update(new_props)
688701
for k, v in props.items():
@@ -692,20 +705,8 @@ def calculate_props(props: dict, path: str):
692705

693706
'''Load JSON configuration schema and validate
694707
'''
695-
def parse_object_pairs(pairs):
696-
d = {}
697-
identifiers = set()
698-
for k, v in pairs:
699-
id = make_identifier(k.removeprefix('@'))
700-
if not id:
701-
raise ValueError(f'Invalid key "{k}"')
702-
if id in identifiers:
703-
raise ValueError(f'Key "{k}" produces duplicate identifier "{id}"')
704-
identifiers.add(id)
705-
d[k] = v
706-
return d
707708
with open(filename, 'r', encoding='utf-8') as f:
708-
schema = json.load(f, object_pairs_hook=parse_object_pairs)
709+
schema = json.load(f)
709710

710711
calculate_props(schema, '')
711712

0 commit comments

Comments
 (0)