MCPcopy Create free account
hub / github.com/cel-expr/cel-go / Copy

Method Copy

parser/helper.go:339–408  ·  view source on GitHub ↗

Copy implements the ExprHelper interface method by producing a copy of the input Expr value with a fresh set of numeric identifiers the Expr and all its descendants.

(expr ast.Expr)

Source from the content-addressed store, hash-verified

337// Copy implements the ExprHelper interface method by producing a copy of the input Expr value
338// with a fresh set of numeric identifiers the Expr and all its descendants.
339func (e *exprHelper) Copy(expr ast.Expr) ast.Expr {
340 offsetRange, _ := e.parserHelper.sourceInfo.GetOffsetRange(expr.ID())
341 copyID := e.parserHelper.newID(offsetRange)
342 switch expr.Kind() {
343 case ast.LiteralKind:
344 return e.exprFactory.NewLiteral(copyID, expr.AsLiteral())
345 case ast.IdentKind:
346 return e.exprFactory.NewIdent(copyID, expr.AsIdent())
347 case ast.SelectKind:
348 sel := expr.AsSelect()
349 op := e.Copy(sel.Operand())
350 if sel.IsTestOnly() {
351 return e.exprFactory.NewPresenceTest(copyID, op, sel.FieldName())
352 }
353 return e.exprFactory.NewSelect(copyID, op, sel.FieldName())
354 case ast.CallKind:
355 call := expr.AsCall()
356 args := call.Args()
357 argsCopy := make([]ast.Expr, len(args))
358 for i, arg := range args {
359 argsCopy[i] = e.Copy(arg)
360 }
361 if !call.IsMemberFunction() {
362 return e.exprFactory.NewCall(copyID, call.FunctionName(), argsCopy...)
363 }
364 return e.exprFactory.NewMemberCall(copyID, call.FunctionName(), e.Copy(call.Target()), argsCopy...)
365 case ast.ListKind:
366 list := expr.AsList()
367 elems := list.Elements()
368 elemsCopy := make([]ast.Expr, len(elems))
369 for i, elem := range elems {
370 elemsCopy[i] = e.Copy(elem)
371 }
372 return e.exprFactory.NewList(copyID, elemsCopy, list.OptionalIndices())
373 case ast.MapKind:
374 m := expr.AsMap()
375 entries := m.Entries()
376 entriesCopy := make([]ast.EntryExpr, len(entries))
377 for i, en := range entries {
378 entry := en.AsMapEntry()
379 entryID := e.nextMacroID()
380 entriesCopy[i] = e.exprFactory.NewMapEntry(entryID,
381 e.Copy(entry.Key()), e.Copy(entry.Value()), entry.IsOptional())
382 }
383 return e.exprFactory.NewMap(copyID, entriesCopy)
384 case ast.StructKind:
385 s := expr.AsStruct()
386 fields := s.Fields()
387 fieldsCopy := make([]ast.EntryExpr, len(fields))
388 for i, f := range fields {
389 field := f.AsStructField()
390 fieldID := e.nextMacroID()
391 fieldsCopy[i] = e.exprFactory.NewStructField(fieldID,
392 field.Name(), e.Copy(field.Value()), field.IsOptional())
393 }
394 return e.exprFactory.NewStruct(copyID, s.TypeName(), fieldsCopy)
395 case ast.ComprehensionKind:
396 compre := expr.AsComprehension()

Callers

nothing calls this directly

Calls 15

nextMacroIDMethod · 0.95
GetOffsetRangeMethod · 0.80
newIDMethod · 0.80
IDMethod · 0.65
KindMethod · 0.65
NewLiteralMethod · 0.65
AsLiteralMethod · 0.65
NewIdentMethod · 0.65
AsIdentMethod · 0.65
AsSelectMethod · 0.65
OperandMethod · 0.65
IsTestOnlyMethod · 0.65

Tested by

no test coverage detected