No match when using default_value for nested objects
See original GitHub issueHello,
I was playing with this cool lib when i stumbled upon the following issue.
When i set default_value to None (or any other value) in a rule’s context, i don’t get any matches for nested objects. Here is a code example :
#! /usr/bin/env python3
import rule_engine
dic = {
"a": 1,
"b": "test1",
"c": {
"c1": 2,
"c2": "test2"
}
}
rule_text = 'a == 1'
rule1 = rule_engine.Rule(rule_text, context=rule_engine.Context())
rule2 = rule_engine.Rule(rule_text, context=rule_engine.Context(default_value=None))
a_match1 = rule1.matches(dic)
a_match2 = rule2.matches(dic)
assert a_match1 == a_match2
rule_text = 'c.c1 == 2'
rule1 = rule_engine.Rule(rule_text, context=rule_engine.Context())
rule2 = rule_engine.Rule(rule_text, context=rule_engine.Context(default_value=None))
c_match1 = rule1.matches(dic)
c_match2 = rule2.matches(dic)
assert c_match1 == c_match2
Only c_match assertion fails.
I think what happens here is c.c1 is replaced with None. The docs says that default_value is used for missing attributes only. c.c1 is not missing therefore it shouldn’t be replaced with None (or any set default_value).
Great lib by the way.
Thanks in advance!
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Nested object is not initialized if no matching property ... - GitHub
If a matching property is found, the Security type is initialized and provided to the constructor. This is consistent with what we do...
Read more >Field 'id' doesn't have a default value because of nested object ...
I am getting field 'id' doesn't have a default value error in my Spring application. I am trying to create an Applicant with...
Read more >Using ES6 To Destructure Deeply Nested Objects in ... - ITNEXT
The Issue: ES6 Object Destructuring & Deeply Nested Objects ... ES6 Destructured Object with No Default Props const { props: { match }...
Read more >Extend the Kubernetes API with CustomResourceDefinitions
In the following example, the custom object contains fields with invalid values: spec.cronSpec does not match the regular expression. spec.
Read more >17.7.0 API Reference - joi.dev
joi lets you describe your data using a simple, intuitive, and readable ... The schema can be a plain JavaScript object where every...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Should be fixed in commit 2754422dcbffa2c83f7fa67ac60cd052519a64f2. I made it the caller’s responsibility to determine whether or not the context’s default value should be used. This seemed like the easiest and most accurate way to avoid a possible scenario where the legitimate value is the same as the default value and the resolution process carried on because it couldn’t tell the difference.
I’m going to close this out since I’m confident I’ve addressed the issue. If you want to give it a test, that’d be great. I’ll plan on publishing the new version to include the patch in a few days.
This bug fix has just been published in version v2.4.1.