-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
86 lines (68 loc) · 2.83 KB
/
utils.py
File metadata and controls
86 lines (68 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Parameter validation and substitution utilities"""
# TODO: This thing should be general wire utils, not tied to specific wire
import re
from asyncapi_python.kernel.document.channel import Channel
def validate_parameters_strict(channel: Channel, provided: dict[str, str]) -> None:
"""
Strict parameter validation - all defined parameters must be provided.
Raises ValueError with detailed message if any parameters are missing.
"""
if not channel.parameters:
return # No parameters defined, nothing to validate
required = set(channel.parameters.keys())
provided_keys = set(provided.keys())
missing = required - provided_keys
if missing:
raise ValueError(
f"Missing required parameters for channel '{channel.address}': {missing}. "
f"Required: {sorted(required)}, Provided: {sorted(provided_keys)}"
)
extra = provided_keys - required
if extra:
raise ValueError(
f"Unexpected parameters for channel '{channel.address}': {extra}. "
f"Expected: {sorted(required)}, Provided: {sorted(provided_keys)}"
)
def substitute_parameters(template: str, parameters: dict[str, str]) -> str:
"""
Substitute {param} placeholders with actual values.
All placeholders must have corresponding parameter values.
"""
# Find all {param} placeholders
placeholders = re.findall(r"\{(\w+)\}", template)
# Check for undefined placeholders
undefined = [p for p in placeholders if p not in parameters]
if undefined:
raise ValueError(
f"Template '{template}' references undefined parameters: {undefined}. "
f"Available parameters: {sorted(parameters.keys())}"
)
# Perform substitution
result = template
for key, value in parameters.items():
result = result.replace(f"{{{key}}}", value)
return result
def validate_channel_template(
channel: Channel, template_name: str, template: str
) -> None:
"""
Validate that a template only references defined channel parameters.
Should be called during application startup to catch configuration errors early.
"""
if not template:
return
placeholders = re.findall(r"\{(\w+)\}", template)
if not placeholders:
return # No parameters used in template
if not channel.parameters:
raise ValueError(
f"Channel {template_name} template '{template}' uses parameters {placeholders} "
f"but no parameters are defined for the channel"
)
undefined = [p for p in placeholders if p not in channel.parameters]
if undefined:
raise ValueError(
f"Channel {template_name} template '{template}' references "
f"undefined parameters: {undefined}. "
f"Defined parameters: {sorted(channel.parameters.keys())}"
)