|
| 1 | +# JSON Rules |
| 2 | + |
| 3 | +The JSON Rules serves as an abstraction layer over the [Golang Rules Engine](https://github.com/nikunjy/rules/blob/master/README.md). |
| 4 | + |
| 5 | +This packages allows you to represent rules in JSON format instead of using the original ANTLR query syntax from the nikunjy/rules implementation. |
| 6 | + |
| 7 | +## Example Queries |
| 8 | + |
| 9 | +You can find example queries in the [examples/](test/examples/) directory. |
| 10 | + |
| 11 | +## Operations |
| 12 | + |
| 13 | +The following operations are supported, matching those available in the [Golang Rules Engine](https://github.com/nikunjy/rules): |
| 14 | + |
| 15 | +| expression | meaning | |
| 16 | +| ---------- | ----------------------------------------------- | |
| 17 | +| eq | equals to | |
| 18 | +| == | equals to | |
| 19 | +| ne | not equal to | |
| 20 | +| != | not equal to | |
| 21 | +| lt | less than | |
| 22 | +| < | less than | |
| 23 | +| gt | greater than | |
| 24 | +| > | greater than | |
| 25 | +| le | less than or equal to | |
| 26 | +| <= | less than or equal to | |
| 27 | +| ge | greater than or equal to | |
| 28 | +| >= | greater than or equal to | |
| 29 | +| co | contains | |
| 30 | +| sw | starts with | |
| 31 | +| ew | ends with | |
| 32 | +| in | in a list | |
| 33 | +| pr | present, will be true if you have a key as true | |
| 34 | +| not | not of a logical expression | |
| 35 | + |
| 36 | +## JSON Rule Example |
| 37 | + |
| 38 | +To create JSON rules, follow these steps: |
| 39 | + |
| 40 | +1. The `and` and `or`operation accepts a list of conditions. The and operation evaluates to true only if all conditions in the list are true, while the or operation evaluates to true if at least one condition is true. They are formatted as follows: |
| 41 | + |
| 42 | +```json |
| 43 | +{ |
| 44 | + "and": [ |
| 45 | + { "==": [{ "var": "y" }, 4] }, |
| 46 | + { ">": [{ "var": "x" }, 1] } |
| 47 | + ] |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +```json |
| 52 | +{ |
| 53 | + "or": [ |
| 54 | + { "==": [{ "var": "y" }, 4] }, |
| 55 | + { ">": [{ "var": "x" }, 1] }, |
| 56 | + { ">": [{ "var": "x" }, 1] } |
| 57 | + ] |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +2. Other operations, such as `eq`, `ne`, `lt`, `gt`, `le`, `ge`, `in`, and others, are formatted to take exactly two operands. Here’s an example of the `eq` operation: |
| 62 | + |
| 63 | +```json |
| 64 | +{ |
| 65 | + "eq": [{ "var": "x" }, 1] |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## How to use |
| 70 | +This example demonstrates how to initialize the parser with a JSON rule and evaluate it against a set of data. Adjust the paths and data as necessary for your specific use case. |
| 71 | + |
| 72 | +```Go |
| 73 | + p := parser.NewParser(filepath.Join("examples", "example.json")) |
| 74 | + err := p.ParseRule() |
| 75 | + if err != nil { |
| 76 | + t.Errorf("%v", err) |
| 77 | + } |
| 78 | + testData := map[string]interface{}{ |
| 79 | + "x" : 1, |
| 80 | + } |
| 81 | + result := p.Evaluate(testData) |
| 82 | +``` |
0 commit comments