Git Product home page Git Product logo

rules's People

Contributors

mstory21 avatar nikunjy avatar sulthonzh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

rules's Issues

Version v2.0.0 not usable

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.

Nested rule not being evaluated correctly

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

rule not passing

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

test.json

{
    "x": {
        "y": {
            "a": 50,
            "b": 2
        },
        "z": "s"
    },
    "name": "myname",
    "age": 15
}

main.go

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))
}

output

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.

Wrong Output

Why is it giving false for

parser.Evaluate("x eq 1", map[string]interface{}{"x": 1})

How can i evaluate over array?

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

Some evaluations not working as expected.

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 ?

How can I parse array?

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?

What would be the best way to build a rule based on date?

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?

it seems antlr library changed

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)

Fix panics

Code has panics. Make things return error and report back an error on Evaluate

rule parse bug

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?

Logical operators OR/AND not working

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.