jamsesso / json-logic-java

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

Integers cast to double in custom operations #12

Closed pn closed 5 years ago

pn commented 5 years ago

When using custom operations integers are cast to double (does not happen for longs). Is this expected behavior? I expected Int.

Here's a test in kotlin:

    @Test
    fun operationParamType() {
        jsonLogic.addOperation("withLong") {
            assert(it[0] is Long)
        }
        jsonLogic.addOperation("withDouble") {
            assert(it[0] is Double)
        }
        jsonLogic.addOperation("withFloat") {
            assert(it[0] is Float)
        }
        jsonLogic.addOperation("withInt") {
            assert(it[0] is Int) // this fails because argument is Double
        }
        jsonLogic.apply("""{"withLong": [{"var": "a"}]}""", mapOf("a" to 1L))
        jsonLogic.apply("""{"withDouble": [{"var": "a"}]}""", mapOf("a" to 1.0))
        jsonLogic.apply("""{"withFloat": [{"var": "a"}]}""", mapOf("a" to 1.0f))
        jsonLogic.apply("""{"withInt": [{"var": "a"}]}""", mapOf("a" to 1)) // fails
    }
jamsesso commented 5 years ago

Good question - there are two answers:

  1. This is expected because I need to comply with the json-logic-js implementation and Javascript does not have an int primitive: https://www.ecma-international.org/ecma-262/5.1/#sec-4.3.19
  2. I'd expect all of these cases to fail except for the withDouble case because each of them should use doubles. This is an issue that needs to be fixed.
jamsesso commented 5 years ago

@pn I'll leave that PR up for a few days for your review.

pn commented 5 years ago

PR #13 LGTM