Documentation
¶
Overview ¶
Package patcher holds the syntax tree patches github.com/SigNoz/baseplate.go/expr applies when it compiles an expression. Each patch is a github.com/expr-lang/expr/ast.Visitor that rewrites nodes in place, as the patches in github.com/expr-lang/expr/patcher do.
NilSafe makes member access return nil instead of failing when the parent is missing or isn't a map. A github.com/SigNoz/baseplate.go/expr.TypedDynamic program applies it.
NilSafeTyped does the same only where the parent is a map or of unknown type, so struct fields keep their types. A github.com/SigNoz/baseplate.go/expr.TypedStatic program applies it.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NilSafe ¶
type NilSafe struct{}
NilSafe rewrites every string-keyed member access a.b, including a["b"], into a call that returns nil unless a is a map[string]any. A name that is missing, or a prefix that holds a plain value, then reads as nil instead of failing the run.
The calls go to the function NilSafe.Function returns, which github.com/SigNoz/baseplate.go/expr registers with the patch.
Example ¶
package main
import (
"fmt"
"github.com/expr-lang/expr/ast"
"github.com/expr-lang/expr/parser"
"github.com/SigNoz/baseplate.go/expr/patcher"
)
func main() {
tree, err := parser.Parse(`service.name == "checkout" && host["name"] != nil`)
if err != nil {
panic(err)
}
ast.Walk(&tree.Node, patcher.NilSafe{})
_, _ = fmt.Println(tree.Node.String())
}
Output: __nilSafeMember(service, "name") == "checkout" && __nilSafeMember(host, "name") != nil
type NilSafeTyped ¶
type NilSafeTyped struct{}
NilSafeTyped rewrites a string-keyed member access a.b, including a["b"], into a call that returns nil when a has no such key, but only where a is a map or of unknown type, such as an any value. Member access on a struct stays as written, so its fields keep their types and a misspelled field stays a compile error. Method calls stay as written too.
The calls go to the function NilSafeTyped.Function returns, which github.com/SigNoz/baseplate.go/expr registers with the patch.
func (NilSafeTyped) Function ¶
func (NilSafeTyped) Function() (string, func(params ...any) (any, error))
Function returns the name and the body of the function the rewritten calls go to. It returns a[key] when a is a map with string keys that has key, and nil otherwise.
func (NilSafeTyped) Visit ¶
func (NilSafeTyped) Visit(node *ast.Node)
Visit rewrites node when it is a string-keyed member access of a map or of a value of unknown type.