Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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.

License

Notifications You must be signed in to change notification settings

microbus-io/boolexp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

2 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

No packages published

Languages

AltStyle によって変換されたページ (->オリジナル) /