Repository: markusressel/container-app-conf

Issue:
limit range of IntConfigEntry using range parameter
Add a constructor parameter to `IntConfigEntry` and `FloatConfigEntry` to limit valid input to a specific range.

Discussion:

Patch:
diff --git a/container_app_conf/entry/float.py b/container_app_conf/entry/float.py
index 6ddb2ca..ee85223 100644
--- a/container_app_conf/entry/float.py
+++ b/container_app_conf/entry/float.py
@@ -17,6 +17,7 @@
 #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 #  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 #  SOFTWARE.
+from py_range_parse import Range
 
 from container_app_conf import ConfigEntry
 
@@ -24,6 +25,11 @@ from container_app_conf import ConfigEntry
 class FloatConfigEntry(ConfigEntry):
     _example = "3.1415926535"
 
+    def __init__(self, key_path: [str], example: any = None, description: str or None = None, default: any = None,
+                 none_allowed: bool = None, range: Range = None):
+        self.range = range
+        super().__init__(key_path, example, description, default, none_allowed)
+
     def _value_to_type(self, value: any) -> float or None:
         """
         Tries to permissively convert the given value to a float.
@@ -31,9 +37,13 @@ class FloatConfigEntry(ConfigEntry):
         :return: the parsed float value
         """
         if isinstance(value, str) and '%' == value[-1]:
-            return float(value[:-1]) / 100.0
+            parsed_value = float(value[:-1]) / 100.0
         else:
-            return float(value)
+            parsed_value = float(value)
+
+        if self.range is not None and parsed_value not in self.range:
+            self._raise_invalid_value(value)
+        return parsed_value
 
     def _type_to_value(self, type: any) -> any:
         return float(type)
diff --git a/container_app_conf/entry/int.py b/container_app_conf/entry/int.py
index 3e50310..beb6ad1 100644
--- a/container_app_conf/entry/int.py
+++ b/container_app_conf/entry/int.py
@@ -17,6 +17,7 @@
 #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 #  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 #  SOFTWARE.
+from py_range_parse import Range
 
 from container_app_conf import ConfigEntry
 
@@ -24,13 +25,21 @@ from container_app_conf import ConfigEntry
 class IntConfigEntry(ConfigEntry):
     _example = "42"
 
+    def __init__(self, key_path: [str], example: any = None, description: str or None = None, default: any = None,
+                 none_allowed: bool = None, range: Range = None):
+        self.range = range
+        super().__init__(key_path, example, description, default, none_allowed)
+
     def _value_to_type(self, value: any) -> int or None:
         """
         Tries to permissively convert the given value to an int.
         :param value: the value to parse
         :return: the parsed int value
         """
-        return int(value)
+        parsed_value = int(value)
+        if self.range is not None and parsed_value not in self.range:
+            self._raise_invalid_value(value)
+        return parsed_value
 
     def _type_to_value(self, type: any) -> any:
         return int(type)


Test patch:
diff --git a/tests/entry_test.py b/tests/entry_test.py
index 820d586..456e6d6 100644
--- a/tests/entry_test.py
+++ b/tests/entry_test.py
@@ -53,18 +53,19 @@ class EntryTest(EntryTestBase):
         self.assert_input_output(config_entry, input_output)
 
     def test_int_entry(self):
-        config_entry = IntConfigEntry(key_path=["int"])
+        config_entry = IntConfigEntry(key_path=["int"], range=Range(-5, 5))
         input_output = [
             ("5", 5),
             (5, 5),
             ("-3", -3),
-            (-3, -3)
+            (-3, -3),
+            (-6, ValueError)
         ]
 
         self.assert_input_output(config_entry, input_output)
 
     def test_float_entry(self):
-        config_entry = FloatConfigEntry(key_path=["float"])
+        config_entry = FloatConfigEntry(key_path=["float"], range=Range(-10.0, 10))
         input_output = [
             ("5", 5.0),
             (5, 5.0),
@@ -72,7 +73,8 @@ class EntryTest(EntryTestBase):
             (-3.0, -3.0),
             (1.2, 1.2),
             ("1.2", 1.2),
-            ("3%", 0.03)
+            ("3%", 0.03),
+            (-20.0, ValueError)
         ]
 
         self.assert_input_output(config_entry, input_output)