| 1 | // Copyright 2022 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 cel |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | |
| 20 | "github.com/google/cel-go/common" |
| 21 | "github.com/google/cel-go/common/ast" |
| 22 | "github.com/google/cel-go/common/types" |
| 23 | "github.com/google/cel-go/parser" |
| 24 | |
| 25 | exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 26 | ) |
| 27 | |
| 28 | // Macro describes a function signature to match and the MacroExpander to apply. |
| 29 | // |
| 30 | // Note: when a Macro should apply to multiple overloads (based on arg count) of a given function, |
| 31 | // a Macro should be created per arg-count or as a var arg macro. |
| 32 | type Macro = parser.Macro |
| 33 | |
| 34 | // MacroFactory defines an expansion function which converts a call and its arguments to a cel.Expr value. |
| 35 | type MacroFactory = parser.MacroExpander |
| 36 | |
| 37 | // MacroExprFactory assists with the creation of Expr values in a manner which is consistent |
| 38 | // the internal semantics and id generation behaviors of the parser and checker libraries. |
| 39 | type MacroExprFactory = parser.ExprHelper |
| 40 | |
| 41 | // MacroExpander converts a call and its associated arguments into a protobuf Expr representation. |
| 42 | // |
| 43 | // If the MacroExpander determines within the implementation that an expansion is not needed it may return |
| 44 | // a nil Expr value to indicate a non-match. However, if an expansion is to be performed, but the arguments |
| 45 | // are not well-formed, the result of the expansion will be an error. |
| 46 | // |
| 47 | // The MacroExpander accepts as arguments a MacroExprHelper as well as the arguments used in the function call |
| 48 | // and produces as output an Expr ast node. |
| 49 | // |
| 50 | // Note: when the Macro.IsReceiverStyle() method returns true, the target argument will be nil. |
| 51 | type MacroExpander func(eh MacroExprHelper, target *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error) |
| 52 | |
| 53 | // MacroExprHelper exposes helper methods for creating new expressions within a CEL abstract syntax tree. |
| 54 | // ExprHelper assists with the manipulation of proto-based Expr values in a manner which is |
| 55 | // consistent with the source position and expression id generation code leveraged by both |
| 56 | // the parser and type-checker. |
| 57 | type MacroExprHelper interface { |
| 58 | // Copy the input expression with a brand new set of identifiers. |
| 59 | Copy(*exprpb.Expr) *exprpb.Expr |
| 60 | |
| 61 | // LiteralBool creates an Expr value for a bool literal. |
| 62 | LiteralBool(value bool) *exprpb.Expr |
| 63 | |
| 64 | // LiteralBytes creates an Expr value for a byte literal. |
| 65 | LiteralBytes(value []byte) *exprpb.Expr |
| 66 | |
| 67 | // LiteralDouble creates an Expr value for double literal. |
| 68 | LiteralDouble(value float64) *exprpb.Expr |
| 69 | |
| 70 | // LiteralInt creates an Expr value for an int literal. |
| 71 | LiteralInt(value int64) *exprpb.Expr |
| 72 | |
| 73 | // LiteralString creates am Expr value for a string literal. |
| 74 | LiteralString(value string) *exprpb.Expr |
| 75 | |
| 76 | // LiteralUint creates an Expr value for a uint literal. |
| 77 | LiteralUint(value uint64) *exprpb.Expr |
| 78 | |
| 79 | // NewList creates a CreateList instruction where the list is comprised of the optional set |
| 80 | // of elements provided as arguments. |
| 81 | NewList(elems ...*exprpb.Expr) *exprpb.Expr |
| 82 | |
| 83 | // NewMap creates a CreateStruct instruction for a map where the map is comprised of the |
| 84 | // optional set of key, value entries. |
| 85 | NewMap(entries ...*exprpb.Expr_CreateStruct_Entry) *exprpb.Expr |
| 86 | |
| 87 | // NewMapEntry creates a Map Entry for the key, value pair. |
| 88 | NewMapEntry(key *exprpb.Expr, val *exprpb.Expr, optional bool) *exprpb.Expr_CreateStruct_Entry |
| 89 | |
| 90 | // NewObject creates a CreateStruct instruction for an object with a given type name and |
| 91 | // optional set of field initializers. |
| 92 | NewObject(typeName string, fieldInits ...*exprpb.Expr_CreateStruct_Entry) *exprpb.Expr |
| 93 | |
| 94 | // NewObjectFieldInit creates a new Object field initializer from the field name and value. |
| 95 | NewObjectFieldInit(field string, init *exprpb.Expr, optional bool) *exprpb.Expr_CreateStruct_Entry |
| 96 | |
| 97 | // Fold creates a fold comprehension instruction. |
| 98 | // |
| 99 | // - iterVar is the iteration variable name. |
| 100 | // - iterRange represents the expression that resolves to a list or map where the elements or |
| 101 | // keys (respectively) will be iterated over. |
| 102 | // - accuVar is the accumulation variable name, typically parser.AccumulatorName. |
| 103 | // - accuInit is the initial expression whose value will be set for the accuVar prior to |
| 104 | // folding. |
| 105 | // - condition is the expression to test to determine whether to continue folding. |
| 106 | // - step is the expression to evaluation at the conclusion of a single fold iteration. |
| 107 | // - result is the computation to evaluate at the conclusion of the fold. |
| 108 | // |
| 109 | // The accuVar should not shadow variable names that you would like to reference within the |
| 110 | // environment in the step and condition expressions. Presently, the name __result__ is commonly |
| 111 | // used by built-in macros but this may change in the future. |
| 112 | Fold(iterVar string, |
| 113 | iterRange *exprpb.Expr, |
| 114 | accuVar string, |
| 115 | accuInit *exprpb.Expr, |
| 116 | condition *exprpb.Expr, |
| 117 | step *exprpb.Expr, |
| 118 | result *exprpb.Expr) *exprpb.Expr |
| 119 | |
| 120 | // Ident creates an identifier Expr value. |
| 121 | Ident(name string) *exprpb.Expr |
| 122 | |
| 123 | // AccuIdent returns an accumulator identifier for use with comprehension results. |
| 124 | AccuIdent() *exprpb.Expr |
| 125 | |
| 126 | // GlobalCall creates a function call Expr value for a global (free) function. |
| 127 | GlobalCall(function string, args ...*exprpb.Expr) *exprpb.Expr |
| 128 | |
| 129 | // ReceiverCall creates a function call Expr value for a receiver-style function. |
| 130 | ReceiverCall(function string, target *exprpb.Expr, args ...*exprpb.Expr) *exprpb.Expr |
| 131 | |
| 132 | // PresenceTest creates a Select TestOnly Expr value for modelling has() semantics. |
| 133 | PresenceTest(operand *exprpb.Expr, field string) *exprpb.Expr |
| 134 | |
| 135 | // Select create a field traversal Expr value. |
| 136 | Select(operand *exprpb.Expr, field string) *exprpb.Expr |
| 137 | |
| 138 | // OffsetLocation returns the Location of the expression identifier. |
| 139 | OffsetLocation(exprID int64) common.Location |
| 140 | |
| 141 | // NewError associates an error message with a given expression id. |
| 142 | NewError(exprID int64, message string) *Error |
| 143 | } |
| 144 | |
| 145 | // MacroOpt defines a functional option for configuring macro behavior. |
| 146 | type MacroOpt = parser.MacroOpt |
| 147 | |
| 148 | // MacroDocs configures a list of strings into a multiline description for the macro. |
| 149 | func MacroDocs(docs ...string) MacroOpt { |
| 150 | return parser.MacroDocs(docs...) |
| 151 | } |
| 152 | |
| 153 | // MacroExamples configures a list of examples, either as a string or common.MultilineString, |
| 154 | // into an example set to be provided with the macro Documentation() call. |
| 155 | func MacroExamples(examples ...string) MacroOpt { |
| 156 | return parser.MacroExamples(examples...) |
| 157 | } |
| 158 | |
| 159 | // GlobalMacro creates a Macro for a global function with the specified arg count. |
| 160 | func GlobalMacro(function string, argCount int, factory MacroFactory, opts ...MacroOpt) Macro { |
| 161 | return parser.NewGlobalMacro(function, argCount, factory, opts...) |
| 162 | } |
| 163 | |
| 164 | // ReceiverMacro creates a Macro for a receiver function matching the specified arg count. |
| 165 | func ReceiverMacro(function string, argCount int, factory MacroFactory, opts ...MacroOpt) Macro { |
| 166 | return parser.NewReceiverMacro(function, argCount, factory, opts...) |
| 167 | } |
| 168 | |
| 169 | // GlobalVarArgMacro creates a Macro for a global function with a variable arg count. |
| 170 | func GlobalVarArgMacro(function string, factory MacroFactory, opts ...MacroOpt) Macro { |
| 171 | return parser.NewGlobalVarArgMacro(function, factory, opts...) |
| 172 | } |
| 173 | |
| 174 | // ReceiverVarArgMacro creates a Macro for a receiver function matching a variable arg count. |
| 175 | func ReceiverVarArgMacro(function string, factory MacroFactory, opts ...MacroOpt) Macro { |
| 176 | return parser.NewReceiverVarArgMacro(function, factory, opts...) |
| 177 | } |
| 178 | |
| 179 | // NewGlobalMacro creates a Macro for a global function with the specified arg count. |
| 180 | // |
| 181 | // Deprecated: use GlobalMacro |
| 182 | func NewGlobalMacro(function string, argCount int, expander MacroExpander) Macro { |
| 183 | expand := adaptingExpander{expander} |
| 184 | return parser.NewGlobalMacro(function, argCount, expand.Expander) |
| 185 | } |
| 186 | |
| 187 | // NewReceiverMacro creates a Macro for a receiver function matching the specified arg count. |
| 188 | // |
| 189 | // Deprecated: use ReceiverMacro |
| 190 | func NewReceiverMacro(function string, argCount int, expander MacroExpander) Macro { |
| 191 | expand := adaptingExpander{expander} |
| 192 | return parser.NewReceiverMacro(function, argCount, expand.Expander) |
| 193 | } |
| 194 | |
| 195 | // NewGlobalVarArgMacro creates a Macro for a global function with a variable arg count. |
| 196 | // |
| 197 | // Deprecated: use GlobalVarArgMacro |
| 198 | func NewGlobalVarArgMacro(function string, expander MacroExpander) Macro { |
| 199 | expand := adaptingExpander{expander} |
| 200 | return parser.NewGlobalVarArgMacro(function, expand.Expander) |
| 201 | } |
| 202 | |
| 203 | // NewReceiverVarArgMacro creates a Macro for a receiver function matching a variable arg count. |
| 204 | // |
| 205 | // Deprecated: use ReceiverVarArgMacro |
| 206 | func NewReceiverVarArgMacro(function string, expander MacroExpander) Macro { |
| 207 | expand := adaptingExpander{expander} |
| 208 | return parser.NewReceiverVarArgMacro(function, expand.Expander) |
| 209 | } |
| 210 | |
| 211 | // HasMacroExpander expands the input call arguments into a presence test, e.g. has(<operand>.field) |
| 212 | func HasMacroExpander(meh MacroExprHelper, target *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error) { |
| 213 | ph, err := toParserHelper(meh) |
| 214 | if err != nil { |
| 215 | return nil, err |
| 216 | } |
| 217 | arg, err := adaptToExpr(args[0]) |
| 218 | if err != nil { |
| 219 | return nil, err |
| 220 | } |
| 221 | if arg.Kind() == ast.SelectKind { |
| 222 | s := arg.AsSelect() |
| 223 | return adaptToProto(ph.NewPresenceTest(s.Operand(), s.FieldName())) |
| 224 | } |
| 225 | return nil, ph.NewError(arg.ID(), "invalid argument to has() macro") |
| 226 | } |
| 227 | |
| 228 | // ExistsMacroExpander expands the input call arguments into a comprehension that returns true if any of the |
| 229 | // elements in the range match the predicate expressions: |
| 230 | // <iterRange>.exists(<iterVar>, <predicate>) |
| 231 | func ExistsMacroExpander(meh MacroExprHelper, target *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error) { |
| 232 | ph, err := toParserHelper(meh) |
| 233 | if err != nil { |
| 234 | return nil, err |
| 235 | } |
| 236 | out, err := parser.MakeExists(ph, mustAdaptToExpr(target), mustAdaptToExprs(args)) |
| 237 | if err != nil { |
| 238 | return nil, err |
| 239 | } |
| 240 | return adaptToProto(out) |
| 241 | } |
| 242 | |
| 243 | // ExistsOneMacroExpander expands the input call arguments into a comprehension that returns true if exactly |
| 244 | // one of the elements in the range match the predicate expressions: |
| 245 | // <iterRange>.exists_one(<iterVar>, <predicate>) |
| 246 | func ExistsOneMacroExpander(meh MacroExprHelper, target *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error) { |
| 247 | ph, err := toParserHelper(meh) |
| 248 | if err != nil { |
| 249 | return nil, err |
| 250 | } |
| 251 | out, err := parser.MakeExistsOne(ph, mustAdaptToExpr(target), mustAdaptToExprs(args)) |
| 252 | if err != nil { |
| 253 | return nil, err |
| 254 | } |
| 255 | return adaptToProto(out) |
| 256 | } |
| 257 | |
| 258 | // MapMacroExpander expands the input call arguments into a comprehension that transforms each element in the |
| 259 | // input to produce an output list. |
| 260 | // |
| 261 | // There are two call patterns supported by map: |
| 262 | // |
| 263 | // <iterRange>.map(<iterVar>, <transform>) |
| 264 | // <iterRange>.map(<iterVar>, <predicate>, <transform>) |
| 265 | // |
| 266 | // In the second form only iterVar values which return true when provided to the predicate expression |
| 267 | // are transformed. |
| 268 | func MapMacroExpander(meh MacroExprHelper, target *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error) { |
| 269 | ph, err := toParserHelper(meh) |
| 270 | if err != nil { |
| 271 | return nil, err |
| 272 | } |
| 273 | out, err := parser.MakeMap(ph, mustAdaptToExpr(target), mustAdaptToExprs(args)) |
| 274 | if err != nil { |
| 275 | return nil, err |
| 276 | } |
| 277 | return adaptToProto(out) |
| 278 | } |
| 279 | |
| 280 | // FilterMacroExpander expands the input call arguments into a comprehension which produces a list which contains |
| 281 | // only elements which match the provided predicate expression: |
| 282 | // <iterRange>.filter(<iterVar>, <predicate>) |
| 283 | func FilterMacroExpander(meh MacroExprHelper, target *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error) { |
| 284 | ph, err := toParserHelper(meh) |
| 285 | if err != nil { |
| 286 | return nil, err |
| 287 | } |
| 288 | out, err := parser.MakeFilter(ph, mustAdaptToExpr(target), mustAdaptToExprs(args)) |
| 289 | if err != nil { |
| 290 | return nil, err |
| 291 | } |
| 292 | return adaptToProto(out) |
| 293 | } |
| 294 | |
| 295 | var ( |
| 296 | // Aliases to each macro in the CEL standard environment. |
| 297 | // Note: reassigning these macro variables may result in undefined behavior. |
| 298 | |
| 299 | // HasMacro expands "has(m.f)" which tests the presence of a field, avoiding the need to |
| 300 | // specify the field as a string. |
| 301 | HasMacro = parser.HasMacro |
| 302 | |
| 303 | // AllMacro expands "range.all(var, predicate)" into a comprehension which ensures that all |
| 304 | // elements in the range satisfy the predicate. |
| 305 | AllMacro = parser.AllMacro |
| 306 | |
| 307 | // ExistsMacro expands "range.exists(var, predicate)" into a comprehension which ensures that |
| 308 | // some element in the range satisfies the predicate. |
| 309 | ExistsMacro = parser.ExistsMacro |
| 310 | |
| 311 | // ExistsOneMacro expands "range.exists_one(var, predicate)", which is true if for exactly one |
| 312 | // element in range the predicate holds. |
| 313 | ExistsOneMacro = parser.ExistsOneMacro |
| 314 | |
| 315 | // MapMacro expands "range.map(var, function)" into a comprehension which applies the function |
| 316 | // to each element in the range to produce a new list. |
| 317 | MapMacro = parser.MapMacro |
| 318 | |
| 319 | // MapFilterMacro expands "range.map(var, predicate, function)" into a comprehension which |
| 320 | // first filters the elements in the range by the predicate, then applies the transform function |
| 321 | // to produce a new list. |
| 322 | MapFilterMacro = parser.MapFilterMacro |
| 323 | |
| 324 | // FilterMacro expands "range.filter(var, predicate)" into a comprehension which filters |
| 325 | // elements in the range, producing a new list from the elements that satisfy the predicate. |
| 326 | FilterMacro = parser.FilterMacro |
| 327 | |
| 328 | // StandardMacros provides an alias to all the CEL macros defined in the standard environment. |
| 329 | StandardMacros = []Macro{ |
| 330 | HasMacro, AllMacro, ExistsMacro, ExistsOneMacro, MapMacro, MapFilterMacro, FilterMacro, |
| 331 | } |
| 332 | |
| 333 | // NoMacros provides an alias to an empty list of macros |
| 334 | NoMacros = []Macro{} |
| 335 | ) |
| 336 | |
| 337 | type adaptingExpander struct { |
| 338 | legacyExpander MacroExpander |
| 339 | } |
| 340 | |
| 341 | func (adapt *adaptingExpander) Expander(eh parser.ExprHelper, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) { |
| 342 | var legacyTarget *exprpb.Expr = nil |
| 343 | var err *Error = nil |
| 344 | if target != nil { |
| 345 | legacyTarget, err = adaptToProto(target) |
| 346 | if err != nil { |
| 347 | return nil, err |
| 348 | } |
| 349 | } |
| 350 | legacyArgs := make([]*exprpb.Expr, len(args)) |
| 351 | for i, arg := range args { |
| 352 | legacyArgs[i], err = adaptToProto(arg) |
| 353 | if err != nil { |
| 354 | return nil, err |
| 355 | } |
| 356 | } |
| 357 | ah := &adaptingHelper{modernHelper: eh} |
| 358 | legacyExpr, err := adapt.legacyExpander(ah, legacyTarget, legacyArgs) |
| 359 | if err != nil { |
| 360 | return nil, err |
| 361 | } |
| 362 | ex, err := adaptToExpr(legacyExpr) |
| 363 | if err != nil { |
| 364 | return nil, err |
| 365 | } |
| 366 | return ex, nil |
| 367 | } |
| 368 | |
| 369 | func wrapErr(id int64, message string, err error) *common.Error { |
| 370 | return &common.Error{ |
| 371 | Location: common.NoLocation, |
| 372 | Message: fmt.Sprintf("%s: %v", message, err), |
| 373 | ExprID: id, |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | type adaptingHelper struct { |
| 378 | modernHelper parser.ExprHelper |
| 379 | } |
| 380 | |
| 381 | // Copy the input expression with a brand new set of identifiers. |
| 382 | func (ah *adaptingHelper) Copy(e *exprpb.Expr) *exprpb.Expr { |
| 383 | return mustAdaptToProto(ah.modernHelper.Copy(mustAdaptToExpr(e))) |
| 384 | } |
| 385 | |
| 386 | // LiteralBool creates an Expr value for a bool literal. |
| 387 | func (ah *adaptingHelper) LiteralBool(value bool) *exprpb.Expr { |
| 388 | return mustAdaptToProto(ah.modernHelper.NewLiteral(types.Bool(value))) |
| 389 | } |
| 390 | |
| 391 | // LiteralBytes creates an Expr value for a byte literal. |
| 392 | func (ah *adaptingHelper) LiteralBytes(value []byte) *exprpb.Expr { |
| 393 | return mustAdaptToProto(ah.modernHelper.NewLiteral(types.Bytes(value))) |
| 394 | } |
| 395 | |
| 396 | // LiteralDouble creates an Expr value for double literal. |
| 397 | func (ah *adaptingHelper) LiteralDouble(value float64) *exprpb.Expr { |
| 398 | return mustAdaptToProto(ah.modernHelper.NewLiteral(types.Double(value))) |
| 399 | } |
| 400 | |
| 401 | // LiteralInt creates an Expr value for an int literal. |
| 402 | func (ah *adaptingHelper) LiteralInt(value int64) *exprpb.Expr { |
| 403 | return mustAdaptToProto(ah.modernHelper.NewLiteral(types.Int(value))) |
| 404 | } |
| 405 | |
| 406 | // LiteralString creates am Expr value for a string literal. |
| 407 | func (ah *adaptingHelper) LiteralString(value string) *exprpb.Expr { |
| 408 | return mustAdaptToProto(ah.modernHelper.NewLiteral(types.String(value))) |
| 409 | } |
| 410 | |
| 411 | // LiteralUint creates an Expr value for a uint literal. |
| 412 | func (ah *adaptingHelper) LiteralUint(value uint64) *exprpb.Expr { |
| 413 | return mustAdaptToProto(ah.modernHelper.NewLiteral(types.Uint(value))) |
| 414 | } |
| 415 | |
| 416 | // NewList creates a CreateList instruction where the list is comprised of the optional set |
| 417 | // of elements provided as arguments. |
| 418 | func (ah *adaptingHelper) NewList(elems ...*exprpb.Expr) *exprpb.Expr { |
| 419 | return mustAdaptToProto(ah.modernHelper.NewList(mustAdaptToExprs(elems)...)) |
| 420 | } |
| 421 | |
| 422 | // NewMap creates a CreateStruct instruction for a map where the map is comprised of the |
| 423 | // optional set of key, value entries. |
| 424 | func (ah *adaptingHelper) NewMap(entries ...*exprpb.Expr_CreateStruct_Entry) *exprpb.Expr { |
| 425 | adaptedEntries := make([]ast.EntryExpr, len(entries)) |
| 426 | for i, e := range entries { |
| 427 | adaptedEntries[i] = mustAdaptToEntryExpr(e) |
| 428 | } |
| 429 | return mustAdaptToProto(ah.modernHelper.NewMap(adaptedEntries...)) |
| 430 | } |
| 431 | |
| 432 | // NewMapEntry creates a Map Entry for the key, value pair. |
| 433 | func (ah *adaptingHelper) NewMapEntry(key *exprpb.Expr, val *exprpb.Expr, optional bool) *exprpb.Expr_CreateStruct_Entry { |
| 434 | return mustAdaptToProtoEntry( |
| 435 | ah.modernHelper.NewMapEntry(mustAdaptToExpr(key), mustAdaptToExpr(val), optional)) |
| 436 | } |
| 437 | |
| 438 | // NewObject creates a CreateStruct instruction for an object with a given type name and |
| 439 | // optional set of field initializers. |
| 440 | func (ah *adaptingHelper) NewObject(typeName string, fieldInits ...*exprpb.Expr_CreateStruct_Entry) *exprpb.Expr { |
| 441 | adaptedEntries := make([]ast.EntryExpr, len(fieldInits)) |
| 442 | for i, e := range fieldInits { |
| 443 | adaptedEntries[i] = mustAdaptToEntryExpr(e) |
| 444 | } |
| 445 | return mustAdaptToProto(ah.modernHelper.NewStruct(typeName, adaptedEntries...)) |
| 446 | } |
| 447 | |
| 448 | // NewObjectFieldInit creates a new Object field initializer from the field name and value. |
| 449 | func (ah *adaptingHelper) NewObjectFieldInit(field string, init *exprpb.Expr, optional bool) *exprpb.Expr_CreateStruct_Entry { |
| 450 | return mustAdaptToProtoEntry( |
| 451 | ah.modernHelper.NewStructField(field, mustAdaptToExpr(init), optional)) |
| 452 | } |
| 453 | |
| 454 | // Fold creates a fold comprehension instruction. |
| 455 | // |
| 456 | // - iterVar is the iteration variable name. |
| 457 | // - iterRange represents the expression that resolves to a list or map where the elements or |
| 458 | // keys (respectively) will be iterated over. |
| 459 | // - accuVar is the accumulation variable name, typically parser.AccumulatorName. |
| 460 | // - accuInit is the initial expression whose value will be set for the accuVar prior to |
| 461 | // folding. |
| 462 | // - condition is the expression to test to determine whether to continue folding. |
| 463 | // - step is the expression to evaluation at the conclusion of a single fold iteration. |
| 464 | // - result is the computation to evaluate at the conclusion of the fold. |
| 465 | // |
| 466 | // The accuVar should not shadow variable names that you would like to reference within the |
| 467 | // environment in the step and condition expressions. Presently, the name __result__ is commonly |
| 468 | // used by built-in macros but this may change in the future. |
| 469 | func (ah *adaptingHelper) Fold(iterVar string, |
| 470 | iterRange *exprpb.Expr, |
| 471 | accuVar string, |
| 472 | accuInit *exprpb.Expr, |
| 473 | condition *exprpb.Expr, |
| 474 | step *exprpb.Expr, |
| 475 | result *exprpb.Expr) *exprpb.Expr { |
| 476 | return mustAdaptToProto( |
| 477 | ah.modernHelper.NewComprehension( |
| 478 | mustAdaptToExpr(iterRange), |
| 479 | iterVar, |
| 480 | accuVar, |
| 481 | mustAdaptToExpr(accuInit), |
| 482 | mustAdaptToExpr(condition), |
| 483 | mustAdaptToExpr(step), |
| 484 | mustAdaptToExpr(result), |
| 485 | ), |
| 486 | ) |
| 487 | } |
| 488 | |
| 489 | // Ident creates an identifier Expr value. |
| 490 | func (ah *adaptingHelper) Ident(name string) *exprpb.Expr { |
| 491 | return mustAdaptToProto(ah.modernHelper.NewIdent(name)) |
| 492 | } |
| 493 | |
| 494 | // AccuIdent returns an accumulator identifier for use with comprehension results. |
| 495 | func (ah *adaptingHelper) AccuIdent() *exprpb.Expr { |
| 496 | return mustAdaptToProto(ah.modernHelper.NewAccuIdent()) |
| 497 | } |
| 498 | |
| 499 | // GlobalCall creates a function call Expr value for a global (free) function. |
| 500 | func (ah *adaptingHelper) GlobalCall(function string, args ...*exprpb.Expr) *exprpb.Expr { |
| 501 | return mustAdaptToProto(ah.modernHelper.NewCall(function, mustAdaptToExprs(args)...)) |
| 502 | } |
| 503 | |
| 504 | // ReceiverCall creates a function call Expr value for a receiver-style function. |
| 505 | func (ah *adaptingHelper) ReceiverCall(function string, target *exprpb.Expr, args ...*exprpb.Expr) *exprpb.Expr { |
| 506 | return mustAdaptToProto( |
| 507 | ah.modernHelper.NewMemberCall(function, mustAdaptToExpr(target), mustAdaptToExprs(args)...)) |
| 508 | } |
| 509 | |
| 510 | // PresenceTest creates a Select TestOnly Expr value for modelling has() semantics. |
| 511 | func (ah *adaptingHelper) PresenceTest(operand *exprpb.Expr, field string) *exprpb.Expr { |
| 512 | op := mustAdaptToExpr(operand) |
| 513 | return mustAdaptToProto(ah.modernHelper.NewPresenceTest(op, field)) |
| 514 | } |
| 515 | |
| 516 | // Select create a field traversal Expr value. |
| 517 | func (ah *adaptingHelper) Select(operand *exprpb.Expr, field string) *exprpb.Expr { |
| 518 | op := mustAdaptToExpr(operand) |
| 519 | return mustAdaptToProto(ah.modernHelper.NewSelect(op, field)) |
| 520 | } |
| 521 | |
| 522 | // OffsetLocation returns the Location of the expression identifier. |
| 523 | func (ah *adaptingHelper) OffsetLocation(exprID int64) common.Location { |
| 524 | return ah.modernHelper.OffsetLocation(exprID) |
| 525 | } |
| 526 | |
| 527 | // NewError associates an error message with a given expression id. |
| 528 | func (ah *adaptingHelper) NewError(exprID int64, message string) *Error { |
| 529 | return ah.modernHelper.NewError(exprID, message) |
| 530 | } |
| 531 | |
| 532 | func mustAdaptToExprs(exprs []*exprpb.Expr) []ast.Expr { |
| 533 | adapted := make([]ast.Expr, len(exprs)) |
| 534 | for i, e := range exprs { |
| 535 | adapted[i] = mustAdaptToExpr(e) |
| 536 | } |
| 537 | return adapted |
| 538 | } |
| 539 | |
| 540 | func mustAdaptToExpr(e *exprpb.Expr) ast.Expr { |
| 541 | out, _ := adaptToExpr(e) |
| 542 | return out |
| 543 | } |
| 544 | |
| 545 | func adaptToExpr(e *exprpb.Expr) (ast.Expr, *Error) { |
| 546 | if e == nil { |
| 547 | return nil, nil |
| 548 | } |
| 549 | out, err := ast.ProtoToExpr(e) |
| 550 | if err != nil { |
| 551 | return nil, wrapErr(e.GetId(), "proto conversion failure", err) |
| 552 | } |
| 553 | return out, nil |
| 554 | } |
| 555 | |
| 556 | func mustAdaptToEntryExpr(e *exprpb.Expr_CreateStruct_Entry) ast.EntryExpr { |
| 557 | out, _ := ast.ProtoToEntryExpr(e) |
| 558 | return out |
| 559 | } |
| 560 | |
| 561 | func mustAdaptToProto(e ast.Expr) *exprpb.Expr { |
| 562 | out, _ := adaptToProto(e) |
| 563 | return out |
| 564 | } |
| 565 | |
| 566 | func adaptToProto(e ast.Expr) (*exprpb.Expr, *Error) { |
| 567 | if e == nil { |
| 568 | return nil, nil |
| 569 | } |
| 570 | out, err := ast.ExprToProto(e) |
| 571 | if err != nil { |
| 572 | return nil, wrapErr(e.ID(), "expr conversion failure", err) |
| 573 | } |
| 574 | return out, nil |
| 575 | } |
| 576 | |
| 577 | func mustAdaptToProtoEntry(e ast.EntryExpr) *exprpb.Expr_CreateStruct_Entry { |
| 578 | out, _ := ast.EntryExprToProto(e) |
| 579 | return out |
| 580 | } |
| 581 | |
| 582 | func toParserHelper(meh MacroExprHelper) (parser.ExprHelper, *Error) { |
| 583 | ah, ok := meh.(*adaptingHelper) |
| 584 | if !ok { |
| 585 | return nil, common.NewError(0, |
| 586 | fmt.Sprintf("unsupported macro helper: %v (%T)", meh, meh), |
| 587 | common.NoLocation) |
| 588 | } |
| 589 | return ah.modernHelper, nil |
| 590 | } |