(ctx *OptimizerContext, e ast.Expr)
| 354 | } |
| 355 | |
| 356 | func pruneOptionalMapEntries(ctx *OptimizerContext, e ast.Expr) { |
| 357 | m := e.AsMap() |
| 358 | entries := m.Entries() |
| 359 | updatedEntries := []ast.EntryExpr{} |
| 360 | modified := false |
| 361 | for _, e := range entries { |
| 362 | entry := e.AsMapEntry() |
| 363 | key := entry.Key() |
| 364 | val := entry.Value() |
| 365 | // If the entry is not optional, or the value-side of the optional hasn't |
| 366 | // been resolved to a literal, then preserve the entry as-is. |
| 367 | if !entry.IsOptional() || val.Kind() != ast.LiteralKind { |
| 368 | updatedEntries = append(updatedEntries, e) |
| 369 | continue |
| 370 | } |
| 371 | optElemVal, ok := val.AsLiteral().(*types.Optional) |
| 372 | if !ok { |
| 373 | updatedEntries = append(updatedEntries, e) |
| 374 | continue |
| 375 | } |
| 376 | // When the key is not a literal, but the value is, then it needs to be |
| 377 | // restored to an optional value. |
| 378 | if key.Kind() != ast.LiteralKind { |
| 379 | undoOptVal, err := adaptLiteral(ctx, optElemVal) |
| 380 | if err != nil { |
| 381 | ctx.ReportErrorAtID(val.ID(), "invalid map value literal %v: %v", optElemVal, err) |
| 382 | } |
| 383 | ctx.UpdateExpr(val, undoOptVal) |
| 384 | updatedEntries = append(updatedEntries, e) |
| 385 | continue |
| 386 | } |
| 387 | modified = true |
| 388 | if !optElemVal.HasValue() { |
| 389 | continue |
| 390 | } |
| 391 | ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue())) |
| 392 | updatedEntry := ctx.NewMapEntry(key, val, false) |
| 393 | updatedEntries = append(updatedEntries, updatedEntry) |
| 394 | } |
| 395 | if modified { |
| 396 | ctx.UpdateExpr(e, ctx.NewMap(updatedEntries)) |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) { |
| 401 | s := e.AsStruct() |
no test coverage detected