Pin new dependency: github.com/google/cel-go v0.9.0

This commit is contained in:
Joe Betz
2021-11-01 14:08:09 -04:00
parent 91ff1f9840
commit d73403dc12
304 changed files with 48716 additions and 995 deletions

View File

@@ -0,0 +1,20 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
package(
default_visibility = ["//visibility:public"],
licenses = ["notice"], # Apache 2.0
)
go_library(
name = "go_default_library",
srcs = [
"decls.go",
"scopes.go",
],
importpath = "github.com/google/cel-go/checker/decls",
deps = [
"@org_golang_google_genproto//googleapis/api/expr/v1alpha1:go_default_library",
"@org_golang_google_protobuf//types/known/emptypb:go_default_library",
"@org_golang_google_protobuf//types/known/structpb:go_default_library",
],
)

231
vendor/github.com/google/cel-go/checker/decls/decls.go generated vendored Normal file
View File

@@ -0,0 +1,231 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package decls provides helpers for creating variable and function declarations.
package decls
import (
emptypb "google.golang.org/protobuf/types/known/emptypb"
structpb "google.golang.org/protobuf/types/known/structpb"
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
)
var (
// Error type used to communicate issues during type-checking.
Error = &exprpb.Type{
TypeKind: &exprpb.Type_Error{
Error: &emptypb.Empty{}}}
// Dyn is a top-type used to represent any value.
Dyn = &exprpb.Type{
TypeKind: &exprpb.Type_Dyn{
Dyn: &emptypb.Empty{}}}
)
// Commonly used types.
var (
Bool = NewPrimitiveType(exprpb.Type_BOOL)
Bytes = NewPrimitiveType(exprpb.Type_BYTES)
Double = NewPrimitiveType(exprpb.Type_DOUBLE)
Int = NewPrimitiveType(exprpb.Type_INT64)
Null = &exprpb.Type{
TypeKind: &exprpb.Type_Null{
Null: structpb.NullValue_NULL_VALUE}}
String = NewPrimitiveType(exprpb.Type_STRING)
Uint = NewPrimitiveType(exprpb.Type_UINT64)
)
// Well-known types.
// TODO: Replace with an abstract type registry.
var (
Any = NewWellKnownType(exprpb.Type_ANY)
Duration = NewWellKnownType(exprpb.Type_DURATION)
Timestamp = NewWellKnownType(exprpb.Type_TIMESTAMP)
)
// NewAbstractType creates an abstract type declaration which references a proto
// message name and may also include type parameters.
func NewAbstractType(name string, paramTypes ...*exprpb.Type) *exprpb.Type {
return &exprpb.Type{
TypeKind: &exprpb.Type_AbstractType_{
AbstractType: &exprpb.Type_AbstractType{
Name: name,
ParameterTypes: paramTypes}}}
}
// NewFunctionType creates a function invocation contract, typically only used
// by type-checking steps after overload resolution.
func NewFunctionType(resultType *exprpb.Type,
argTypes ...*exprpb.Type) *exprpb.Type {
return &exprpb.Type{
TypeKind: &exprpb.Type_Function{
Function: &exprpb.Type_FunctionType{
ResultType: resultType,
ArgTypes: argTypes}}}
}
// NewFunction creates a named function declaration with one or more overloads.
func NewFunction(name string,
overloads ...*exprpb.Decl_FunctionDecl_Overload) *exprpb.Decl {
return &exprpb.Decl{
Name: name,
DeclKind: &exprpb.Decl_Function{
Function: &exprpb.Decl_FunctionDecl{
Overloads: overloads}}}
}
// NewIdent creates a named identifier declaration with an optional literal
// value.
//
// Literal values are typically only associated with enum identifiers.
//
// Deprecated: Use NewVar or NewConst instead.
func NewIdent(name string, t *exprpb.Type, v *exprpb.Constant) *exprpb.Decl {
return &exprpb.Decl{
Name: name,
DeclKind: &exprpb.Decl_Ident{
Ident: &exprpb.Decl_IdentDecl{
Type: t,
Value: v}}}
}
// NewConst creates a constant identifier with a CEL constant literal value.
func NewConst(name string, t *exprpb.Type, v *exprpb.Constant) *exprpb.Decl {
return NewIdent(name, t, v)
}
// NewVar creates a variable identifier.
func NewVar(name string, t *exprpb.Type) *exprpb.Decl {
return NewIdent(name, t, nil)
}
// NewInstanceOverload creates a instance function overload contract.
// First element of argTypes is instance.
func NewInstanceOverload(id string, argTypes []*exprpb.Type,
resultType *exprpb.Type) *exprpb.Decl_FunctionDecl_Overload {
return &exprpb.Decl_FunctionDecl_Overload{
OverloadId: id,
ResultType: resultType,
Params: argTypes,
IsInstanceFunction: true}
}
// NewListType generates a new list with elements of a certain type.
func NewListType(elem *exprpb.Type) *exprpb.Type {
return &exprpb.Type{
TypeKind: &exprpb.Type_ListType_{
ListType: &exprpb.Type_ListType{
ElemType: elem}}}
}
// NewMapType generates a new map with typed keys and values.
func NewMapType(key *exprpb.Type, value *exprpb.Type) *exprpb.Type {
return &exprpb.Type{
TypeKind: &exprpb.Type_MapType_{
MapType: &exprpb.Type_MapType{
KeyType: key,
ValueType: value}}}
}
// NewObjectType creates an object type for a qualified type name.
func NewObjectType(typeName string) *exprpb.Type {
return &exprpb.Type{
TypeKind: &exprpb.Type_MessageType{
MessageType: typeName}}
}
// NewOverload creates a function overload declaration which contains a unique
// overload id as well as the expected argument and result types. Overloads
// must be aggregated within a Function declaration.
func NewOverload(id string, argTypes []*exprpb.Type,
resultType *exprpb.Type) *exprpb.Decl_FunctionDecl_Overload {
return &exprpb.Decl_FunctionDecl_Overload{
OverloadId: id,
ResultType: resultType,
Params: argTypes,
IsInstanceFunction: false}
}
// NewParameterizedInstanceOverload creates a parametric function instance overload type.
func NewParameterizedInstanceOverload(id string,
argTypes []*exprpb.Type,
resultType *exprpb.Type,
typeParams []string) *exprpb.Decl_FunctionDecl_Overload {
return &exprpb.Decl_FunctionDecl_Overload{
OverloadId: id,
ResultType: resultType,
Params: argTypes,
TypeParams: typeParams,
IsInstanceFunction: true}
}
// NewParameterizedOverload creates a parametric function overload type.
func NewParameterizedOverload(id string,
argTypes []*exprpb.Type,
resultType *exprpb.Type,
typeParams []string) *exprpb.Decl_FunctionDecl_Overload {
return &exprpb.Decl_FunctionDecl_Overload{
OverloadId: id,
ResultType: resultType,
Params: argTypes,
TypeParams: typeParams,
IsInstanceFunction: false}
}
// NewPrimitiveType creates a type for a primitive value. See the var declarations
// for Int, Uint, etc.
func NewPrimitiveType(primitive exprpb.Type_PrimitiveType) *exprpb.Type {
return &exprpb.Type{
TypeKind: &exprpb.Type_Primitive{
Primitive: primitive}}
}
// NewTypeType creates a new type designating a type.
func NewTypeType(nested *exprpb.Type) *exprpb.Type {
if nested == nil {
// must set the nested field for a valid oneof option
nested = &exprpb.Type{}
}
return &exprpb.Type{
TypeKind: &exprpb.Type_Type{
Type: nested}}
}
// NewTypeParamType creates a type corresponding to a named, contextual parameter.
func NewTypeParamType(name string) *exprpb.Type {
return &exprpb.Type{
TypeKind: &exprpb.Type_TypeParam{
TypeParam: name}}
}
// NewWellKnownType creates a type corresponding to a protobuf well-known type
// value.
func NewWellKnownType(wellKnown exprpb.Type_WellKnownType) *exprpb.Type {
return &exprpb.Type{
TypeKind: &exprpb.Type_WellKnown{
WellKnown: wellKnown}}
}
// NewWrapperType creates a wrapped primitive type instance. Wrapped types
// are roughly equivalent to a nullable, or optionally valued type.
func NewWrapperType(wrapped *exprpb.Type) *exprpb.Type {
primitive := wrapped.GetPrimitive()
if primitive == exprpb.Type_PRIMITIVE_TYPE_UNSPECIFIED {
// TODO: return an error
panic("Wrapped type must be a primitive")
}
return &exprpb.Type{
TypeKind: &exprpb.Type_Wrapper{
Wrapper: primitive}}
}

115
vendor/github.com/google/cel-go/checker/decls/scopes.go generated vendored Normal file
View File

@@ -0,0 +1,115 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package decls
import exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
// Scopes represents nested Decl sets where the Scopes value contains a Groups containing all
// identifiers in scope and an optional parent representing outer scopes.
// Each Groups value is a mapping of names to Decls in the ident and function namespaces.
// Lookups are performed such that bindings in inner scopes shadow those in outer scopes.
type Scopes struct {
parent *Scopes
scopes *Group
}
// NewScopes creates a new, empty Scopes.
// Some operations can't be safely performed until a Group is added with Push.
func NewScopes() *Scopes {
return &Scopes{
scopes: newGroup(),
}
}
// Push creates a new Scopes value which references the current Scope as its parent.
func (s *Scopes) Push() *Scopes {
return &Scopes{
parent: s,
scopes: newGroup(),
}
}
// Pop returns the parent Scopes value for the current scope, or the current scope if the parent
// is nil.
func (s *Scopes) Pop() *Scopes {
if s.parent != nil {
return s.parent
}
// TODO: Consider whether this should be an error / panic.
return s
}
// AddIdent adds the ident Decl in the current scope.
// Note: If the name collides with an existing identifier in the scope, the Decl is overwritten.
func (s *Scopes) AddIdent(decl *exprpb.Decl) {
s.scopes.idents[decl.Name] = decl
}
// FindIdent finds the first ident Decl with a matching name in Scopes, or nil if one cannot be
// found.
// Note: The search is performed from innermost to outermost.
func (s *Scopes) FindIdent(name string) *exprpb.Decl {
if ident, found := s.scopes.idents[name]; found {
return ident
}
if s.parent != nil {
return s.parent.FindIdent(name)
}
return nil
}
// FindIdentInScope finds the first ident Decl with a matching name in the current Scopes value, or
// nil if one does not exist.
// Note: The search is only performed on the current scope and does not search outer scopes.
func (s *Scopes) FindIdentInScope(name string) *exprpb.Decl {
if ident, found := s.scopes.idents[name]; found {
return ident
}
return nil
}
// AddFunction adds the function Decl to the current scope.
// Note: Any previous entry for a function in the current scope with the same name is overwritten.
func (s *Scopes) AddFunction(fn *exprpb.Decl) {
s.scopes.functions[fn.Name] = fn
}
// FindFunction finds the first function Decl with a matching name in Scopes.
// The search is performed from innermost to outermost.
// Returns nil if no such function in Scopes.
func (s *Scopes) FindFunction(name string) *exprpb.Decl {
if fn, found := s.scopes.functions[name]; found {
return fn
}
if s.parent != nil {
return s.parent.FindFunction(name)
}
return nil
}
// Group is a set of Decls that is pushed on or popped off a Scopes as a unit.
// Contains separate namespaces for idenifier and function Decls.
// (Should be named "Scope" perhaps?)
type Group struct {
idents map[string]*exprpb.Decl
functions map[string]*exprpb.Decl
}
func newGroup() *Group {
return &Group{
idents: make(map[string]*exprpb.Decl),
functions: make(map[string]*exprpb.Decl),
}
}