| 1 |
// Copyright 2018 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package decls provides helpers for creating variable and function declarations. |
| 16 |
package decls |
| 17 |
|
| 18 |
import ( |
| 19 |
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 20 |
emptypb "google.golang.org/protobuf/types/known/emptypb" |
| 21 |
structpb "google.golang.org/protobuf/types/known/structpb" |
| 22 |
) |
| 23 |
|
| 24 |
var ( |
| 25 |
// Error type used to communicate issues during type-checking. |
| 26 |
Error = &exprpb.Type{ |
| 27 |
TypeKind: &exprpb.Type_Error{ |
| 28 |
Error: &emptypb.Empty{}}} |
| 29 |
|
| 30 |
// Dyn is a top-type used to represent any value. |
| 31 |
Dyn = &exprpb.Type{ |
| 32 |
TypeKind: &exprpb.Type_Dyn{ |
| 33 |
Dyn: &emptypb.Empty{}}} |
| 34 |
) |
| 35 |
|
| 36 |
// Commonly used types. |
| 37 |
var ( |
| 38 |
Bool = NewPrimitiveType(exprpb.Type_BOOL) |
| 39 |
Bytes = NewPrimitiveType(exprpb.Type_BYTES) |
| 40 |
Double = NewPrimitiveType(exprpb.Type_DOUBLE) |
| 41 |
Int = NewPrimitiveType(exprpb.Type_INT64) |
| 42 |
Null = &exprpb.Type{ |
| 43 |
TypeKind: &exprpb.Type_Null{ |
| 44 |
Null: structpb.NullValue_NULL_VALUE}} |
| 45 |
String = NewPrimitiveType(exprpb.Type_STRING) |
| 46 |
Uint = NewPrimitiveType(exprpb.Type_UINT64) |
| 47 |
) |
| 48 |
|
| 49 |
// Well-known types. |
| 50 |
// TODO: Replace with an abstract type registry. |
| 51 |
var ( |
| 52 |
Any = NewWellKnownType(exprpb.Type_ANY) |
| 53 |
Duration = NewWellKnownType(exprpb.Type_DURATION) |
| 54 |
Timestamp = NewWellKnownType(exprpb.Type_TIMESTAMP) |
| 55 |
) |
| 56 |
|
| 57 |
// NewAbstractType creates an abstract type declaration which references a proto |
| 58 |
// message name and may also include type parameters. |
| 59 |
func NewAbstractType(name string, paramTypes ...*exprpb.Type) *exprpb.Type { |
| 60 |
return &exprpb.Type{ |
| 61 |
TypeKind: &exprpb.Type_AbstractType_{ |
| 62 |
AbstractType: &exprpb.Type_AbstractType{ |
| 63 |
Name: name, |
| 64 |
ParameterTypes: paramTypes}}} |
| 65 |
} |
| 66 |
|
| 67 |
// NewOptionalType constructs an abstract type indicating that the parameterized type |
| 68 |
// may be contained within the object. |
| 69 |
func NewOptionalType(paramType *exprpb.Type) *exprpb.Type { |
| 70 |
return NewAbstractType("optional_type", paramType) |
| 71 |
} |
| 72 |
|
| 73 |
// NewFunctionType creates a function invocation contract, typically only used |
| 74 |
// by type-checking steps after overload resolution. |
| 75 |
func NewFunctionType(resultType *exprpb.Type, |
| 76 |
argTypes ...*exprpb.Type) *exprpb.Type { |
| 77 |
return &exprpb.Type{ |
| 78 |
TypeKind: &exprpb.Type_Function{ |
| 79 |
Function: &exprpb.Type_FunctionType{ |
| 80 |
ResultType: resultType, |
| 81 |
ArgTypes: argTypes}}} |
| 82 |
} |
| 83 |
|
| 84 |
// NewFunction creates a named function declaration with one or more overloads. |
| 85 |
func NewFunction(name string, |
| 86 |
overloads ...*exprpb.Decl_FunctionDecl_Overload) *exprpb.Decl { |
| 87 |
return &exprpb.Decl{ |
| 88 |
Name: name, |
| 89 |
DeclKind: &exprpb.Decl_Function{ |
| 90 |
Function: &exprpb.Decl_FunctionDecl{ |
| 91 |
Overloads: overloads}}} |
| 92 |
} |
| 93 |
|
| 94 |
// NewFunctionWithDoc creates a named function declaration with a description and one or more overloads. |
| 95 |
func NewFunctionWithDoc(name, doc string, |
| 96 |
overloads ...*exprpb.Decl_FunctionDecl_Overload) *exprpb.Decl { |
| 97 |
return &exprpb.Decl{ |
| 98 |
Name: name, |
| 99 |
DeclKind: &exprpb.Decl_Function{ |
| 100 |
Function: &exprpb.Decl_FunctionDecl{ |
| 101 |
// Doc: desc, |
| 102 |
Overloads: overloads}}} |
| 103 |
} |
| 104 |
|
| 105 |
// NewIdent creates a named identifier declaration with an optional literal |
| 106 |
// value. |
| 107 |
// |
| 108 |
// Literal values are typically only associated with enum identifiers. |
| 109 |
// |
| 110 |
// Deprecated: Use NewVar or NewConst instead. |
| 111 |
func NewIdent(name string, t *exprpb.Type, v *exprpb.Constant) *exprpb.Decl { |
| 112 |
return newIdent(name, t, v, "") |
| 113 |
} |
| 114 |
|
| 115 |
func newIdent(name string, t *exprpb.Type, v *exprpb.Constant, desc string) *exprpb.Decl { |
| 116 |
return &exprpb.Decl{ |
| 117 |
Name: name, |
| 118 |
DeclKind: &exprpb.Decl_Ident{ |
| 119 |
Ident: &exprpb.Decl_IdentDecl{ |
| 120 |
Type: t, |
| 121 |
Value: v, |
| 122 |
Doc: desc}}} |
| 123 |
} |
| 124 |
|
| 125 |
// NewConst creates a constant identifier with a CEL constant literal value. |
| 126 |
func NewConst(name string, t *exprpb.Type, v *exprpb.Constant) *exprpb.Decl { |
| 127 |
return newIdent(name, t, v, "") |
| 128 |
} |
| 129 |
|
| 130 |
// NewVar creates a variable identifier. |
| 131 |
func NewVar(name string, t *exprpb.Type) *exprpb.Decl { |
| 132 |
return newIdent(name, t, nil, "") |
| 133 |
} |
| 134 |
|
| 135 |
// NewVarWithDoc creates a variable identifier with a type and a description string. |
| 136 |
func NewVarWithDoc(name string, t *exprpb.Type, desc string) *exprpb.Decl { |
| 137 |
return newIdent(name, t, nil, desc) |
| 138 |
} |
| 139 |
|
| 140 |
// NewInstanceOverload creates a instance function overload contract. |
| 141 |
// First element of argTypes is instance. |
| 142 |
func NewInstanceOverload(id string, argTypes []*exprpb.Type, resultType *exprpb.Type) *exprpb.Decl_FunctionDecl_Overload { |
| 143 |
return &exprpb.Decl_FunctionDecl_Overload{ |
| 144 |
OverloadId: id, |
| 145 |
ResultType: resultType, |
| 146 |
Params: argTypes, |
| 147 |
IsInstanceFunction: true} |
| 148 |
} |
| 149 |
|
| 150 |
// NewListType generates a new list with elements of a certain type. |
| 151 |
func NewListType(elem *exprpb.Type) *exprpb.Type { |
| 152 |
return &exprpb.Type{ |
| 153 |
TypeKind: &exprpb.Type_ListType_{ |
| 154 |
ListType: &exprpb.Type_ListType{ |
| 155 |
ElemType: elem}}} |
| 156 |
} |
| 157 |
|
| 158 |
// NewMapType generates a new map with typed keys and values. |
| 159 |
func NewMapType(key *exprpb.Type, value *exprpb.Type) *exprpb.Type { |
| 160 |
return &exprpb.Type{ |
| 161 |
TypeKind: &exprpb.Type_MapType_{ |
| 162 |
MapType: &exprpb.Type_MapType{ |
| 163 |
KeyType: key, |
| 164 |
ValueType: value}}} |
| 165 |
} |
| 166 |
|
| 167 |
// NewObjectType creates an object type for a qualified type name. |
| 168 |
func NewObjectType(typeName string) *exprpb.Type { |
| 169 |
return &exprpb.Type{ |
| 170 |
TypeKind: &exprpb.Type_MessageType{ |
| 171 |
MessageType: typeName}} |
| 172 |
} |
| 173 |
|
| 174 |
// NewOverload creates a function overload declaration which contains a unique |
| 175 |
// overload id as well as the expected argument and result types. Overloads |
| 176 |
// must be aggregated within a Function declaration. |
| 177 |
func NewOverload(id string, argTypes []*exprpb.Type, resultType *exprpb.Type) *exprpb.Decl_FunctionDecl_Overload { |
| 178 |
return &exprpb.Decl_FunctionDecl_Overload{ |
| 179 |
OverloadId: id, |
| 180 |
ResultType: resultType, |
| 181 |
Params: argTypes, |
| 182 |
IsInstanceFunction: false} |
| 183 |
} |
| 184 |
|
| 185 |
// NewParameterizedInstanceOverload creates a parametric function instance overload type. |
| 186 |
func NewParameterizedInstanceOverload(id string, |
| 187 |
argTypes []*exprpb.Type, |
| 188 |
resultType *exprpb.Type, |
| 189 |
typeParams []string) *exprpb.Decl_FunctionDecl_Overload { |
| 190 |
return &exprpb.Decl_FunctionDecl_Overload{ |
| 191 |
OverloadId: id, |
| 192 |
ResultType: resultType, |
| 193 |
Params: argTypes, |
| 194 |
TypeParams: typeParams, |
| 195 |
IsInstanceFunction: true} |
| 196 |
} |
| 197 |
|
| 198 |
// NewParameterizedOverload creates a parametric function overload type. |
| 199 |
func NewParameterizedOverload(id string, |
| 200 |
argTypes []*exprpb.Type, |
| 201 |
resultType *exprpb.Type, |
| 202 |
typeParams []string) *exprpb.Decl_FunctionDecl_Overload { |
| 203 |
return &exprpb.Decl_FunctionDecl_Overload{ |
| 204 |
OverloadId: id, |
| 205 |
ResultType: resultType, |
| 206 |
Params: argTypes, |
| 207 |
TypeParams: typeParams, |
| 208 |
IsInstanceFunction: false} |
| 209 |
} |
| 210 |
|
| 211 |
// NewPrimitiveType creates a type for a primitive value. See the var declarations |
| 212 |
// for Int, Uint, etc. |
| 213 |
func NewPrimitiveType(primitive exprpb.Type_PrimitiveType) *exprpb.Type { |
| 214 |
return &exprpb.Type{ |
| 215 |
TypeKind: &exprpb.Type_Primitive{ |
| 216 |
Primitive: primitive}} |
| 217 |
} |
| 218 |
|
| 219 |
// NewTypeType creates a new type designating a type. |
| 220 |
func NewTypeType(nested *exprpb.Type) *exprpb.Type { |
| 221 |
if nested == nil { |
| 222 |
// must set the nested field for a valid oneof option |
| 223 |
nested = &exprpb.Type{} |
| 224 |
} |
| 225 |
return &exprpb.Type{ |
| 226 |
TypeKind: &exprpb.Type_Type{ |
| 227 |
Type: nested}} |
| 228 |
} |
| 229 |
|
| 230 |
// NewTypeParamType creates a type corresponding to a named, contextual parameter. |
| 231 |
func NewTypeParamType(name string) *exprpb.Type { |
| 232 |
return &exprpb.Type{ |
| 233 |
TypeKind: &exprpb.Type_TypeParam{ |
| 234 |
TypeParam: name}} |
| 235 |
} |
| 236 |
|
| 237 |
// NewWellKnownType creates a type corresponding to a protobuf well-known type |
| 238 |
// value. |
| 239 |
func NewWellKnownType(wellKnown exprpb.Type_WellKnownType) *exprpb.Type { |
| 240 |
return &exprpb.Type{ |
| 241 |
TypeKind: &exprpb.Type_WellKnown{ |
| 242 |
WellKnown: wellKnown}} |
| 243 |
} |
| 244 |
|
| 245 |
// NewWrapperType creates a wrapped primitive type instance. Wrapped types |
| 246 |
// are roughly equivalent to a nullable, or optionally valued type. |
| 247 |
func NewWrapperType(wrapped *exprpb.Type) *exprpb.Type { |
| 248 |
primitive := wrapped.GetPrimitive() |
| 249 |
if primitive == exprpb.Type_PRIMITIVE_TYPE_UNSPECIFIED { |
| 250 |
// TODO: return an error |
| 251 |
panic("Wrapped type must be a primitive") |
| 252 |
} |
| 253 |
return &exprpb.Type{TypeKind: &exprpb.Type_Wrapper{Wrapper: primitive}} |
| 254 |
} |