Skip to content

Commit 4ce9cbe

Browse files
authored
Merge pull request #6 from ahuangg/alan-branch
docs(readme): included conversion guide
2 parents 4ca372d + b215846 commit 4ce9cbe

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

README.md

+40-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,45 @@
22

33
JSON Rules is an abstraction layer over the [Golang Rules Engine](https://github.com/nikunjy/rules/blob/master/README.md).
44

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+
```
644

745
## Operations
846

@@ -61,20 +99,4 @@ To create a JSON rule, follow these steps:
6199

62100
## Examples
63101

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

Comments
 (0)