(ctx *OptimizerContext, e ast.Expr)
| 436 | } |
| 437 | |
| 438 | func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) { |
| 439 | s := e.AsStruct() |
| 440 | fields := s.Fields() |
| 441 | updatedFields := []ast.EntryExpr{} |
| 442 | modified := false |
| 443 | for _, f := range fields { |
| 444 | field := f.AsStructField() |
| 445 | val := field.Value() |
| 446 | if !field.IsOptional() || val.Kind() != ast.LiteralKind { |
| 447 | updatedFields = append(updatedFields, f) |
| 448 | continue |
| 449 | } |
| 450 | optElemVal, ok := val.AsLiteral().(*types.Optional) |
| 451 | if !ok { |
| 452 | updatedFields = append(updatedFields, f) |
| 453 | continue |
| 454 | } |
| 455 | modified = true |
| 456 | if !optElemVal.HasValue() { |
| 457 | continue |
| 458 | } |
| 459 | ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue())) |
| 460 | updatedField := ctx.NewStructField(field.Name(), val, false) |
| 461 | updatedFields = append(updatedFields, updatedField) |
| 462 | } |
| 463 | if modified { |
| 464 | ctx.UpdateExpr(e, ctx.NewStruct(s.TypeName(), updatedFields)) |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | // adaptLiteral converts a runtime CEL value to its equivalent literal expression. |
| 469 | // |
no test coverage detected