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

Add assignment operation

Open txomon opened this issue 7 years ago • 4 comments

I really like the idea of having all this json specs, but I miss the ability to update values of variables, or to create variables.

I think it can be done quite easily, by just specifying and equals operator {"=" : [{"var": "new_value"}, ...]}"

It is backwards compatible, maintains the spec and gives a really nice functionality for dynamic logic execution.

Of course, the return value would be data with {"new_value": ...} added.

txomon avatar Jul 23 '16 17:07 txomon

Yeah, it's not needful yet the way I'm using it, but I can see what you mean.

I'm tempted to use something other than = as the assignment operator, to avoid assignment / equality confusion. How would you feel about set instead, since the whole spec is inspired by Lisp s-expressions?

Also, I'd expect the return value to be the value being set, that's how assignment works in JavaScript, PHP and Lisp:

a = b = c = 5;
a //5
b //5
c //5

Would it meet your needs with those changes?

jwadhams avatar Jul 25 '16 15:07 jwadhams

I don't really understand your proposal... Could you make an example?

txomon avatar Jul 29 '16 08:07 txomon

Ok, I have read many times this until suddenly it made clack on my mind, and I understood the piece of code you put there.

I am mostly familiarized with C-Python-Java-Javascript from one side and VHDL from the other, so I don't feel the burden in any case, however, the first example in the webpage jsonLogic.apply( { "==" : [1, 1] } ); uses == for comparison, so one thinks on = for assignment. These two always go together.

Also, the fact that we are using json (javascript object notation) leads one to think that JS notation is in use, and it's kind of misleading breaking that javascript notation with the assignment.

The = syntax is easily usable for most of the languages, using := as an assignment may be weird for most of the people. However we could support := and = without any problem, although I find it kind of dirty.

AST construction in any case will remain the same, and just the DSL definition will change a little.

Therefore, my vote is for = for assignment, with strong preference based on existing syntax, but I would accept := without any problem =)

txomon avatar Aug 01 '16 20:08 txomon

I created a set property (https://github.com/diegoholiveira/jsonlogic/blob/master/jsonlogic.go#L498).

I use it like this: https://github.com/diegoholiveira/jsonlogic/blob/master/jsonlogic_test.go#L316

Currently I'm using JsonLogic to allow my app to calculate deadlines to delivery packages and the set is used to extend the deadline under some scenarios.

diegoholiveira avatar Feb 05 '19 12:02 diegoholiveira