expr

package
v0.0.0-...-cc2a400 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const ExprRequestAuthKey = "auth"
View Source
const ExprRequestKey = "request"

Variables

This section is empty.

Functions

func ResolveAnyExpression

func ResolveAnyExpression(vm *vm.Program, ctx Context) (any, error)

ResolveAnyExpression evaluates the expression and returns the result as a any. 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 Body

type Body struct {
	Raw string `expr:"raw"`
}

type Client

type Client struct {
	Name    string `expr:"name"`
	Version string `expr:"version"`
	IP      string `expr:"ip"`
}

type ClientTrace

type ClientTrace struct {
	ConnectionAcquireDuration float64 `expr:"connAcquireDuration"`
}

type Context

type Context struct {
	Request  Request  `expr:"request"` // if changing the expr tag, the ExprRequestKey should be updated
	Response Response `expr:"response"`
	Subgraph Subgraph `expr:"subgraph"`
}

Context is the context for expressions parser when evaluating dynamic expressions

func (Context) Clone

func (copyCtx Context) Clone() *Context

Clone creates a deep copy of the Context

type Manager

type Manager struct {
	VisitorManager *VisitorGroup
}

func CreateNewExprManager

func CreateNewExprManager() *Manager

func (*Manager) CompileAnyExpression

func (c *Manager) CompileAnyExpression(exprString string, visitors ...ast.Visitor) (*vm.Program, error)

CompileAnyExpression compiles an expression and returns the program for any type. The exprContext is used to provide the context for the expression evaluation. Not safe for concurrent use.

func (*Manager) CompileExpression

func (c *Manager) CompileExpression(exprString string, kind reflect.Kind, visitors ...ast.Visitor) (*vm.Program, error)

CompileExpression compiles an expression and returns the program for the specific type. The exprContext is used to provide the context for the expression evaluation. Not safe for concurrent use.

func (*Manager) ValidateAnyExpression

func (c *Manager) ValidateAnyExpression(s string) error

ValidateAnyExpression compiles the expression to ensure that the expression itself is valid but more importantly it checks if the return type is not nil and is an allowed return type this allows us to ensure that nil and return types such as func or channels are not returned

type Operation

type Operation struct {
	Name string `expr:"name"`
	Type string `expr:"type"`
	Hash string `expr:"hash"`
}

type Request

type Request struct {
	Auth      RequestAuth    `expr:"auth"` // if changing the expr tag, the ExprRequestAuthKey should be updated
	URL       RequestURL     `expr:"url"`
	Header    RequestHeaders `expr:"header"`
	Body      Body           `expr:"body"`
	Trace     Trace          `expr:"trace"`
	Operation Operation      `expr:"operation"`
	Client    Client         `expr:"client"`
	Error     error          `expr:"error"`
}

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.

type Response

type Response struct {
	Body Body `expr:"body"`
}

type Subgraph

type Subgraph struct {
	Id       string           `expr:"id"`
	Name     string           `expr:"name"`
	Request  SubgraphRequest  `expr:"request"`
	Response SubgraphResponse `expr:"response"`
}

Subgraph Related

type SubgraphRequest

type SubgraphRequest struct {
	Error       error       `expr:"error"`
	ClientTrace ClientTrace `expr:"clientTrace"`
}

type SubgraphResponse

type SubgraphResponse struct {
	Body Body `expr:"body"`
}

type Trace

type Trace struct {
	Sampled bool `expr:"sampled"`
}

type UsesBody

type UsesBody struct {
	UsesBody bool
}

func (*UsesBody) Visit

func (v *UsesBody) Visit(baseNode *ast.Node)

type UsesResponseBody

type UsesResponseBody struct {
	UsesResponseBody bool
}

This visitor is used to identify if expressions use response.body

func (*UsesResponseBody) Visit

func (v *UsesResponseBody) Visit(baseNode *ast.Node)

type UsesSubgraph

type UsesSubgraph struct {
	UsesSubgraph bool
}

This visitor is used to identify if expressions should be executed in a subgraph context

func (*UsesSubgraph) Visit

func (v *UsesSubgraph) Visit(baseNode *ast.Node)

type UsesSubgraphResponseBody

type UsesSubgraphResponseBody struct {
	UsesSubgraphResponseBody bool
}

This visitor is used to identify if expressions use subgraph.response.body

func (*UsesSubgraphResponseBody) Visit

func (v *UsesSubgraphResponseBody) Visit(baseNode *ast.Node)

type UsesSubgraphTrace

type UsesSubgraphTrace struct {
	UsesSubgraphTrace bool
}

This visitor is used to identify if we should enable client tracing because clientTrace has been accessed in an expression

func (*UsesSubgraphTrace) Visit

func (v *UsesSubgraphTrace) Visit(baseNode *ast.Node)

type VisitorGroup

type VisitorGroup struct {
	// contains filtered or unexported fields
}

VisitorGroup is a struct that holds all the VisitorManager that are used to compile the expressions We use a separate struct so that the visitor is passed in to places will use it that are in the request chain this means that they don't have access to compiling expressions in a request by default via the expr manager

func (*VisitorGroup) IsRequestBodyUsedInExpressions

func (c *VisitorGroup) IsRequestBodyUsedInExpressions() bool

func (*VisitorGroup) IsResponseBodyUsedInExpressions

func (c *VisitorGroup) IsResponseBodyUsedInExpressions() bool

func (*VisitorGroup) IsSubgraphResponseBodyUsedInExpressions

func (c *VisitorGroup) IsSubgraphResponseBodyUsedInExpressions() bool

func (*VisitorGroup) IsSubgraphTraceUsedInExpressions

func (c *VisitorGroup) IsSubgraphTraceUsedInExpressions() bool

Jump to

Keyboard shortcuts

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