Documentation
¶
Overview ¶
Package expression provides a small, typed, reflection-free expression language for compiler-validated framework policies.
Expressions can use explicitly declared Boolean and string variables and functions. They support !, &&, ||, ==, !=, literals, calls, and parentheses. There is no property traversal, bean lookup, method invocation, assignment, allocation, I/O, or access to ambient process state.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CompileError ¶
CompileError is a source-positioned expression failure. Offset is a zero-based UTF-8 byte offset into the expression literal.
func (*CompileError) Error ¶
func (err *CompileError) Error() string
Error renders a bounded message without including the expression source.
type Function ¶
Function evaluates one declared function. Arguments and results are checked against the compiled schema before values enter or leave the function.
type FunctionSpec ¶
FunctionSpec declares one positional function available to an expression.
type Inputs ¶
Inputs binds variables and functions in the declaration order of the schema passed to Compile. Slices are read only for the duration of Evaluate.
type Kind ¶
type Kind uint8
Kind identifies one expression value type.
const ( // Boolean identifies a Boolean expression value. Boolean Kind // String identifies a string expression value. String )
type Program ¶
type Program struct {
// contains filtered or unexported fields
}
Program is an immutable compiled Boolean expression.
func Compile ¶
Compile parses and type-checks one Boolean expression against an explicit schema. The schema and source are defensively copied into the returned immutable program.
Example ¶
program, err := Compile(`enabled && hasLabel("stable")`, Schema{
Variables: []Variable{{Name: "enabled", Kind: Boolean}},
Functions: []FunctionSpec{{
Name: "hasLabel",
Parameters: []Kind{String},
Result: Boolean,
}},
})
if err != nil {
fmt.Println(err)
return
}
allowed, err := program.Evaluate(context.Background(), Inputs{
Variables: []Value{Bool(true)},
Functions: []Function{func(_ context.Context, arguments []Value) (Value, error) {
label, _ := arguments[0].StringValue()
return Bool(label == "stable"), nil
}},
})
fmt.Println(allowed, err)
Output: true <nil>
type Schema ¶
type Schema struct {
Variables []Variable
Functions []FunctionSpec
}
Schema is the complete immutable symbol surface accepted by Compile. Declaration order becomes the positional runtime binding order.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is one immutable typed expression value.
func (Value) BooleanValue ¶
BooleanValue returns a Boolean value and whether its type matches.
func (Value) StringValue ¶
StringValue returns a string value and whether its type matches.