patcher

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: AGPL-3.0 Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func (NilSafe) Function

func (NilSafe) 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[string]any, and nil otherwise.

func (NilSafe) Visit

func (NilSafe) Visit(node *ast.Node)

Visit rewrites node when it is a string-keyed member access.

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.

Jump to

Keyboard shortcuts

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