jsonlogic 1.2.4

jsonlogic 1.2.4

Christos Koninis维护。



jsonlogic 1.2.4

  • Christos Koninis

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 包管理器

如果您使用 Swift 包管理器,请在依赖项中添加以下内容

    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
>=8.0 >=10.0 >=2.0 >=10.12

作者

克里斯托斯·康尼亚斯,[邮箱地址保护 - 34571a5f5b5a5d5a5d4774555247511a5141]

许可

Swift 的 JsonLogic 适用于 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。