jsonlogic-swift

CI Status Version Platform codecov

一个原生的 Swift JsonLogic 实现。该解析器接受 JsonLogic 规则并执行它们。

JsonLogic 是一种使用 JSON 格式编写包含计算的规则的方式,这些规则可以应用于 JSON 数据并产生一致的结果。 因此,您可以在服务器和客户端之间以通用格式共享规则。原始的 JS JsonLogic 实现由 Jeremy Wadhams 开发。

安装

使用 CocoaPods

要在您的项目中使用 pod,请在 Podfile 中添加

pod jsonlogic

要运行示例项目,只需运行

pod try jsonlogic    

使用 Swift Package Manager

如果您使用 Swift Package Manager,请在 dependencies 中添加以下内容

    dependencies: [
    .package(
        url: "https://github.com/advantagefse/json-logic-swift", from: "1.0.0"
    )
]

用法

您只需导入模块并调用 applyRule 全局方法

import jsonlogic

let rule =
"""
{ "var" : "name" }
"""
let data =
"""
{ "name" : "Jon" }
"""

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

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

applyRule 将解析规则,然后将其应用于 data,并尝试将结果转换为推断的返回类型。如果失败,将抛出错误。

如果您需要将同一规则应用于多个数据,那么最好只解析一次规则。 您可以通过使用规则初始化一个 JsonRule 对象,然后调用 applyRule 来实现。

//Example parsing
let jsonlogic = try JsonLogic(rule)

var result: Bool = jsonlogic.applyRule(to: data1)
result = jsonlogic.applyRule(to: data2)
//etc..

示例

简单

let rule = """
{ "==" : [1, 1] }
"""

let result: Bool = try applyRule(rule)
//evaluates to true

这是一个简单的测试,相当于 1 == 1。关于格式的一些说明

  1. 运算符始终位于“键”的位置。 每个 JsonLogic 规则只有一个键。
  2. 这些值通常是一个数组。
  3. 每个值可以是字符串、数字、布尔值、数组(非关联)或 null

复合

在这里,我们开始嵌套规则。

let rule = """
  {"and" : [
    { ">" : [3,1] },
    { "<" : [1,3] }
  ] }
"""
let result: Bool = try applyRule(rule)
//evaluates to true

在一种中缀语言中,这可以写成

( (3 > 1) && (1 < 3) )

数据驱动

显然,如果这些规则只能接受静态字面数据,它们就不是很有趣。 通常,jsonLogic 会被调用,并传入一个规则对象和一个数据对象。 您可以使用 var 运算符来获取数据对象的属性

let rule = """
  { "var" : ["a"] }
"""
let data = """
  { a : 1, b : 2 }
"""
let result: Int = try applyRule(rule, to: data)
//evaluates to 1

如果您喜欢,我们支持跳过值周围的数组

let rule = """
  { "var" : "a" }
"""
let data = """
  { a : 1, b : 2 }
"""
let result: Int = try applyRule(rule, to: data)
//evaluates to 1

您还可以使用 var 运算符按数字索引访问数组

jsonLogic.apply(
  {"var" : 1 },
  [ "apple", "banana", "carrot" ]
);
// "banana"

这是一个混合了字面量和数据的复杂规则。 只有当馅饼的温度低于 110 度,并且装满了苹果,才能食用。

let rule = """
{ "and" : [
  {"<" : [ { "var" : "temp" }, 110 ]},
  {"==" : [ { "var" : "pie.filling" }, "apple" ] }
] }
"""
let data = """
  { "temp" : 100, "pie" : { "filling" : "apple" } }
"""

let result: Bool = try applyRule(rule, to: data)
//evaluates to true

自定义运算符

您可以注册一个自定义运算符

import jsonlogic
import JSON

// the key is the operator and the value is a closure that takes as argument
// a JSON and returns a JSON
let customRules =
    ["numberOfElementsInArray": { (json: JSON?) -> JSON in                                 
        switch json {
        case let .Array(array):
            return JSON(array.count)
        default:
            return JSON(0)
        }
    }]
    
let rule = """
    { "numberOfElementsInArray" : [1, 2, 3] }
"""
    
// The value is 3
let value: Int = try JsonLogic(rule, customOperators: customRules).applyRule()

其他运算符

有关支持的运算符及其用法的完整列表,请参阅 jsonlogic 运算符

命令行界面

敬请期待...

贡献

欢迎做出更改。如果您发现错误,请在提交修复之前提交一个重现该错误的单元测试。

由于该项目是使用 Swift PM 创建和构建的,因此仓库中没有提交 Xcode 项目文件。 如果您需要一个,您可以通过在终端中运行 genenate-xcodeproj.sh 来生成一个

$ . generate-xcodeproj.sh

要求

iOS tvOS watchOS macOS
>=9.0 >=10.0 >=2.0 >=10.12

作者

Christos Koninis, c.koninis@afse.eu

许可证

Swift 版 JsonLogic 在 MIT 许可证下可用。 有关更多信息,请参见 LICENSE 文件。