Skip to content

microbus-io/boolexp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Boolean Expression Evaluator

The boolexp module is a utility for evaluating a boolean expression such as foo=='bar' && (y==3 || y<0) against a set of key-value pairs in the form of a map[string]any or fields of a struct.

Syntax:

  • Logical operators &&, || and !
  • Comparison operators ==, !=, <, <=, > and >=
  • Regexp operators =~ and !~
  • Parentheses ( and )
  • Constants true and false
  • Dot notation symbol.property
  • Quotation of string literals using ', " or `

Evaluate against a map[string]any:

symbols := map[string]any{
    "i": 5,
    "s": "hello",
    "nested": map[string]any{
        "field": "alphanumeric",
    },
}
isTrue, err := Eval("(i==5 || i==7) && s=='hello' && nested.field=~'^[a-z0-9]+$'", symbols)

Evaluate against an object's fields:

type NestedObj struct {
    Field string `json:"field"`
}
type Obj struct {
    I      int       `json:"i"`
    S      string    `json:"s"`
    Nested NestedObj `json:"nested"`
}
object := Obj{
    I: 5,
    S: "hello",
    Nested: NestedObj{
        Field: "alphanumeric",
    },
}
isTrue, err := Eval("(i==5 || i==7) && s=='hello' && nested.field=~'^[a-z0-9]+$'", object)

About

The boolexp module is a utility for evaluating a boolean expression against a set of key-value pairs in the form of a map[string]any or fields of a struct.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages