From be769153b305c2909367e5fcb9e68d6a3dcfb57b Mon Sep 17 00:00:00 2001 From: Dominik Krenn Date: Tue, 1 Jul 2025 10:09:38 +0200 Subject: [PATCH] feat: add casting functions for environment variable types --- multinut/env.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/multinut/env.py b/multinut/env.py index 56b1042..80905d2 100644 --- a/multinut/env.py +++ b/multinut/env.py @@ -118,3 +118,30 @@ class Environment: def __getitem__(self, key: str): return self.get(key) + + +def cast_bool(value: str) -> bool: + return value.strip().lower() in ("1", "true", "yes", "on") + +def cast_int(value: str) -> int: + return int(value.strip()) + +def cast_float(value: str) -> float: + return float(value.strip()) + +def cast_str(value: str) -> str: + return str(value) + +def cast_list(value: str) -> list: + return [item.strip() for item in value.split(",")] + +def cast_tuple(value: str) -> tuple: + return tuple(item.strip() for item in value.split(",")) + +def cast_dict(value: str) -> dict: + return json.loads(value) + +def cast_none_or_str(value: str): + if value.strip().lower() in ("null", "none"): + return None + return value