nikunjy / rules Goto Github PK
View Code? Open in Web Editor NEWGeneric Rules engine in golang
License: MIT License
Generic Rules engine in golang
License: MIT License
How Can I add my own custom operator .
Then I Can extend this rule engine more powerful.
I want to create a rule like that
country !=IN ["MX","mx"]
Hello,
I was trying to use version v2.0.0
of the lib but it is not possible to use it for now.
On a clean project when I do go get github.com/nikunjy/rules
it downloads the version v1.1.0
.
go: added github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220415221154-79c36419192d
go: added github.com/blang/semver v3.5.1+incompatible
go: added github.com/nikunjy/rules v1.1.0
And when I try to force the go get of the version v2.0.0
I have an error:
> go get github.com/nikunjy/[email protected]
go: github.com/nikunjy/[email protected]: invalid version: module contains a go.mod file, so module path must match major version ("github.com/nikunjy/rules/v2")
After reading here and there it seems that the issue is caused because your version v2
is not following the GO convention.
Look at this article to know more.
I believe this is happening because you have main.go#main in your repository.
Why is this nested rule returning false
?
func TestNestedRule(t *testing.T) {
// given a rule
rule := "x.a eq 0"
// given an instance to check
type obj map[string]interface{}
instance := obj{
"x": obj{
"a": 0,
},
}
// when
o := parser.Evaluate(rule, instance)
// then
if !o {
t.Errorf("This rule '%v', does not match this instance '%v'", rule, instance)
}
}
When I run this test with go test -v -count=1 -run TestNestedRule
(with the rule being either "x.a eq 0"
or "x.a == 0"
), then I see this:
=== RUN TestNestedRule
TestNestedRule: go_nikunjy_rules_test.go:80: This rule 'x.a eq 0', does not match this instance 'map[x:map[a:0]]'
--- FAIL: TestNestedRule (0.00s)
FAIL
exit status 1
FAIL oerules 0.003s
My go.mod
shows: github.com/nikunjy/rules v0.0.0-20200120082459-0b7c4dc9dc86
think this is a great package @nikunjy however i have found an issue :(
rule engine failing a condition
rule:
name == "myname" and x.y.b == 2
{
"x": {
"y": {
"a": 50,
"b": 2
},
"z": "s"
},
"name": "myname",
"age": 15
}
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/nikunjy/rules/parser"
)
type obj map[string]interface{}
func main() {
jsonFile, _ := ioutil.ReadFile("test.json")
rulesBytes, _ := ioutil.ReadFile("rules.txt")
var rulesString = string(rulesBytes)
var info map[string]interface{}
json.Unmarshal([]byte(jsonFile), &info)
fmt.Println(rulesString, info, parser.Evaluate(rulesString, info))
}
name == "myname" and x.y.b == 2 map[x:map[y:map[a:50 b:2] z:s] name:myname age:15] false
should be
name == "myname" and x.y.b == 2 map[x:map[y:map[a:50 b:2] z:s] name:myname age:15] true
since name is myname and x.y.b equals 2. also doesnt work for any other combination.
Why is it giving false for
parser.Evaluate("x eq 1", map[string]interface{}{"x": 1})
Hello,
how can i evaluate the value inside array?
maybe for some object like this
{
"data": [
{ "key": "val1" },
{ "key": "val2" },
...
]
}
evaluate the object like this
data.key == "val1"
Thanks in advance
The documentation is not very clear and this is what I have done and it is not really working.
m := make(map[string]interface{})
m["bankcode"] = "CIT"
rule := "bankcode eq CIT"
fmt.Println("Rule Passed ? : ", parser.Evaluate(rule,m))
output : Rule Passed ? : false
This should not happen, right ?
Hi, nikunjy
Thank you for the great package.
I have a question. I want to evaluate array.
ex )
{
"class":"A",
"student:[{
"name","jon",
"age":"30"
},
{
"name":"park",
"age":"31"
}
]}
rule := "student[0].name == "json" and student[0].age < "31""
parser.Evaluate(rule, obj)
-> I want true result but false.
How can I parse array?
Hi,
I'm using this amazing engine in a project that need to apply rules based on date.
I'm doing rules like this:
"CurrentDate < 1610016640 and CurrentDate > 1110016640"
It works but the unix epoch representation is not as friendly as the iso8601 an I can't manage Timezone.
Any advice to use it with iso8601 date format?
I am receiving this exception and it seems antlr dependency has updated and deprecated/removed this method.
jsonquery_lexer.go:152:34: lexerDeserializer.DeserializeFromUInt16 undefined (type *antlr.ATNDeserializer has no field or method DeserializeFromUInt16)
Code has panics. Make things return error and report back an error on Evaluate
the below return false
case1:
parser.Evaluate("x < 2 and ( y > 4 or z == true )", map[string]interface{}{ "x": 1, "y": 3, "z": true, }, )
and the below return true
case2:
parser.Evaluate("x < 2 and ( y > 4 or z == true)", map[string]interface{}{ "x": 1, "y": 3, "z": true, }, )
in case 1, the end of the rule contains ' ', and this is bug?
It would be great if basic mathematical operations can be accommodated in the grammar.
E.g.
x+4 eq 10
x%20 eq 1
The following are supposed to be working according to README.md but not. @nikunjy
parser.Evaluate("x eq 1 OR x eq 2", map[string]interface{}{"x": 1})
It would be nice to support ||
and &&
as equivalent of and
and or
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.