json-logic-js icon indicating copy to clipboard operation
json-logic-js copied to clipboard

Add a built in operator "meta" for metadata

Open deinspanjer opened this issue 7 years ago • 0 comments

One of my developers ran into a problem with my API that uses json-logic for parsing. In the normal case, they might have a query such as these:

{"~": [{"var": "hierarchies"}, "Geo.AMER.USA.*"]}
{"~": [{"var": "hierarchies"}, "Geo.EMEA.*"]}
{"~": [{"var": "hierarchies"}, "Alternate.Favorites.*"]}

And when they read that query in, they can easily re-build the query form by just interpreting the value where the first token is the hierarchy to be selected (Geo or Alternate).

The problem comes when the user builds a query that is selecting individual devices instead of hierarchy folders:

{"and": [{"or": [[{"==": [{"var": "device_id"}, "9895bb67-93a7-4bde-86d8-c9b2fe8d4667"]}]]}]}

If they select a device, we don't care what hierarchy it is in for the query itself, but the user found the device by drilling through the hierarchy, and when we rebuild the query form for them to edit, they should see the same hierarchy they originally selected it from.

So my solution is to add a new operator for json-logic called "meta". The logic for it is that it takes two arguments, and always returns the first argument for further evaluation while the second is completely ignored.

{"and": [{"or": [[{"==": [{"meta":[{"var": "device_id"},{"hierarchy":"Flat_List"}]}, "9895bb67-93a7-4bde-86d8-c9b2fe8d4667"]}]]}]}
// or even
{"meta":[{"and": [{"or": [[{"==": [{"var": "device_id"}, "9895bb67-93a7-4bde-86d8-c9b2fe8d4667"]}]]}]},{"hierarchy":"Flat_List","happy_attr":[1,2,3]}]}

In some ways, this is similar to log, but I didn't want to use log because that has a side-effect of writing stuff to the log. ;)

This seemed like it might be useful enough in the general case so I thought I'd also open an issue for it.

deinspanjer avatar May 18 '17 12:05 deinspanjer