(ctx *OptimizerContext, e ast.Expr)
| 313 | } |
| 314 | |
| 315 | func pruneOptionalMapEntries(ctx *OptimizerContext, e ast.Expr) { |
| 316 | m := e.AsMap() |
| 317 | entries := m.Entries() |
| 318 | updatedEntries := []ast.EntryExpr{} |
| 319 | modified := false |
| 320 | for _, e := range entries { |
| 321 | entry := e.AsMapEntry() |
| 322 | key := entry.Key() |
| 323 | val := entry.Value() |
| 324 | // If the entry is not optional, or the value-side of the optional hasn't |
| 325 | // been resolved to a literal, then preserve the entry as-is. |
| 326 | if !entry.IsOptional() || val.Kind() != ast.LiteralKind { |
| 327 | updatedEntries = append(updatedEntries, e) |
| 328 | continue |
| 329 | } |
| 330 | optElemVal, ok := val.AsLiteral().(*types.Optional) |
| 331 | if !ok { |
| 332 | updatedEntries = append(updatedEntries, e) |
| 333 | continue |
| 334 | } |
| 335 | // When the key is not a literal, but the value is, then it needs to be |
| 336 | // restored to an optional value. |
| 337 | if key.Kind() != ast.LiteralKind { |
| 338 | undoOptVal, err := adaptLiteral(ctx, optElemVal) |
| 339 | if err != nil { |
| 340 | ctx.ReportErrorAtID(val.ID(), "invalid map value literal %v: %v", optElemVal, err) |
| 341 | } |
| 342 | ctx.UpdateExpr(val, undoOptVal) |
| 343 | updatedEntries = append(updatedEntries, e) |
| 344 | continue |
| 345 | } |
| 346 | modified = true |
| 347 | if !optElemVal.HasValue() { |
| 348 | continue |
| 349 | } |
| 350 | ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue())) |
| 351 | updatedEntry := ctx.NewMapEntry(key, val, false) |
| 352 | updatedEntries = append(updatedEntries, updatedEntry) |
| 353 | } |
| 354 | if modified { |
| 355 | ctx.UpdateExpr(e, ctx.NewMap(updatedEntries)) |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) { |
| 360 | s := e.AsStruct() |
no test coverage detected