boolexp

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

README

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)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eval

func Eval(boolExp string, symbols any) (bool, error)

Eval evaluates a boolean expression against a set of key-value pairs which can be provided as a map or a struct.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL