comparison data_schema/__init__.py @ 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
comparison
equal deleted inserted replaced
38:5a2fba996773 39:78f5ef0ee087
1264 """Eval the condition in `cond` and return a tuple `(hit, predval)` 1264 """Eval the condition in `cond` and return a tuple `(hit, predval)`
1265 1265
1266 """ 1266 """
1267 pred, predval = _get_one_of( 1267 pred, predval = _get_one_of(
1268 cond, 1268 cond,
1269 "when-ref-true", "when-ref-exists", "when", 1269 "when-ref-true", "when-ref-false",
1270 "when-ref-exists", "when-ref-not-exists", "when",
1270 default=_SENTINEL) 1271 default=_SENTINEL)
1271 1272
1272 if pred == "when": 1273 if pred == "when":
1273 # rekursive evaluation of `predval` as the real predicate 1274 # rekursive evaluation of `predval` as the real predicate
1274 return eval_pred(predval, context, schema) 1275 return eval_pred(predval, context, schema)
1275 elif pred == "when-ref-true": 1276 elif pred == "when-ref-true":
1276 refobj = try_get_reference(predval, context, schema, default=None) 1277 refobj = try_get_reference(predval, context, schema, default=None)
1277 return bool(refobj) 1278 return bool(refobj)
1279 elif pred == "when-ref-false":
1280 refobj = try_get_reference(predval, context, schema, default=None)
1281 return not bool(refobj)
1278 elif pred == "when-ref-exists": 1282 elif pred == "when-ref-exists":
1279 refobj = try_get_reference(predval, context, schema, default=_SENTINEL) 1283 refobj = try_get_reference(predval, context, schema, default=_SENTINEL)
1280 return refobj is not _SENTINEL 1284 return refobj is not _SENTINEL
1285 elif pred == "when-ref-not-exists":
1286 refobj = try_get_reference(predval, context, schema, default=_SENTINEL)
1287 return refobj is _SENTINEL
1281 else: 1288 else:
1282 raise SchemaError("unknown condition type: {}".format(pred)) 1289 raise SchemaError("unknown condition type: {}".format(pred))
1283 1290
1284 1291
1285 def eval_pred(pred, context, schema): 1292 def eval_pred(pred, context, schema):
1323 raise SchemaError( 1330 raise SchemaError(
1324 "unknown logical operator: {}".format(combinator)) 1331 "unknown logical operator: {}".format(combinator))
1325 else: 1332 else:
1326 pred_key, pred_val = _get_one_of( 1333 pred_key, pred_val = _get_one_of(
1327 pred, 1334 pred,
1328 "ref-true", "ref-exists", "equals", 1335 "ref-true", "ref-false", "ref-exists", "ref-not-exists",
1336 "equals",
1329 default=None) 1337 default=None)
1330 if pred_key == "ref-true": 1338 if pred_key == "ref-true":
1331 refobj = try_get_reference( 1339 refobj = try_get_reference(
1332 pred_val, context, schema, default=None) 1340 pred_val, context, schema, default=None)
1333 return bool(refobj) 1341 return bool(refobj)
1342 elif pred_key == "ref-false":
1343 refobj = try_get_reference(
1344 pred_val, context, schema, default=None)
1345 return not bool(refobj)
1334 elif pred_key == "ref-exists": 1346 elif pred_key == "ref-exists":
1335 refobj = try_get_reference( 1347 refobj = try_get_reference(
1336 pred_val, context, schema, default=_SENTINEL) 1348 pred_val, context, schema, default=_SENTINEL)
1337 return refobj is not _SENTINEL 1349 return refobj is not _SENTINEL
1350 elif pred_key == "ref-not-exists":
1351 refobj = try_get_reference(
1352 pred_val, context, schema, default=_SENTINEL)
1353 return refobj is _SENTINEL
1338 elif pred_key == "equals": 1354 elif pred_key == "equals":
1339 if not isinstance(pred_val, (list, tuple)): 1355 if not isinstance(pred_val, (list, tuple)):
1340 raise SchemaError("`equals' requires a list as childs") 1356 raise SchemaError("`equals' requires a list as childs")
1341 if len(pred_val) != 2: 1357 if len(pred_val) != 2:
1342 raise SchemaError("`equals' requires a list of len 2") 1358 raise SchemaError("`equals' requires a list of len 2")