(ctx *OptimizerContext, e ast.Expr)
| 418 | } |
| 419 | |
| 420 | func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) { |
| 421 | s := e.AsStruct() |
| 422 | fields := s.Fields() |
| 423 | updatedFields := []ast.EntryExpr{} |
| 424 | modified := false |
| 425 | for _, f := range fields { |
| 426 | field := f.AsStructField() |
| 427 | val := field.Value() |
| 428 | if !field.IsOptional() || val.Kind() != ast.LiteralKind { |
| 429 | updatedFields = append(updatedFields, f) |
| 430 | continue |
| 431 | } |
| 432 | optElemVal, ok := val.AsLiteral().(*types.Optional) |
| 433 | if !ok { |
| 434 | updatedFields = append(updatedFields, f) |
| 435 | continue |
| 436 | } |
| 437 | modified = true |
| 438 | if !optElemVal.HasValue() { |
| 439 | continue |
| 440 | } |
| 441 | ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue())) |
| 442 | updatedField := ctx.NewStructField(field.Name(), val, false) |
| 443 | updatedFields = append(updatedFields, updatedField) |
| 444 | } |
| 445 | if modified { |
| 446 | ctx.UpdateExpr(e, ctx.NewStruct(s.TypeName(), updatedFields)) |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | // adaptLiteral converts a runtime CEL value to its equivalent literal expression. |
| 451 | // |
no test coverage detected