(ctx *OptimizerContext, e ast.Expr)
| 398 | } |
| 399 | |
| 400 | func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) { |
| 401 | s := e.AsStruct() |
| 402 | fields := s.Fields() |
| 403 | updatedFields := []ast.EntryExpr{} |
| 404 | modified := false |
| 405 | for _, f := range fields { |
| 406 | field := f.AsStructField() |
| 407 | val := field.Value() |
| 408 | if !field.IsOptional() || val.Kind() != ast.LiteralKind { |
| 409 | updatedFields = append(updatedFields, f) |
| 410 | continue |
| 411 | } |
| 412 | optElemVal, ok := val.AsLiteral().(*types.Optional) |
| 413 | if !ok { |
| 414 | updatedFields = append(updatedFields, f) |
| 415 | continue |
| 416 | } |
| 417 | modified = true |
| 418 | if !optElemVal.HasValue() { |
| 419 | continue |
| 420 | } |
| 421 | ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue())) |
| 422 | updatedField := ctx.NewStructField(field.Name(), val, false) |
| 423 | updatedFields = append(updatedFields, updatedField) |
| 424 | } |
| 425 | if modified { |
| 426 | ctx.UpdateExpr(e, ctx.NewStruct(s.TypeName(), updatedFields)) |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // adaptLiteral converts a runtime CEL value to its equivalent literal expression. |
| 431 | // |
no test coverage detected