Resolve ${var} references in a string value.
(value: str, variables: dict[str, Any])
| 917 | |
| 918 | |
| 919 | def _resolve_string(value: str, variables: dict[str, Any]) -> Any: |
| 920 | """Resolve ${var} references in a string value.""" |
| 921 | if not value or "${" not in value: |
| 922 | return value |
| 923 | |
| 924 | # Single variable reference: "${var}" → return the value directly (preserves type) |
| 925 | if value.startswith("${") and value.endswith("}") and value.count("${") == 1: |
| 926 | var_path = value[2:-1] |
| 927 | resolved = _resolve_path(var_path, variables) |
| 928 | return resolved if resolved is not None else value |
| 929 | |
| 930 | # Template string: "text ${var} more" → string interpolation |
| 931 | def replacer(match: re.Match) -> str: |
| 932 | var_path = match.group(1) |
| 933 | resolved = _resolve_path(var_path, variables) |
| 934 | return str(resolved) if resolved is not None else match.group(0) |
| 935 | |
| 936 | return re.sub(r"\$\{([^}]+)}", replacer, value) |
| 937 | |
| 938 | |
| 939 | def _resolve_path(var_path: str, variables: dict[str, Any]) -> Any: |
no test coverage detected