Skip to content

Commit 4e70966

Browse files
Refactor Value.resolve method to use pattern matching (#137)
1 parent eeb7494 commit 4e70966

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/django_bird/params.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,29 @@ class Value:
1818
raw: str | bool | None
1919
quoted: bool = False
2020

21-
def resolve(self, context: Context | dict[str, Any]) -> Any:
22-
if self.raw is None or (isinstance(self.raw, str) and self.raw == "False"):
23-
return None
24-
if (isinstance(self.raw, bool) and self.raw) or (
25-
isinstance(self.raw, str) and self.raw == "True"
26-
):
27-
return True
28-
if isinstance(self.raw, str) and not self.quoted:
29-
try:
30-
return template.Variable(str(self.raw)).resolve(context)
31-
except template.VariableDoesNotExist:
32-
return self.raw
33-
return self.raw
21+
def resolve(self, context: Context | dict[str, Any]) -> str | bool | None | Any:
22+
match (self.raw, self.quoted):
23+
# Handle special string values and None
24+
case (None, _) | ("False", _):
25+
return None
26+
case ("True", _):
27+
return True
28+
29+
# Handle boolean values
30+
case (bool(boolean), _):
31+
return boolean
32+
33+
# Handle quoted strings as literals
34+
case (str(quoted_string), True):
35+
return quoted_string
36+
37+
# Handle everything else as template variables, falling back to raw
38+
case _:
39+
raw_string = str(self.raw)
40+
try:
41+
return template.Variable(raw_string).resolve(context)
42+
except template.VariableDoesNotExist:
43+
return raw_string
3444

3545

3646
@dataclass

0 commit comments

Comments
 (0)