Skip to content

Commit ca7ce96

Browse files
CopilotMaestroError
andcommitted
Extract duplicated regex pattern as class constant
- Add _JSON_NATIVE_PATTERN class constant to TemplateEngine - Replace inline pattern strings with self._JSON_NATIVE_PATTERN - Eliminates duplication between is_json_native_placeholder and resolve_json_native methods - All 648 tests passing, linting clean Co-authored-by: MaestroError <46760939+MaestroError@users.noreply.github.com>
1 parent e8916f9 commit ca7ce96

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/mcipy/templating.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class TemplateEngine:
3030
- Control blocks: @if(condition) ... @elseif(condition) ... @else ... @endif
3131
"""
3232

33+
# Pattern for JSON-native placeholders: {!!path!!} with optional whitespace
34+
_JSON_NATIVE_PATTERN = r"^\{!!\s*([^}]+?)\s*!!\}$"
35+
3336
def is_json_native_placeholder(self, value: str) -> bool:
3437
"""
3538
Check if a string is a JSON-native placeholder (and only that).
@@ -47,8 +50,7 @@ def is_json_native_placeholder(self, value: str) -> bool:
4750
return False
4851

4952
# Pattern: exactly {!! ... !!} with nothing before or after
50-
pattern = r"^\{!!\s*([^}]+?)\s*!!\}$"
51-
return bool(re.match(pattern, value))
53+
return bool(re.match(self._JSON_NATIVE_PATTERN, value))
5254

5355
def resolve_json_native(self, placeholder: str, context: dict[str, Any]) -> Any:
5456
"""
@@ -76,8 +78,7 @@ def resolve_json_native(self, placeholder: str, context: dict[str, Any]) -> Any:
7678
)
7779

7880
# Extract the path from {!! ... !!}
79-
pattern = r"^\{!!\s*([^}]+?)\s*!!\}$"
80-
match = re.match(pattern, placeholder)
81+
match = re.match(self._JSON_NATIVE_PATTERN, placeholder)
8182
if not match:
8283
raise TemplateError(f"Failed to extract path from placeholder: '{placeholder}'")
8384

0 commit comments

Comments
 (0)