如何用AND或JSON健壮地描述条件表达式?

时间:2021-08-30 22:32:45

Say I have an expression:

假设我有一个表达式:

( A >= 10 && B == 20 ) || ( C < 30 ) || ( D != 50 )

I can suggest the following JSON to store/represent this expression:

我可以建议使用以下JSON存储/表示这个表达式:

{ "filter": 
    [
        { "var":"A", "condition":"ge", "num":10 },
        { "var":"B", "condition":"e", "num":20 }
    ],
    [
        { "var":"C", "condition":"lt", "num":30 }
    ],
    [
        { "var":"D", "condition":"ne", "num":50 }
    ]
}

in which "filter" maps to an array of sub-arrays. All elements in each sub-array are associated with AND, while all sub-arrays are associated with OR.

其中“筛选器”映射到一组子数组。每个子数组中的所有元素都与AND相关联,而所有子数组都与OR相关联。

Is there anything I've overlooked in writing the JSON like this?

在编写JSON时,我是否忽略了什么?

1 个解决方案

#1


12  

You're making a couple of assumptions here:

你在这里做了几个假设:

  1. Comparisons will be always be between a variable and a number, and never between two variables or two numbers.
  2. 比较总是在一个变量和一个数字之间,而不是在两个变量或两个数字之间。
  3. The variable will always be on the left hand side of the comparison, and the number on the right.
  4. 变量总是在比较的左边,数字在右边。

Those assumptions may be correct for your particular use case, but a more future-proof approach would be to treat comparisons similarly to functions with arguments:

这些假设可能对您的特定用例是正确的,但是一种更有前途的方法是将比较类似于用参数处理函数:

{ "ge": ["A", 10] }

Also, while your idea of using an array of objects to represent AND and an array of arrays to represent OR is clever, it might not be immediately obvious to a human being tasked with writing code to parse it. Reusing the idea of an object where the key represents a function and its associated value the arguments is more expressive:

此外,虽然您使用对象数组表示和数组来表示或表示的想法是明智的,但对于需要编写代码来解析它的人来说,这可能不会立即显现出来。重复使用对象的概念,其中键表示函数及其关联值,参数更具有表达性:

{ "all": [<condition 1>, <condition 2>, ...] }

Putting those two ideas together, we get something like this:

把这两个想法放在一起,我们得到了这样的结果:

{ "any": [
    { "all": [
        { "ge": ["A", 10] },
        { "eq": ["B", 20] }
    ]},
    { "lt": ["C", 30] },
    { "ne": ["D", 50] }
]}

#1


12  

You're making a couple of assumptions here:

你在这里做了几个假设:

  1. Comparisons will be always be between a variable and a number, and never between two variables or two numbers.
  2. 比较总是在一个变量和一个数字之间,而不是在两个变量或两个数字之间。
  3. The variable will always be on the left hand side of the comparison, and the number on the right.
  4. 变量总是在比较的左边,数字在右边。

Those assumptions may be correct for your particular use case, but a more future-proof approach would be to treat comparisons similarly to functions with arguments:

这些假设可能对您的特定用例是正确的,但是一种更有前途的方法是将比较类似于用参数处理函数:

{ "ge": ["A", 10] }

Also, while your idea of using an array of objects to represent AND and an array of arrays to represent OR is clever, it might not be immediately obvious to a human being tasked with writing code to parse it. Reusing the idea of an object where the key represents a function and its associated value the arguments is more expressive:

此外,虽然您使用对象数组表示和数组来表示或表示的想法是明智的,但对于需要编写代码来解析它的人来说,这可能不会立即显现出来。重复使用对象的概念,其中键表示函数及其关联值,参数更具有表达性:

{ "all": [<condition 1>, <condition 2>, ...] }

Putting those two ideas together, we get something like this:

把这两个想法放在一起,我们得到了这样的结果:

{ "any": [
    { "all": [
        { "ge": ["A", 10] },
        { "eq": ["B", 20] }
    ]},
    { "lt": ["C", 30] },
    { "ne": ["D", 50] }
]}