Skip to content
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

Accessing data using var path inside an array #87

Open
cj-f opened this issue Aug 8, 2024 · 4 comments
Open

Accessing data using var path inside an array #87

cj-f opened this issue Aug 8, 2024 · 4 comments

Comments

@cj-f
Copy link

cj-f commented Aug 8, 2024

Hey, not sure if this is best place to post this question, as I don't think it is a problem with the golang jsonlogic as I am noticing the same behavior in go as I do in the play.html page on jsonlogic, but I'm trying to figure out how I can compare array values against data outside of the array

I know that var inside of an array is relative, so { "var": "id" } appears to work, but is there any way I could check that against data outside of the array like the subject.id. If var inside an array is only relative, is the only solution to add a custom operator like absolute_var, if yes, if there any online examples where I could understand how to write an operator like that in golang

Rule: {"some": [{ "var": "attributes.parents" }, {"==": [{ "var": "id" }, { "var": "subject.id" }]}]}

Data: { "subject": { "id": "hugh", }, "attributes": { "parents": [ { "id": "ryan" }, { "id": "hugh" }, ] } }

@diegoholiveira
Copy link
Owner

@joaoandre do you remember how we solve this issue?

@cj-f
Copy link
Author

cj-f commented Aug 8, 2024

Actually upon further testing it appears that when using some and the golang jsonlogic pkg, that you can access absolute variable paths, but when using all it does not seem to resolve absolute vars. When testing on https://jsonlogic.com/play.html I notice both all and some do not resolve absolute variables.

So there may be a difference in the golang package with some. I'm currently testing on github.com/diegoholiveira/jsonlogic/v3 v3.5.3

Although I will say I like the behavior of some in go even if it may be a bug, because I want absolute variables to resolve haha, but I would need it to also work with all

@cj-f
Copy link
Author

cj-f commented Aug 8, 2024

Is the reduce operation also not support in the golang pkg of jsonlogic. I was trying to think of another option if I could get all to work, and figured I could reduce and then cat to build a single string to perform and == check against, but that works on the website, doesn't work using the pkg. Although I just noticed I don't see reduce in the operations.go file

@TotalTechGeek
Copy link

This is unspecified behavior in JSON Logic that prevents most interpreters from being able to do this.

The way json-logic-engine handles this is by implementing Handlebars / Directory style traversal to allow you to access above contexts, so in your case:

{"some": [{ "var": "attributes.parents" }, {"==": [{ "var": "id" }, { "var": "../../subject.id" }]}]}

Would be the way this gets written.

See:


This is partly why I've opened #89, as I believe it would be ideal for the community to come together to identify solutions for these types of common gaps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants