-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle flat JSON escape dot syntax #37
Conversation
Hello @jstewmon @mplanchard 👋 Can you look at it when you have time? 👀 Thank you very much! 🙏 |
I also tested it using this code and it's also working great! import jsonlogic_rs
def main() -> None:
schema = {
"or": [
{"===": [{"var": "a\\.b"}, "Guillaume"]},
{"===": [{"var": "a\\.b.Name"}, "Guillaume"]},
]
}
data1 = {"a.b": "Guillaume"}
data2 = {"a.b": {"Name": "Guillaume"}}
assert jsonlogic_rs.apply(schema, data1) is True
assert jsonlogic_rs.apply(schema, data2) is True
if __name__ == "__main__":
main() |
d7b8eea
to
82e3b77
Compare
Hello @jstewmon and @mplanchard 👋 Can you look at this pull request when you have time? 👀 Thank you! 🤟 |
Hello @jstewmon and @mplanchard 👋 Can you look at this pull request when you have time? 👀 Thank you! 🤟 I'm using this version in prod since 1 month and everything is running fine 💪 Is it possible to be a co-maintainer to be able to update to project with new Python version and to help you to review + merge incoming pull requests? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if we want to use \
as an escape character, the solution should be a little more robust.
For example, a key whose unescaped value is foo\.bar
should be supported. The JSON value for foo\.bar
would be "foo\\\\\\\\\\\\.bar"
😵💫 .
Here's a set of test cases we should add (expressed as unescaped values):
foo\.bar => [foo.bar]
foo\\.bar => [foo\, bar]
foo\\\.bar => [foo\.bar]
foo\\bar => [foo\bar]
I asked Copilot "rust function to split a string by a character, using backslash as escape character", and it suggested the following:
pub fn split_with_escape(input: &str, delimiter: char) -> Vec<String> {
let mut result = Vec::new();
let mut current = String::new();
let mut escape = false;
for c in input.chars() {
if escape {
current.push(c);
escape = false;
} else if c == '\\' {
escape = true;
} else if c == delimiter {
result.push(current.clone());
current.clear();
} else {
current.push(c);
}
}
if !current.is_empty() {
result.push(current);
}
result
}
The Copilot suggestion LGTM if you'd like to use it to amend your PR.
Great idea! 💡 I just implemented it and I added some unit tests. Thanks for the review! 🤟 |
The goal is to support this kind of JSON
using this rule:
It's already implemented in the
json-logic-engine
: https://github.com/TotalTechGeek/json-logic-engineSee:
I already made the code for you but I'm not a Rust developer so feel free to edit the code as you wish 😉
I tested my code using the following Python code: