package ast
import "github.com/google/cel-go/common/ast"
Package ast declares data structures useful for parsed and checked abstract syntax trees
Index ¶
- func CheckedASTToCheckedExpr(ast *CheckedAST) (*exprpb.CheckedExpr, error)
- func ConstantToVal(c *exprpb.Constant) (ref.Val, error)
- func ReferenceInfoToReferenceExpr(info *ReferenceInfo) (*exprpb.Reference, error)
- func ValToConstant(v ref.Val) (*exprpb.Constant, error)
- type CheckedAST
- type ExprKind
- type ExprMatcher
- func AllMatcher() ExprMatcher
- func ConstantValueMatcher() ExprMatcher
- func FunctionMatcher(funcName string) ExprMatcher
- func KindMatcher(kind ExprKind) ExprMatcher
- type NavigableCallExpr
- type NavigableComprehensionExpr
- type NavigableEntry
- type NavigableExpr
- func MatchDescendants(expr NavigableExpr, matcher ExprMatcher) []NavigableExpr
- func MatchSubset(exprs []NavigableExpr, matcher ExprMatcher) []NavigableExpr
- func NavigateCheckedAST(ast *CheckedAST) NavigableExpr
- type NavigableField
- type NavigableListExpr
- type NavigableMapExpr
- type NavigableSelectExpr
- type NavigableStructExpr
- type ReferenceInfo
- func NewFunctionReference(overloads ...string) *ReferenceInfo
- func NewIdentReference(name string, value ref.Val) *ReferenceInfo
- func ReferenceExprToReferenceInfo(ref *exprpb.Reference) (*ReferenceInfo, error)
- func (r *ReferenceInfo) AddOverload(overloadID string)
- func (r *ReferenceInfo) Equals(other *ReferenceInfo) bool
Functions ¶
func CheckedASTToCheckedExpr ¶
func CheckedASTToCheckedExpr(ast *CheckedAST) (*exprpb.CheckedExpr, error)
CheckedASTToCheckedExpr converts a CheckedAST to a CheckedExpr protobouf.
func ConstantToVal ¶
ConstantToVal converts a protobuf Constant to a CEL-native ref.Val.
func ReferenceInfoToReferenceExpr ¶
func ReferenceInfoToReferenceExpr(info *ReferenceInfo) (*exprpb.Reference, error)
ReferenceInfoToReferenceExpr converts a ReferenceInfo instance to a protobuf Reference suitable for serialization.
func ValToConstant ¶
ValToConstant converts a CEL-native ref.Val to a protobuf Constant.
Only simple scalar types are supported by this method.
Types ¶
type CheckedAST ¶
type CheckedAST struct { Expr *exprpb.Expr SourceInfo *exprpb.SourceInfo TypeMap map[int64]*types.Type ReferenceMap map[int64]*ReferenceInfo }
CheckedAST contains a protobuf expression and source info along with CEL-native type and reference information.
func CheckedExprToCheckedAST ¶
func CheckedExprToCheckedAST(checked *exprpb.CheckedExpr) (*CheckedAST, error)
CheckedExprToCheckedAST converts a CheckedExpr protobuf to a CheckedAST instance.
type ExprKind ¶
type ExprKind int
ExprKind represents the expression node kind.
const ( // UnspecifiedKind represents an unset expression with no specified properties. UnspecifiedKind ExprKind = iota // LiteralKind represents a primitive scalar literal. LiteralKind // IdentKind represents a simple variable, constant, or type identifier. IdentKind // SelectKind represents a field selection expression. SelectKind // CallKind represents a function call. CallKind // ListKind represents a list literal expression. ListKind // MapKind represents a map literal expression. MapKind // StructKind represents a struct literal expression. StructKind // ComprehensionKind represents a comprehension expression generated by a macro. ComprehensionKind )
type ExprMatcher ¶
type ExprMatcher func(NavigableExpr) bool
ExprMatcher takes a NavigableExpr in and indicates whether the value is a match.
This function type should be use with the `Match` and `MatchList` calls.
func AllMatcher ¶
func AllMatcher() ExprMatcher
AllMatcher returns true for all descendants of a NavigableExpr, effectively flattening them into a list.
Such a result would work well with subsequent MatchList calls.
func ConstantValueMatcher ¶
func ConstantValueMatcher() ExprMatcher
ConstantValueMatcher returns an ExprMatcher which will return true if the input NavigableExpr is comprised of all constant values, such as a simple literal or even list and map literal.
func FunctionMatcher ¶
func FunctionMatcher(funcName string) ExprMatcher
FunctionMatcher returns an ExprMatcher which will match NavigableExpr nodes of CallKind type whose function name is equal to `funcName`.
func KindMatcher ¶
func KindMatcher(kind ExprKind) ExprMatcher
KindMatcher returns an ExprMatcher which will return true if the input NavigableExpr.Kind() matches the specified `kind`.
type NavigableCallExpr ¶
type NavigableCallExpr interface { // FunctionName returns the name of the function. () string // Target returns the target of the expression if one is present. () NavigableExpr // Args returns the list of call arguments, excluding the target. () []NavigableExpr // ReturnType returns the result type of the call. () *types.Type // contains filtered or unexported methods }
NavigableCallExpr defines an interface for inspecting a function call and its arugments.
type NavigableComprehensionExpr ¶
type NavigableComprehensionExpr interface { // IterRange returns the iteration range expression. () NavigableExpr // IterVar returns the iteration variable name. () string // AccuVar returns the accumulation variable name. () string // AccuInit returns the accumulation variable initialization expression. () NavigableExpr // LoopCondition returns the loop condition expression. () NavigableExpr // LoopStep returns the loop step expression. () NavigableExpr // Result returns the comprehension result expression. () NavigableExpr // contains filtered or unexported methods }
NavigableComprehensionExpr defines an interface for inspecting a comprehension expression.
type NavigableEntry ¶
type NavigableEntry interface { // Key returns the map entry key expression. () NavigableExpr // Value returns the map entry value expression. () NavigableExpr // IsOptional returns whether the entry is optional. () bool // contains filtered or unexported methods }
NavigableEntry defines an interface for inspecting a map entry.
type NavigableExpr ¶
type NavigableExpr interface { // ID of the expression as it appears in the AST () int64 // Kind of the expression node. See ExprKind for the valid enum values. () ExprKind // Type of the expression node. () *types.Type // Parent returns the parent expression node, if one exists. () (NavigableExpr, bool) // Children returns a list of child expression nodes. () []NavigableExpr // ToExpr adapts this NavigableExpr to a protobuf representation. () *exprpb.Expr // AsCall adapts the expr into a NavigableCallExpr // // The Kind() must be equal to a CallKind for the conversion to be well-defined. () NavigableCallExpr // AsComprehension adapts the expr into a NavigableComprehensionExpr. // // The Kind() must be equal to a ComprehensionKind for the conversion to be well-defined. () NavigableComprehensionExpr // AsIdent adapts the expr into an identifier string. // // The Kind() must be equal to an IdentKind for the conversion to be well-defined. () string // AsLiteral adapts the expr into a constant ref.Val. // // The Kind() must be equal to a LiteralKind for the conversion to be well-defined. () ref.Val // AsList adapts the expr into a NavigableListExpr. // // The Kind() must be equal to a ListKind for the conversion to be well-defined. () NavigableListExpr // AsMap adapts the expr into a NavigableMapExpr. // // The Kind() must be equal to a MapKind for the conversion to be well-defined. () NavigableMapExpr // AsSelect adapts the expr into a NavigableSelectExpr. // // The Kind() must be equal to a SelectKind for the conversion to be well-defined. () NavigableSelectExpr // AsStruct adapts the expr into a NavigableStructExpr. // // The Kind() must be equal to a StructKind for the conversion to be well-defined. () NavigableStructExpr // contains filtered or unexported methods }
NavigableExpr represents the base navigable expression value.
Depending on the `Kind()` value, the NavigableExpr may be converted to a concrete expression types as indicated by the `As<Kind>` methods.
NavigableExpr values and their concrete expression types should be nil-safe. Conversion of an expr to the wrong kind should produce a nil value.
func MatchDescendants ¶
func MatchDescendants(expr NavigableExpr, matcher ExprMatcher) []NavigableExpr
MatchDescendants takes a NavigableExpr and ExprMatcher and produces a list of NavigableExpr values of the descendants which match.
func MatchSubset ¶
func MatchSubset(exprs []NavigableExpr, matcher ExprMatcher) []NavigableExpr
MatchSubset applies an ExprMatcher to a list of NavigableExpr values and their descendants, producing a subset of NavigableExpr values which match.
func NavigateCheckedAST ¶
func NavigateCheckedAST(ast *CheckedAST) NavigableExpr
NavigateCheckedAST converts a CheckedAST to a NavigableExpr
type NavigableField ¶
type NavigableField interface { // FieldName returns the name of the field. () string // Value returns the field initialization expression. () NavigableExpr // IsOptional returns whether the field is optional. () bool // contains filtered or unexported methods }
NavigableField defines an interface for inspecting a struct field initialization.
type NavigableListExpr ¶
type NavigableListExpr interface { // Elements returns the list elements as navigable expressions. () []NavigableExpr // OptionalIndicies returns the list of optional indices in the list literal. () []int32 // Size returns the number of elements in the list. () int // contains filtered or unexported methods }
NavigableListExpr defines an interface for inspecting a list literal expression.
type NavigableMapExpr ¶
type NavigableMapExpr interface { // Entries returns the map key value pairs as NavigableEntry values. () []NavigableEntry // Size returns the number of entries in the map. () int // contains filtered or unexported methods }
NavigableMapExpr defines an interface for inspecting a map expression.
type NavigableSelectExpr ¶
type NavigableSelectExpr interface { // Operand returns the selection operand expression. () NavigableExpr // FieldName returns the field name being selected from the operand. () string // IsTestOnly indicates whether the select expression is a presence test generated by a macro. () bool // contains filtered or unexported methods }
NavigableSelectExpr defines an interface for inspecting a select expression.
type NavigableStructExpr ¶
type NavigableStructExpr interface { // TypeName returns the struct type name. () string // Fields returns the set of field initializers in the struct expression as NavigableField values. () []NavigableField // contains filtered or unexported methods }
NavigableStructExpr defines an interfaces for inspecting a struct and its field initializers.
type ReferenceInfo ¶
ReferenceInfo contains a CEL native representation of an identifier reference which may refer to either a qualified identifier name, a set of overload ids, or a constant value from an enum.
func NewFunctionReference ¶
func NewFunctionReference(overloads ...string) *ReferenceInfo
NewFunctionReference creates a ReferenceInfo instance for a set of function overloads.
func NewIdentReference ¶
func NewIdentReference(name string, value ref.Val) *ReferenceInfo
NewIdentReference creates a ReferenceInfo instance for an identifier with an optional constant value.
func ReferenceExprToReferenceInfo ¶
func ReferenceExprToReferenceInfo(ref *exprpb.Reference) (*ReferenceInfo, error)
ReferenceExprToReferenceInfo converts a protobuf Reference into a CEL-native ReferenceInfo instance.
func (*ReferenceInfo) AddOverload ¶
func (r *ReferenceInfo) AddOverload(overloadID string)
AddOverload appends a function overload ID to the ReferenceInfo.
func (*ReferenceInfo) Equals ¶
func (r *ReferenceInfo) Equals(other *ReferenceInfo) bool
Equals returns whether two references are identical to each other.
Source Files ¶
- Version
- v0.17.3
- Published
- Aug 19, 2023
- Platform
- js/wasm
- Imports
- 5 packages
- Last checked
- 1 minute ago –
Tools for package owners.