(ctx *OptimizerContext, e ast.Expr)
| 374 | } |
| 375 | |
| 376 | func pruneOptionalMapEntries(ctx *OptimizerContext, e ast.Expr) { |
| 377 | m := e.AsMap() |
| 378 | entries := m.Entries() |
| 379 | updatedEntries := []ast.EntryExpr{} |
| 380 | modified := false |
| 381 | for _, e := range entries { |
| 382 | entry := e.AsMapEntry() |
| 383 | key := entry.Key() |
| 384 | val := entry.Value() |
| 385 | // If the entry is not optional, or the value-side of the optional hasn't |
| 386 | // been resolved to a literal, then preserve the entry as-is. |
| 387 | if !entry.IsOptional() || val.Kind() != ast.LiteralKind { |
| 388 | updatedEntries = append(updatedEntries, e) |
| 389 | continue |
| 390 | } |
| 391 | optElemVal, ok := val.AsLiteral().(*types.Optional) |
| 392 | if !ok { |
| 393 | updatedEntries = append(updatedEntries, e) |
| 394 | continue |
| 395 | } |
| 396 | // When the key is not a literal, but the value is, then it needs to be |
| 397 | // restored to an optional value. |
| 398 | if key.Kind() != ast.LiteralKind { |
| 399 | undoOptVal, err := adaptLiteral(ctx, optElemVal) |
| 400 | if err != nil { |
| 401 | ctx.ReportErrorAtID(val.ID(), "invalid map value literal %v: %v", optElemVal, err) |
| 402 | } |
| 403 | ctx.UpdateExpr(val, undoOptVal) |
| 404 | updatedEntries = append(updatedEntries, e) |
| 405 | continue |
| 406 | } |
| 407 | modified = true |
| 408 | if !optElemVal.HasValue() { |
| 409 | continue |
| 410 | } |
| 411 | ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue())) |
| 412 | updatedEntry := ctx.NewMapEntry(key, val, false) |
| 413 | updatedEntries = append(updatedEntries, updatedEntry) |
| 414 | } |
| 415 | if modified { |
| 416 | ctx.UpdateExpr(e, ctx.NewMap(updatedEntries)) |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) { |
| 421 | s := e.AsStruct() |
no test coverage detected