changeset 39:78f5ef0ee087

Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
author Franz Glasner <f.glasner@feldmann-mg.com>
date Wed, 19 Jul 2023 14:19:21 +0200
parents 5a2fba996773
children 2376224a9717
files data_schema/__init__.py docs/schema.txt
diffstat 2 files changed, 24 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/data_schema/__init__.py	Wed Jul 19 13:30:55 2023 +0200
+++ b/data_schema/__init__.py	Wed Jul 19 14:19:21 2023 +0200
@@ -1266,7 +1266,8 @@
     """
     pred, predval = _get_one_of(
         cond,
-        "when-ref-true", "when-ref-exists", "when",
+        "when-ref-true", "when-ref-false",
+        "when-ref-exists", "when-ref-not-exists", "when",
         default=_SENTINEL)
 
     if pred == "when":
@@ -1275,9 +1276,15 @@
     elif pred == "when-ref-true":
         refobj = try_get_reference(predval, context, schema, default=None)
         return bool(refobj)
+    elif pred == "when-ref-false":
+        refobj = try_get_reference(predval, context, schema, default=None)
+        return not bool(refobj)
     elif pred == "when-ref-exists":
         refobj = try_get_reference(predval, context, schema, default=_SENTINEL)
         return refobj is not _SENTINEL
+    elif pred == "when-ref-not-exists":
+        refobj = try_get_reference(predval, context, schema, default=_SENTINEL)
+        return refobj is _SENTINEL
     else:
         raise SchemaError("unknown condition type: {}".format(pred))
 
@@ -1325,16 +1332,25 @@
         else:
             pred_key, pred_val = _get_one_of(
                 pred,
-                "ref-true", "ref-exists", "equals",
+                "ref-true", "ref-false", "ref-exists", "ref-not-exists",
+                "equals",
                 default=None)
             if pred_key == "ref-true":
                 refobj = try_get_reference(
                     pred_val, context, schema, default=None)
                 return bool(refobj)
+            elif pred_key == "ref-false":
+                refobj = try_get_reference(
+                    pred_val, context, schema, default=None)
+                return not bool(refobj)
             elif pred_key == "ref-exists":
                 refobj = try_get_reference(
                     pred_val, context, schema, default=_SENTINEL)
                 return refobj is not _SENTINEL
+            elif pred_key == "ref-not-exists":
+                refobj = try_get_reference(
+                    pred_val, context, schema, default=_SENTINEL)
+                return refobj is _SENTINEL
             elif pred_key == "equals":
                 if not isinstance(pred_val, (list, tuple)):
                     raise SchemaError("`equals' requires a list as childs")
--- a/docs/schema.txt	Wed Jul 19 13:30:55 2023 +0200
+++ b/docs/schema.txt	Wed Jul 19 14:19:21 2023 +0200
@@ -406,7 +406,8 @@
 
   - eine Liste von Wenn-Dann-Paaren
 
-    Bedingung: ``when``, ``when-ref-true``, ``when-ref-exists``
+    Bedingung: ``when``, ``when-ref-true``, ``when-ref-false``, 
+    ``when-ref-exists``, ``when-ref-not-exists``
 
     Dann: ``then``, ``then-merge``
 
@@ -424,7 +425,8 @@
 
       Prädikate:
 
-        ``ref-true``, ``ref-exists``, ein Objekt im boolschen Kontext
+        ``ref-true``, ``ref-false``, ``ref-exists``, ``ref-not-exists``
+        ein Objekt im boolschen Kontext
 
       Vergleichs-Operator:
 
@@ -453,6 +455,8 @@
          when:
            ref-exists:  ...
 
+    Analoges gilt für ``when-ref-false`` und ``when-ref-not-exists``.
+
   - die *erste* zutreffende Bedingung bestimmt via seinem "Dann" ein Schema
 
       ``then``