|
2 | 2 |
|
3 | 3 | JSON Rules is an abstraction layer over the [Golang Rules Engine](https://github.com/nikunjy/rules/blob/master/README.md).
|
4 | 4 |
|
5 |
| -This package allows you to represent rules in JSON format instead of using the original ANTLR query syntax from the nikunjy/rules implementation. |
| 5 | +This package allows you to: |
| 6 | + |
| 7 | +- Define rules using JSON Logic syntax |
| 8 | +- Evaluate complex logical conditions using JSON Logic |
| 9 | +- Convert between JSON Rules and ANTLR query syntax |
| 10 | + |
| 11 | +## How to use |
| 12 | + |
| 13 | +### Evaluating from JSON Logic |
| 14 | + |
| 15 | +This example demonstrates how to initialize the parser with a JSON rule and evaluate it against a data set. You can adjust the paths and data as necessary for your specific use case. |
| 16 | + |
| 17 | +```Go |
| 18 | + import "github.com/ahuangg/json-rules/parser" |
| 19 | + |
| 20 | + p := parser.NewParser(filepath.Join("examples", "example.json")) |
| 21 | + err := p.ParseRule() |
| 22 | + if err != nil { |
| 23 | + t.Errorf("%v", err) |
| 24 | + } |
| 25 | + testData := map[string]interface{}{ |
| 26 | + "x" : 1, |
| 27 | + } |
| 28 | + result := p.Evaluate(testData) |
| 29 | +``` |
| 30 | + |
| 31 | +### Converting between rule formats |
| 32 | + |
| 33 | +This example demonstrates how you to convert between the two different rule formats |
| 34 | + |
| 35 | +```Go |
| 36 | + import "github.com/ahuangg/json-rules/converter" |
| 37 | + |
| 38 | + // convert JSON logic to expression |
| 39 | + expression, err := converter.JSONToExpression(jsonLogic) |
| 40 | + |
| 41 | + //convert expression to JSON logic |
| 42 | + jsonLogic, err := converter.ExpressionToJSON(expression) |
| 43 | +``` |
6 | 44 |
|
7 | 45 | ## Operations
|
8 | 46 |
|
@@ -61,20 +99,4 @@ To create a JSON rule, follow these steps:
|
61 | 99 |
|
62 | 100 | ## Examples
|
63 | 101 |
|
64 |
| -You can find more example json rules in the [examples/](test/examples/) directory. |
65 |
| - |
66 |
| -## How to use |
67 |
| - |
68 |
| -This example demonstrates how to initialize the parser with a JSON rule and evaluate it against a data set. You can adjust the paths and data as necessary for your specific use case. |
69 |
| - |
70 |
| -```Go |
71 |
| - p := parser.NewParser(filepath.Join("examples", "example.json")) |
72 |
| - err := p.ParseRule() |
73 |
| - if err != nil { |
74 |
| - t.Errorf("%v", err) |
75 |
| - } |
76 |
| - testData := map[string]interface{}{ |
77 |
| - "x" : 1, |
78 |
| - } |
79 |
| - result := p.Evaluate(testData) |
80 |
| -``` |
| 102 | +You can find more example JSON rule format in the [examples/](test/examples/) directory. |
0 commit comments