exprs

package
v0.67.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 1 Imported by: 1

Documentation

Overview

Package exprs provides primitives for working with v1alpha1/expr values.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

type Matcher func(exp *expr.Expr) bool

Matcher returns true if the expr matches the predicate.

func MatchAny

func MatchAny(e **expr.Expr) Matcher

MatchAny matches any expr.Expr. The expr is populated in argument e.

func MatchAnyFloat

func MatchAnyFloat(value *float64) Matcher

MatchAnyFloat matches an expr.Constant_DoubleValue with any value. The value of the expr is populated in argument value.

func MatchAnyFunction

func MatchAnyFunction(name *string, args ...Matcher) Matcher

MatchAnyFunction matches an expr.Expr_Call where the provided args matches the function arguments. The name of the function is populated in argument name.

func MatchAnyInt

func MatchAnyInt(value *int64) Matcher

MatchAnyInt matches an expr.Constant_Int64Value with any value. The value of the expr is populated in argument value.

func MatchAnyMember

func MatchAnyMember(operand Matcher, field *string) Matcher

MatchAnyMember matches an expr.Expr_Select where the operand matches the argument operand. The field of the expr is populated in argument field.

func MatchAnyString

func MatchAnyString(value *string) Matcher

MatchAnyString matches an expr.Constant_StringValue with any value. The value of the expr is populated in argument value.

func MatchAnyText

func MatchAnyText(text *string) Matcher

MatchAnyText matches an expr.Expr_Ident with any name. The name of the expr is populated in argument text.

func MatchFloat

func MatchFloat(value float64) Matcher

MatchFloat matches an expr.Constant_DoubleValue with an exact value.

func MatchFunction

func MatchFunction(name string, args ...Matcher) Matcher

MatchFunction matches an expr.Expr_Call where the name of the expr matches argument name, and arguments of the function matches the provided args (length must match).

Example (ValidateResourceNames)
const pattern = "books/{book}"

var walkErr error
walkFn := func(currExpr, _ *expr.Expr) bool {
	var name string
	// match any function expression with name '=' and LHS 'name'
	matcher := MatchFunction(filtering.FunctionEquals, MatchText("name"), MatchAnyString(&name))
	if matcher(currExpr) && !resourcename.Match(pattern, name) {
		walkErr = fmt.Errorf("expected resource name matching '%s' but got '%s'", pattern, name)
		return false
	}
	return true
}

// name = "not a resource name" or name = "books/2"
invalidExpr := filtering.Or(
	filtering.Equals(filtering.Text("name"), filtering.String("not a resource name")),
	filtering.Equals(filtering.Text("name"), filtering.String("books/2")),
)
filtering.Walk(walkFn, invalidExpr)
fmt.Println(walkErr)

// reset
walkErr = nil

// name = "books/1" or name = "books/2"
validExpr := filtering.Or(
	filtering.Equals(filtering.Text("name"), filtering.String("books/1")),
	filtering.Equals(filtering.Text("name"), filtering.String("books/2")),
)
filtering.Walk(walkFn, validExpr)
fmt.Println(walkErr)
Output:

expected resource name matching 'books/{book}' but got 'not a resource name'
<nil>

func MatchInt

func MatchInt(value int64) Matcher

MatchInt matches an expr.Constant_Int64Value with an exact value.

func MatchMember

func MatchMember(operand Matcher, field string) Matcher

MatchMember matches an expr.Expr_Select where the operand matches the argument operand, and the field matches argument field.

func MatchString

func MatchString(s string) Matcher

MatchString matches an expr.Constant_StringValue with an exact value.

func MatchText

func MatchText(text string) Matcher

MatchText matches an expr.Expr_Ident where the name of the ident matches an exact value.

Jump to

Keyboard shortcuts

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