Representing logic as data in JSON

前端 未结 12 1494
眼角桃花
眼角桃花 2021-01-29 21:37

For business reasons we need to externalize some conditional logic into external files: preferably JSON.

A simple filter-by scenario could be handled by adding a node a

12条回答
  •  逝去的感伤
    2021-01-29 22:15

    Following Jeremy Wadhams comment, I implemented a parser I hope it can help you:

    https://play.golang.org/p/QV0FQLrTlyo

    The idea is to set all logic operators in special keys with $ character like $and or $lte.

    As an example:

    { 
       "$or":[ 
          { 
             "age":{ 
                "$lte":3
             }
          },
          { 
             "name":"Joe"
          },
          { 
             "$and":[ 
                { 
                   "age":5
                },
                { 
                   "age ":{ 
                      " $nin ":[ 
                         1,
                         2,
                         3
                      ]
                   }
                }
             ]
          }
       ]
    }
    

    Regards.

    Is translated as:

     ( age  <= 3 OR  name  = Joe  OR  ( age  = 5  AND  age  NOT IN (1,2,3) )  )  
    

提交回复
热议问题