(dict_def: Dict[str, Any], dict_dir: Path)
| 109 | |
| 110 | |
| 111 | def compile_dict(dict_def: Dict[str, Any], dict_dir: Path) -> Dict[str, Any]: |
| 112 | if dict_def["type"] == "group": |
| 113 | compiled_children = [compile_dict(sub_def, dict_dir) for sub_def in dict_def["dicts"]] |
| 114 | match_policy = dict_def.get("match_policy", "short_circuit") |
| 115 | |
| 116 | if match_policy == "union": |
| 117 | # Union semantics: longest prefix wins regardless of child order. |
| 118 | # A flat merged inline dict has the same semantics because LeafMatcher |
| 119 | # always returns the longest match; first-occurrence wins for same key. |
| 120 | # Nested union groups are also absorbed since union(union(...)) = union(...). |
| 121 | if all(is_union_mergeable(child) for child in compiled_children): |
| 122 | merged: Dict[str, str] = {} |
| 123 | for child in compiled_children: |
| 124 | collect_union_entries(child, merged) |
| 125 | return {"type": "inline", "entries": sort_dict_keys(merged)} |
| 126 | |
| 127 | result: Dict[str, Any] = {"type": "group"} |
| 128 | if "match_policy" in dict_def: |
| 129 | result["match_policy"] = dict_def["match_policy"] |
| 130 | result["dicts"] = compiled_children |
| 131 | return result |
| 132 | |
| 133 | if dict_def["type"] == "inline": |
| 134 | entries = dict_def["entries"] |
| 135 | else: |
| 136 | entries = inline_entries_for_file_dict(dict_def, dict_dir) |
| 137 | |
| 138 | return { |
| 139 | "type": "inline", |
| 140 | "entries": sort_dict_keys(entries), |
| 141 | } |
| 142 | |
| 143 | def sort_dict_keys(d: Dict[str, str]) -> Dict[str, str]: |
| 144 | return {k: d[k] for k in sorted(d.keys())} |
no test coverage detected