Skip to content

Commit

Permalink
Unify decls.Type with types.Type (#745)
Browse files Browse the repository at this point in the history
* Unify decls.Type with types.Type
  • Loading branch information
TristonianJones authored Jun 16, 2023
1 parent 10a4b58 commit 1ad5fe6
Show file tree
Hide file tree
Showing 26 changed files with 559 additions and 848 deletions.
86 changes: 43 additions & 43 deletions cel/decls.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,109 +27,109 @@ import (
)

// Kind indicates a CEL type's kind which is used to differentiate quickly between simple and complex types.
type Kind = decls.Kind
type Kind = types.Kind

const (
// DynKind represents a dynamic type. This kind only exists at type-check time.
DynKind Kind = decls.DynKind
DynKind Kind = types.DynKind

// AnyKind represents a google.protobuf.Any type. This kind only exists at type-check time.
AnyKind = decls.AnyKind
AnyKind = types.AnyKind

// BoolKind represents a boolean type.
BoolKind = decls.BoolKind
BoolKind = types.BoolKind

// BytesKind represents a bytes type.
BytesKind = decls.BytesKind
BytesKind = types.BytesKind

// DoubleKind represents a double type.
DoubleKind = decls.DoubleKind
DoubleKind = types.DoubleKind

// DurationKind represents a CEL duration type.
DurationKind = decls.DurationKind
DurationKind = types.DurationKind

// IntKind represents an integer type.
IntKind = decls.IntKind
IntKind = types.IntKind

// ListKind represents a list type.
ListKind = decls.ListKind
ListKind = types.ListKind

// MapKind represents a map type.
MapKind = decls.MapKind
MapKind = types.MapKind

// NullTypeKind represents a null type.
NullTypeKind = decls.NullTypeKind
NullTypeKind = types.NullTypeKind

// OpaqueKind represents an abstract type which has no accessible fields.
OpaqueKind = decls.OpaqueKind
OpaqueKind = types.OpaqueKind

// StringKind represents a string type.
StringKind = decls.StringKind
StringKind = types.StringKind

// StructKind represents a structured object with typed fields.
StructKind = decls.StructKind
StructKind = types.StructKind

// TimestampKind represents a a CEL time type.
TimestampKind = decls.TimestampKind
TimestampKind = types.TimestampKind

// TypeKind represents the CEL type.
TypeKind = decls.TypeKind
TypeKind = types.TypeKind

// TypeParamKind represents a parameterized type whose type name will be resolved at type-check time, if possible.
TypeParamKind = decls.TypeParamKind
TypeParamKind = types.TypeParamKind

// UintKind represents a uint type.
UintKind = decls.UintKind
UintKind = types.UintKind
)

var (
// AnyType represents the google.protobuf.Any type.
AnyType = decls.AnyType
AnyType = types.AnyType
// BoolType represents the bool type.
BoolType = decls.BoolType
BoolType = types.BoolType
// BytesType represents the bytes type.
BytesType = decls.BytesType
BytesType = types.BytesType
// DoubleType represents the double type.
DoubleType = decls.DoubleType
DoubleType = types.DoubleType
// DurationType represents the CEL duration type.
DurationType = decls.DurationType
DurationType = types.DurationType
// DynType represents a dynamic CEL type whose type will be determined at runtime from context.
DynType = decls.DynType
DynType = types.DynType
// IntType represents the int type.
IntType = decls.IntType
IntType = types.IntType
// NullType represents the type of a null value.
NullType = decls.NullType
NullType = types.NullType
// StringType represents the string type.
StringType = decls.StringType
StringType = types.StringType
// TimestampType represents the time type.
TimestampType = decls.TimestampType
TimestampType = types.TimestampType
// TypeType represents a CEL type
TypeType = decls.TypeType
TypeType = types.TypeType
// UintType represents a uint type.
UintType = decls.UintType
UintType = types.UintType

// function references for instantiating new types.

// ListType creates an instances of a list type value with the provided element type.
ListType = decls.NewListType
ListType = types.NewListType
// MapType creates an instance of a map type value with the provided key and value types.
MapType = decls.NewMapType
MapType = types.NewMapType
// NullableType creates an instance of a nullable type with the provided wrapped type.
//
// Note: only primitive types are supported as wrapped types.
NullableType = decls.NewNullableType
NullableType = types.NewNullableType
// OptionalType creates an abstract parameterized type instance corresponding to CEL's notion of optional.
OptionalType = decls.NewOptionalType
OptionalType = types.NewOptionalType
// OpaqueType creates an abstract parameterized type with a given name.
OpaqueType = decls.NewOpaqueType
OpaqueType = types.NewOpaqueType
// ObjectType creates a type references to an externally defined type, e.g. a protobuf message type.
ObjectType = decls.NewObjectType
ObjectType = types.NewObjectType
// TypeParamType creates a parameterized type instance.
TypeParamType = decls.NewTypeParamType
TypeParamType = types.NewTypeParamType
)

// Type holds a reference to a runtime type with an optional type-checked set of type parameters.
type Type = decls.Type
type Type = types.Type

// Variable creates an instance of a variable declaration with a variable name and type.
func Variable(name string, t *Type) EnvOption {
Expand Down Expand Up @@ -298,12 +298,12 @@ func OverloadOperandTrait(trait int) OverloadOpt {

// TypeToExprType converts a CEL-native type representation to a protobuf CEL Type representation.
func TypeToExprType(t *Type) (*exprpb.Type, error) {
return decls.TypeToExprType(t)
return types.TypeToExprType(t)
}

// ExprTypeToType converts a protobuf CEL type representation to a CEL-native type representation.
func ExprTypeToType(t *exprpb.Type) (*Type, error) {
return decls.ExprTypeToType(t)
return types.ExprTypeToType(t)
}

// ExprDeclToDeclaration converts a protobuf CEL declaration to a CEL-native declaration, either a Variable or Function.
Expand All @@ -315,21 +315,21 @@ func ExprDeclToDeclaration(d *exprpb.Decl) (EnvOption, error) {
for i, o := range overloads {
args := make([]*Type, len(o.GetParams()))
for j, p := range o.GetParams() {
a, err := decls.ExprTypeToType(p)
a, err := types.ExprTypeToType(p)
if err != nil {
return nil, err
}
args[j] = a
}
res, err := decls.ExprTypeToType(o.GetResultType())
res, err := types.ExprTypeToType(o.GetResultType())
if err != nil {
return nil, err
}
opts[i] = decls.Overload(o.GetOverloadId(), args, res)
}
return Function(d.GetName(), opts...), nil
case *exprpb.Decl_Ident:
t, err := decls.ExprTypeToType(d.GetIdent().GetType())
t, err := types.ExprTypeToType(d.GetIdent().GetType())
if err != nil {
return nil, err
}
Expand Down
2 changes: 0 additions & 2 deletions common/decls/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ go_library(
name = "go_default_library",
srcs = [
"decls.go",
"types.go",
],
importpath = "github.com/google/cel-go/common/decls",
deps = [
Expand All @@ -26,7 +25,6 @@ go_test(
name = "go_default_test",
srcs = [
"decls_test.go",
"types_test.go",
],
embed = [":go_default_library"],
deps = [
Expand Down
40 changes: 24 additions & 16 deletions common/decls/decls.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ func SingletonFunctionBinding(fn functions.FunctionOp, traits ...int) FunctionOp
//
// Note: function bindings should be commonly configured with Overload instances whereas operand traits and
// strict-ness should be rare occurrences.
func Overload(overloadID string, args []*Type, resultType *Type, opts ...OverloadOpt) FunctionOpt {
func Overload(overloadID string,
args []*types.Type, resultType *types.Type,
opts ...OverloadOpt) FunctionOpt {
return newOverload(overloadID, false, args, resultType, opts...)
}

Expand All @@ -375,11 +377,15 @@ func Overload(overloadID string, args []*Type, resultType *Type, opts ...Overloa
//
// Note: function bindings should be commonly configured with Overload instances whereas operand traits and
// strict-ness should be rare occurrences.
func MemberOverload(overloadID string, args []*Type, resultType *Type, opts ...OverloadOpt) FunctionOpt {
func MemberOverload(overloadID string,
args []*types.Type, resultType *types.Type,
opts ...OverloadOpt) FunctionOpt {
return newOverload(overloadID, true, args, resultType, opts...)
}

func newOverload(overloadID string, memberFunction bool, args []*Type, resultType *Type, opts ...OverloadOpt) FunctionOpt {
func newOverload(overloadID string,
memberFunction bool, args []*types.Type, resultType *types.Type,
opts ...OverloadOpt) FunctionOpt {
return func(f *FunctionDecl) (*FunctionDecl, error) {
overload, err := newOverloadInternal(overloadID, memberFunction, args, resultType, opts...)
if err != nil {
Expand All @@ -393,7 +399,9 @@ func newOverload(overloadID string, memberFunction bool, args []*Type, resultTyp
}
}

func newOverloadInternal(overloadID string, memberFunction bool, args []*Type, resultType *Type, opts ...OverloadOpt) (*OverloadDecl, error) {
func newOverloadInternal(overloadID string,
memberFunction bool, args []*types.Type, resultType *types.Type,
opts ...OverloadOpt) (*OverloadDecl, error) {
overload := &OverloadDecl{
ID: overloadID,
ArgTypes: args,
Expand Down Expand Up @@ -424,10 +432,10 @@ type OverloadDecl struct {
// ArgTypes contains the set of argument types expected by the overload.
//
// For member functions ArgTypes[0] represents the member operand type.
ArgTypes []*Type
ArgTypes []*types.Type

// ResultType indicates the output type from calling the function.
ResultType *Type
ResultType *types.Type

// IsMemberFunction indicates whether the overload is a member function
IsMemberFunction bool
Expand Down Expand Up @@ -558,7 +566,7 @@ func (o *OverloadDecl) matchesRuntimeSignature(disableTypeGuards bool, args ...r
return matchOperandTrait(o.OperandTrait, args[0])
}

func matchRuntimeArgType(nonStrict, disableTypeGuards bool, argType *Type, arg ref.Val) bool {
func matchRuntimeArgType(nonStrict, disableTypeGuards bool, argType *types.Type, arg ref.Val) bool {
if nonStrict && (disableTypeGuards || types.IsUnknownOrError(arg)) {
return true
}
Expand Down Expand Up @@ -637,14 +645,14 @@ func OverloadOperandTrait(trait int) OverloadOpt {
}

// NewVariable creates a new variable declaration.
func NewVariable(name string, t *Type) *VariableDecl {
func NewVariable(name string, t *types.Type) *VariableDecl {
return &VariableDecl{Name: name, Type: t}
}

// VariableDecl defines a variable declaration which may optionally have a constant value.
type VariableDecl struct {
Name string
Type *Type
Type *types.Type
}

// DeclarationEquals returns true if one variable declaration has the same name and same type as the input.
Expand All @@ -657,16 +665,16 @@ func (v *VariableDecl) DeclarationEquals(other *VariableDecl) bool {

// VariableDeclToExprDecl converts a go-native variable declaration into a protobuf-type variable declaration.
func VariableDeclToExprDecl(v *VariableDecl) (*exprpb.Decl, error) {
varType, err := TypeToExprType(v.Type)
varType, err := types.TypeToExprType(v.Type)
if err != nil {
return nil, err
}
return chkdecls.NewVar(v.Name, varType), nil
}

// TypeVariable creates a new type identifier for use within a ref.TypeProvider
func TypeVariable(t *Type) *VariableDecl {
return NewVariable(t.TypeName(), NewTypeTypeWithParam(t))
func TypeVariable(t *types.Type) *VariableDecl {
return NewVariable(t.TypeName(), types.NewTypeTypeWithParam(t))
}

// FunctionDeclToExprDecl converts a go-native function declaration into a protobuf-typed function declaration.
Expand All @@ -678,14 +686,14 @@ func FunctionDeclToExprDecl(f *FunctionDecl) (*exprpb.Decl, error) {
argTypes := make([]*exprpb.Type, len(o.ArgTypes))
for j, a := range o.ArgTypes {
collectParamNames(paramNames, a)
at, err := TypeToExprType(a)
at, err := types.TypeToExprType(a)
if err != nil {
return nil, err
}
argTypes[j] = at
}
collectParamNames(paramNames, o.ResultType)
resultType, err := TypeToExprType(o.ResultType)
resultType, err := types.TypeToExprType(o.ResultType)
if err != nil {
return nil, err
}
Expand All @@ -710,8 +718,8 @@ func FunctionDeclToExprDecl(f *FunctionDecl) (*exprpb.Decl, error) {
return chkdecls.NewFunction(f.Name, overloads...), nil
}

func collectParamNames(paramNames map[string]struct{}, arg *Type) {
if arg.Kind == TypeParamKind {
func collectParamNames(paramNames map[string]struct{}, arg *types.Type) {
if arg.Kind == types.TypeParamKind {
paramNames[arg.TypeName()] = struct{}{}
}
for _, param := range arg.Parameters {
Expand Down
Loading

0 comments on commit 1ad5fe6

Please sign in to comment.