GithubHelp home page GithubHelp logo

rules's Introduction

Go Report Card PkgGoDev Release Release Maintenance License GitHub stars

Golang Rules Engine

Rules engine written in golang with the help of antlr.

This package will be very helpful in situations where you have a generic rule and want to verify if your values (specified using map[string]interface{}) satisfy the rule.

Here are some examples:

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

  parser.Evaluate("x.a == 1 and x.b.c <= 2", map[string]interface{}{
    "x": map[string]interface{}{
       "a": 1,
       "b": map[string]interface{}{
          "c": 2,
       },
    },
  })


  parser.Evaluate("y == 4 and (x > 1)", map[string]interface{}{"x": 1})

  parser.Evaluate("y == 4 and (x IN [1,2,3])", map[string]interface{}{"x": 1})

  parser.Evaluate("y == 4 and (x eq 1.2.3)", map[string]interface{}{"x": "1.2.3"})

Operations

All the operations can be written capitalized or lowercase (ex: eq or EQ can be used)

Logical Operations supported are and or

Compare Expression and their definitions

expression meaning
eq equals to
== equals to
ne not equal to
!= not equal to
lt less than
< less than
gt greater than
> greater than
le less than or equal to
<= less than or equal to
ge greater than or equal to
>= greater than or equal to
co contains
sw starts with
ew ends with
in in a list
pr present, will be true if you have a key as true
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

How to extend the grammar

  1. Please look at this antlr tutorial, the link will show you how to setup antlr. The article has a whole lot of detail about antlr I encourage you to read it, you might also like my blog post about this repo.
  2. After taking a look at the antlr tutorial, you can extend the JsonQuery.g4 file.
  3. Compile the parser antlr4 -Dlanguage=Go -visitor -no-listener JsonQuery.g4 -o ./ (Note: -o is the output directory, make sure all the stuff it generates is in the parser directory of the root repo folder)

rules's People

Contributors

nikunjy avatar mstory21 avatar sulthonzh avatar

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.