jamsesso / json-logic-java

A pure Java implementation of JsonLogic without using the Nashorn JS engine
MIT License
97 stars 50 forks source link

How to use `in` array operator with data? #6

Closed pn closed 5 years ago

pn commented 5 years ago

I'm trying to use inoperator with arrays. It works with inline data but I'm not able to make it work with data object.

assertEquals(true, jsonLogic.apply("""{"in": [1, [1, 2, 3]]}""", null)) // this works

val data1 = mapOf("value" to 1)
assertEquals(true, jsonLogic.apply("""{"in": [{"var": "value"}, [1, 2, 3]]}""", data1)) // does not work
val data2 = mapOf("list" to listOf(1, 2, 3))
assertEquals(true, jsonLogic.apply("""{"in": [1, {"var": "list"}]}""", data2)) // does not work

Am I doing something wrong? Or is it a bug?

jamsesso commented 5 years ago

Does it work on json-logic.com in the playground?

On Fri, May 17, 2019 at 10:08 AM Paweł Nadolski notifications@github.com wrote:

I'm trying to use inoperator with arrays. It works with inline data but I'm not able to make it work with data object.

assertEquals(true, jsonLogic.apply("""{"in": [1, [1, 2, 3]]}""", null)) // this works

val data1 = mapOf("value" to 1) assertEquals(true, jsonLogic.apply("""{"in": [{"var": "value"}, [1, 2, 3]]}""", data1)) // does not work val data2 = mapOf("list" to listOf(1, 2, 3)) assertEquals(true, jsonLogic.apply("""{"in": [1, {"var": "list"}]}""", data2)) // does not work

Am I doing something wrong? Or is it a bug?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jamsesso/json-logic-java/issues/6?email_source=notifications&email_token=AAA2KIX5FBMOP3RNJGIYIIDPV2U4FA5CNFSM4HNVEA22YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4GUMZC6A, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA2KITT42T4F5MZCYLG5E3PV2U4FANCNFSM4HNVEA2Q .

pn commented 5 years ago

Yes, both examples work there.

pn commented 5 years ago

Sample values for the playground: Rule: {"in": [1, {"var": "list"}]} Data: {"list": [1,2,3]} is true

Rule: {"in": [1, {"var": "list"}]} Data: {"list": [5,2,3]} is false

jamsesso commented 5 years ago

Thanks for checking, I'll try to take a look at this soon.

pn commented 5 years ago

Looks like integer is evaluated as floating point and then:

ArrayLike(arguments.get(1)).contains(arguments.get(0));

gets 1.0 and [1, 2, 3] and it doesn't match. So, this works:

val data2 = mapOf("list" to listOf(1.0, 2.0, 3.0))
assertEquals(true, jsonLogic.apply("""{"in": [1, {"var": "list"}]}""", data2))
pn commented 5 years ago

@jamsesso, do you have an idea how to fix it?

jamsesso commented 5 years ago

I haven't had a chance to look into it yet.

jamsesso commented 5 years ago

@pn Can you review the above PR?