|
| 1 | +"""Validation and merge helpers for enforced registry init params.""" |
| 2 | + |
| 3 | +import copy |
| 4 | +from typing import Any, Callable, Dict, Optional |
| 5 | + |
| 6 | + |
| 7 | +def merge_init_param_enforce(config: Any, init_enforce: Any) -> Dict[str, Any]: |
| 8 | + """Merge runtime config with enforced registry params, letting registry win. |
| 9 | +
|
| 10 | + Runtime config may still provide fields not owned by the device model, but |
| 11 | + every field present in ``init_param_enforce`` is applied last and therefore |
| 12 | + cannot be overridden by instance-level config. |
| 13 | + """ |
| 14 | + base = copy.deepcopy(config) if isinstance(config, dict) else {} |
| 15 | + if not isinstance(init_enforce, dict): |
| 16 | + return base |
| 17 | + return _merge_dict_with_enforce(base, init_enforce) |
| 18 | + |
| 19 | + |
| 20 | +def _merge_dict_with_enforce(config: Dict[str, Any], init_enforce: Dict[str, Any]) -> Dict[str, Any]: |
| 21 | + merged = copy.deepcopy(config) |
| 22 | + for key, enforced_value in init_enforce.items(): |
| 23 | + current_value = merged.get(key) |
| 24 | + if isinstance(current_value, dict) and isinstance(enforced_value, dict): |
| 25 | + merged[key] = _merge_dict_with_enforce(current_value, enforced_value) |
| 26 | + else: |
| 27 | + merged[key] = copy.deepcopy(enforced_value) |
| 28 | + return merged |
| 29 | + |
| 30 | + |
| 31 | +def validate_init_param_enforce( |
| 32 | + device_id: str, |
| 33 | + init_schema: Optional[Dict[str, Any]], |
| 34 | + init_enforce: Any, |
| 35 | + error_factory: Callable[[str], Exception] = ValueError, |
| 36 | +) -> None: |
| 37 | + """Validate the JSON enforced config paired with ``init_param_schema``. |
| 38 | +
|
| 39 | + ``init_param_enforce`` is a plain JSON config object. It must not reintroduce |
| 40 | + the old ``class.init`` object factory DSL; drivers should construct rich |
| 41 | + objects from JSON-friendly type strings and params inside ``__init__``. |
| 42 | + """ |
| 43 | + if not isinstance(init_schema, dict): |
| 44 | + return |
| 45 | + |
| 46 | + config_schema = init_schema.get("config") |
| 47 | + if not isinstance(config_schema, dict): |
| 48 | + return |
| 49 | + |
| 50 | + required = config_schema.get("required") |
| 51 | + required_fields = [str(field) for field in required] if isinstance(required, list) else [] |
| 52 | + properties = config_schema.get("properties") |
| 53 | + has_config_contract = bool(required_fields) or ( |
| 54 | + isinstance(properties, dict) and bool(properties) |
| 55 | + ) |
| 56 | + if not has_config_contract: |
| 57 | + return |
| 58 | + |
| 59 | + if not isinstance(init_enforce, dict): |
| 60 | + raise error_factory( |
| 61 | + f"{device_id}: init_param_schema.config 已声明参数,必须提供对象形式的 init_param_enforce" |
| 62 | + ) |
| 63 | + |
| 64 | + missing = [field for field in required_fields if field not in init_enforce] |
| 65 | + if missing: |
| 66 | + raise error_factory( |
| 67 | + f"{device_id}: init_param_enforce 缺少 required 字段: {', '.join(missing)}" |
| 68 | + ) |
| 69 | + |
| 70 | + _reject_legacy_init_enforce( |
| 71 | + init_enforce, |
| 72 | + f"{device_id}.init_param_enforce", |
| 73 | + error_factory, |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def _reject_legacy_init_enforce( |
| 78 | + value: Any, |
| 79 | + path: str, |
| 80 | + error_factory: Callable[[str], Exception], |
| 81 | +) -> None: |
| 82 | + if isinstance(value, str): |
| 83 | + if "${" in value and "}" in value: |
| 84 | + raise error_factory(f"{path}: init_param_enforce 不支持 ${{...}} 模板,动态值应来自实例 config") |
| 85 | + return |
| 86 | + |
| 87 | + if isinstance(value, list): |
| 88 | + for index, item in enumerate(value): |
| 89 | + _reject_legacy_init_enforce(item, f"{path}[{index}]", error_factory) |
| 90 | + return |
| 91 | + |
| 92 | + if not isinstance(value, dict): |
| 93 | + return |
| 94 | + |
| 95 | + keys = set(value) |
| 96 | + if "factory" in keys or "args" in keys or "kwargs" in keys or keys == {"value"}: |
| 97 | + raise error_factory(f"{path}: init_param_enforce 不支持 class.init 的 factory/args/kwargs/value DSL") |
| 98 | + |
| 99 | + for key, item in value.items(): |
| 100 | + _reject_legacy_init_enforce(item, f"{path}.{key}", error_factory) |
0 commit comments