Giter Site home page Giter Site logo

advantagefse / json-logic-swift Goto Github PK

View Code? Open in Web Editor NEW
25.0 25.0 22.0 167 KB

A native Swift JsonLogic implementation. This parser accepts JsonLogic rules and executes them.

License: MIT License

Swift 98.66% Ruby 1.19% Shell 0.15%
cocoapods ios json jsonlogic spm swift

json-logic-swift's People

Contributors

4-sho avatar alexchornyi avatar csknns avatar mauriciomaniglia avatar rep2 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

json-logic-swift's Issues

Division by zero

Hello,

Thank you for this library.

Potential unhandled crash: Division by Zero

I have identified a runtime crash issue caused by division by zero. For instance, the following code snippet causes the library to crash:

let rule =
"""
{ "/" : [10, 0] }
"""

// Example parsing
let result: String? = try? JSONLogicClient.applyRule(rule, to: nil)

print("result = \(String(describing: result))")

This pull request #33 implements a fix to prevent this crash.

Define truthy evaluation for Dictionaries

In the original JS implementation https://jsonlogic.com/truthy.htm truthy evaluation for empty arrays is false but for empty dictionaries it is not defined.
For consistency we will define it as false if empty.

So this will evaluate to false
{"==" : [{}, true]}

And this to true
{"==" : [{ "key": value}, true]}

Also fix typo thruthy->truthy

Using applyRule with Double data types

Hello. I'm applying JsonLogic to a Swift Double data type and expecting the result to be of the same type.

{"if": [ {">": [ {"var": "double1"}, {"var": "double2"} ] }, {"var": "double1"} ]}

with the values

 { "double1":1618495500, "double2":1618495000 } 

My exception is for JsonLogic.applyRule to return a Double data type with the value 1618495500.
However given the jsonDataOrNil param of applyRule is a String data type I suspect the Double values are cast as Int as there is no explicit tag to denote these values as Double. Therefore applyRule returns an Int data type with the value 1618495500.
Note that if I adjust my values to include a fractional component applyRule returns a Double data type as expected.

 { "double1":1618495500.01, "double2":1618495000.01 } 

Keen to hear your thoughts on this and preferred workaround?

Strict comparison between Arrays and Dictionaries is not applied recursively to nested items

This is a small but important difference between the way https://jsonlogic.com JS implementation works and this swift implementation.

The original JS implementation allows only Numbers, Strings, Null, Boolean. The following eval to true:

{"===" : ["123", "123"]}

{"===" : [123, "123]}

{"===" : [false, false]}

{"===" : [null, null]}

But this implementation allows also JSON Dictionaries and Arrays. The following eval to true:

{"===" : [ ["123"], ["123"]]}

{"===" : [ {"key": 1}, {"key": 1}]}

There is bug that with strict comparison for Dictionaries and Arrays that leads to type coercion for the nested values.
So this will eval as true when it should not:
{"===" : [ ["123"], [123]]}

Also for non string equality the dictionary support was missing

Carthage Support

Thanks for this framework! Any plans to include support for Carthage?

Rule evaluates wrong

Hi,

Json-logic-swift is very useful for our development. It works great overall. Thank you!

We discovered a case where a rule does't evaluate to the same result compared to the test tool on https://jsonlogic.com/play.html . Assuming jsonlogic.com is the reference implementation, we think there might be a bug in json-logic-swift.

Here is the test case. It evaluates to TRUE on jsonlogic.com and FALSE with json-logic-swift.

Rule:
{"!": [
{"and" : [
{"in":["Population", { "var" : "CHOIX" }]},
{"in" : [ { "var" : "TAILLE_POP" }, ["", null] ]}
]}
]}

Data:
{ "COMPARTIEMENT" : "Faune/Wildlife", "ACTIF" : "false" }

Have a great day,

Jean-Robert

Upgrade Package.swift version to iOS 9

Starting Xcode 12, the minimum os version supported is iOS 9. When using this package, it shows the below warning.

'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.0.99.

Request to bump the Package.swift to use v9 as developers will be using newer Xcodes to build their project. We are at 13.4 version of Xcode now.

Are defaults supported?

Hi, I'm currently trying to build my use case using your jsonlint package and I'm trying to do additions with data that might be missing some keys. To circumvent this, I'm adding defaults as documented here, but i'm always receiving keyNotFound Errors.
Are default values supported?

jsonLogic:

{
	"+": [{
		"var": ["MtgS-wRFsq2W6ZOZ9gq", 0]
	}, {
		"var": ["MtgS1WFJ2DaVGrpvUha", 0]
	}, {
		"var": ["MtgS7bS6e1trrSe-suX", 0]
	}, {
		"var": ["MtgSJyxA9kEf6b_61Vz", 0]
	}]
}

Parsed jsonLogic Expression:

(jsonlogic.ArrayOfExpressions) $R11 = {
  expressions = 4 values {
    [0] = {
      expression = {
        expressions = 2 values {
          [0] = {
            json = String (String = "MtgS-wRFsq2W6ZOZ9gq")
          }
          [1] = {
            json = Int (Int = 0)
          }
        }
      }
    }
    [1] = {
      expression = {
        expressions = 2 values {
          [0] = {
            json = String (String = "MtgS1WFJ2DaVGrpvUha")
          }
          [1] = {
            json = Int (Int = 0)
          }
        }
      }
    }
    [2] = {
      expression = {
        expressions = 2 values {
          [0] = {
            json = String (String = "MtgS7bS6e1trrSe-suX")
          }
          [1] = {
            json = Int (Int = 0)
          }
        }
      }
    }
    [3] = {
      expression = {
        expressions = 2 values {
          [0] = {
            json = String (String = "MtgSJyxA9kEf6b_61Vz")
          }
          [1] = {
            json = Int (Int = 0)
          }
        }
      }
    }
  }
}

Data parsed to JSON:

  Dictionary = 10 key/value pairs {
    [0] = {
      key = "MtgS7bS6e1trrSe-suX"
      value = Int (Int = 0)
    }
    [1] = {
      key = "MtgS-wRFsq2W6ZOZ9gq"
      value = Int (Int = 1)
    }
    [2] = {
      key = "MtgRwxWKfwXn-UNCzET"
      value = Int (Int = 0)
    }
    [3] = {
      key = "MtgS34Y8AXt1UJiTYyY"
      value = Int (Int = 0)
    }
    [4] = {
      key = "MtgRyq_u9Nu8FpYGnQW"
      value = Int (Int = 0)
    }
    [5] = {
      key = "MtgS6A5NxGFs2BIwYX5"
      value = Int (Int = 0)
    }
    [6] = {
      key = "MtgS1WFJ2DaVGrpvUha"
      value = Int (Int = 0)
    }
    [7] = {
      key = "MtgS9OWoc3O8CXJeSdy"
      value = Int (Int = 0)
    }
    [8] = {
      key = "MtgS4PC_qmPCcjyH5CA"
      value = Int (Int = 0)
    }
    [9] = {
      key = "MtgSAfANO6aGCRzVdcp"
      value = Int (Int = 0)
    }
  }
}

Add linux support

Currently the library does not compile in Linux.
This is due to the bridging support for Cocoa types is available only in Darwin platforms, so this does not work:

throw` NSError() // In Linux -> error: thrown expression type 'NSError?' does not conform to 'Error'

Also we should add travis build for linux

License clarification

The last sentence of the README states that this project is licensed under LGPL. However, the project's LICENSE file has the regular GNU GPLv3 license. What's the actual license?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.