(ctx *OptimizerContext, e ast.Expr)
| 357 | } |
| 358 | |
| 359 | func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) { |
| 360 | s := e.AsStruct() |
| 361 | fields := s.Fields() |
| 362 | updatedFields := []ast.EntryExpr{} |
| 363 | modified := false |
| 364 | for _, f := range fields { |
| 365 | field := f.AsStructField() |
| 366 | val := field.Value() |
| 367 | if !field.IsOptional() || val.Kind() != ast.LiteralKind { |
| 368 | updatedFields = append(updatedFields, f) |
| 369 | continue |
| 370 | } |
| 371 | optElemVal, ok := val.AsLiteral().(*types.Optional) |
| 372 | if !ok { |
| 373 | updatedFields = append(updatedFields, f) |
| 374 | continue |
| 375 | } |
| 376 | modified = true |
| 377 | if !optElemVal.HasValue() { |
| 378 | continue |
| 379 | } |
| 380 | ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue())) |
| 381 | updatedField := ctx.NewStructField(field.Name(), val, false) |
| 382 | updatedFields = append(updatedFields, updatedField) |
| 383 | } |
| 384 | if modified { |
| 385 | ctx.UpdateExpr(e, ctx.NewStruct(s.TypeName(), updatedFields)) |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // adaptLiteral converts a runtime CEL value to its equivalent literal expression. |
| 390 | // |
no test coverage detected