expr

package
v0.0.0-...-5f38cae Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompileAnyExpression

func CompileAnyExpression(s string) (*vm.Program, error)

func CompileBoolExpression

func CompileBoolExpression(s string) (*vm.Program, error)

CompileBoolExpression compiles an expression and returns the program. It is used for expressions that return bool. The exprContext is used to provide the context for the expression evaluation. Not safe for concurrent use.

func CompileStringExpression

func CompileStringExpression(s string) (*vm.Program, error)

CompileStringExpression compiles an expression and returns the program. It is used for expressions that return strings The exprContext is used to provide the context for the expression evaluation. Not safe for concurrent use.

func ResolveBoolExpression

func ResolveBoolExpression(vm *vm.Program, ctx Context) (bool, error)

ResolveBoolExpression evaluates the expression and returns the result as a bool. The exprContext is used to provide the context for the expression evaluation. Not safe for concurrent use.

func ResolveStringExpression

func ResolveStringExpression(vm *vm.Program, ctx Context) (string, error)

ResolveStringExpression evaluates the expression and returns the result as a string. The exprContext is used to provide the context for the expression evaluation. Not safe for concurrent use.

Types

type Context

type Context struct {
	Request Request `expr:"request"`
}

Context is the context for expressions parser when evaluating dynamic expressions

type Request

type Request struct {
	Auth   RequestAuth    `expr:"auth"`
	URL    RequestURL     `expr:"url"`
	Header RequestHeaders `expr:"header"`
}

Request is the context for the request object in expressions. Be aware, that only value receiver methods are exported in the expr environment. This is because the expressions are evaluated in a read-only context.

func LoadRequest

func LoadRequest(req *http.Request) Request

LoadRequest loads the request object into the context.

type RequestAuth

type RequestAuth struct {
	IsAuthenticated bool           `expr:"isAuthenticated"`
	Type            string         `expr:"type"`
	Claims          map[string]any `expr:"claims"`
	Scopes          []string       `expr:"scopes"`
}

func LoadAuth

func LoadAuth(ctx context.Context) RequestAuth

LoadAuth loads the authentication context into the request object. Must only be called when the authentication was successful.

type RequestHeaders

type RequestHeaders struct {
	Header http.Header `expr:"-"` // Do not expose the full header
}

func (RequestHeaders) Get

func (r RequestHeaders) Get(key string) string

Get returns the value of the header with the given key. If the header is not present, an empty string is returned. The key is case-insensitive and transformed to the canonical format. TODO: Use interface to expose only the required methods. Blocked by https://github.com/expr-lang/expr/issues/744

type RequestURL

type RequestURL struct {
	Method string `expr:"method"`
	// Scheme is the scheme of the URL
	Scheme string `expr:"scheme"`
	// Host is the host of the URL
	Host string `expr:"host"`
	// Path is the path of the URL
	Path string `expr:"path"`
	// Query is the parsed query parameters
	Query map[string]string `expr:"query"`
}

RequestURL is the context for the URL object in expressions it is limited in scope to the URL object and its components. For convenience, the query parameters are parsed.

Jump to

Keyboard shortcuts

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