Skip to content

Commit

Permalink
Moving main to its own package (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikunjy authored Jan 4, 2020
1 parent 9c1aef6 commit 824e620
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ pr: present
not: not of a logical expression
```

## How to use it
Use your dependency manager to import `github.com/nikunjy/rules/parser`. This will let you parse a rule and keep the parsed representation around.
Alternatively, you can also use `github.com/nikunjy/rules` directly to call the root `Evaluate(string, map[string]interface{})` method.

I would recommend importing `github.com/nikunjy/rules/parser`

[ci-img]: https://api.travis-ci.org/nikunjy/rules.svg?branch=master
[ci]: https://travis-ci.org/nikunjy/rules
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions evaluate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package rules

import "github.com/nikunjy/rules/parser"

func Evaluate(rule string, items map[string]interface{}) (bool, error) {
ev, err := parser.NewEvaluator(rule)
if err != nil {
return false, err
}
return ev.Process(items)
}
21 changes: 21 additions & 0 deletions evaluate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package rules

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestEvaluateBasic(t *testing.T) {
res, err := Evaluate(`x eq "abc"`, map[string]interface{}{
"x": "abc",
})
require.NoError(t, err)
require.True(t, res)

res, err = Evaluate(`x eq abc`, map[string]interface{}{
"x": "abc",
})
require.Error(t, err)
require.False(t, res)
}

0 comments on commit 824e620

Please sign in to comment.