(ctx *OptimizerContext, e ast.Expr)
| 392 | } |
| 393 | |
| 394 | func pruneOptionalMapEntries(ctx *OptimizerContext, e ast.Expr) { |
| 395 | m := e.AsMap() |
| 396 | entries := m.Entries() |
| 397 | updatedEntries := []ast.EntryExpr{} |
| 398 | modified := false |
| 399 | for _, e := range entries { |
| 400 | entry := e.AsMapEntry() |
| 401 | key := entry.Key() |
| 402 | val := entry.Value() |
| 403 | // If the entry is not optional, or the value-side of the optional hasn't |
| 404 | // been resolved to a literal, then preserve the entry as-is. |
| 405 | if !entry.IsOptional() || val.Kind() != ast.LiteralKind { |
| 406 | updatedEntries = append(updatedEntries, e) |
| 407 | continue |
| 408 | } |
| 409 | optElemVal, ok := val.AsLiteral().(*types.Optional) |
| 410 | if !ok { |
| 411 | updatedEntries = append(updatedEntries, e) |
| 412 | continue |
| 413 | } |
| 414 | // When the key is not a literal, but the value is, then it needs to be |
| 415 | // restored to an optional value. |
| 416 | if key.Kind() != ast.LiteralKind { |
| 417 | undoOptVal, err := adaptLiteral(ctx, optElemVal) |
| 418 | if err != nil { |
| 419 | ctx.ReportErrorAtID(val.ID(), "invalid map value literal %v: %v", optElemVal, err) |
| 420 | } |
| 421 | ctx.UpdateExpr(val, undoOptVal) |
| 422 | updatedEntries = append(updatedEntries, e) |
| 423 | continue |
| 424 | } |
| 425 | modified = true |
| 426 | if !optElemVal.HasValue() { |
| 427 | continue |
| 428 | } |
| 429 | ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue())) |
| 430 | updatedEntry := ctx.NewMapEntry(key, val, false) |
| 431 | updatedEntries = append(updatedEntries, updatedEntry) |
| 432 | } |
| 433 | if modified { |
| 434 | ctx.UpdateExpr(e, ctx.NewMap(updatedEntries)) |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) { |
| 439 | s := e.AsStruct() |
no test coverage detected