Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ JSON Rules is an abstraction layer over the [Golang Rules Engine](https://github

This package allows you to represent rules in JSON format instead of using the original ANTLR query syntax from the nikunjy/rules implementation.

## Example Queries

You can find example queries in the [examples/](test/examples/) directory.

## Operations

The following operations are supported, matching those available in the [Golang Rules Engine](https://github.com/nikunjy/rules):
Expand All @@ -33,18 +29,15 @@ The following operations are supported, matching those available in the [Golang
| pr | present, will be true if you have a key as true |
| not | not of a logical expression |

## JSON Rule Example
## JSON Rule Guide

To create a JSON rule, follow these steps:

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:

```json
{
"and": [
{ "==": [{ "var": "y" }, 4] },
{ ">": [{ "var": "x" }, 1] }
]
"and": [{ "==": [{ "var": "y" }, 4] }, { ">": [{ "var": "x" }, 1] }]
}
```

Expand All @@ -66,7 +59,12 @@ To create a JSON rule, follow these steps:
}
```

## Examples

You can find more example json rules in the [examples/](test/examples/) directory.

## How to use

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.

```Go
Expand Down
4 changes: 0 additions & 4 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,4 @@ func (p *Parser) formatRule(node map[string]interface{}) string {

func (p *Parser) Evaluate(items map[string]interface{}) bool {
return parser.Evaluate(p.rule, items)
}

func (p *Parser) GetRule() string {
return p.rule
}
4 changes: 1 addition & 3 deletions test/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package test

import (
"fmt"
"path/filepath"
"testing"

Expand All @@ -16,7 +15,6 @@ func TestEqual(t *testing.T) {
t.Errorf("%v", err)
}

fmt.Println(p.GetRule())
testData := map[string]interface{}{
"x" : 1,
}
Expand Down Expand Up @@ -139,7 +137,7 @@ func TestEqualAndGT(t *testing.T) {
}

result := p.Evaluate(testData)
assert.False(t, result, p.GetRule())
assert.False(t, result)
}


Expand Down
Loading