(sctx *super.Context, m *ast.Map, cast super.Type)
| 489 | } |
| 490 | |
| 491 | func (a Analyzer) convertMap(sctx *super.Context, m *ast.Map, cast super.Type) (Value, error) { |
| 492 | var keyType, valType super.Type |
| 493 | if cast != nil { |
| 494 | typ, ok := super.TypeUnder(cast).(*super.TypeMap) |
| 495 | if !ok { |
| 496 | return nil, errors.New("map decorator not of type map") |
| 497 | } |
| 498 | keyType = typ.KeyType |
| 499 | valType = typ.ValType |
| 500 | } |
| 501 | keys := make([]Value, 0, len(m.Entries)) |
| 502 | vals := make([]Value, 0, len(m.Entries)) |
| 503 | for _, e := range m.Entries { |
| 504 | key, err := a.convertValue(sctx, e.Key, keyType) |
| 505 | if err != nil { |
| 506 | return nil, err |
| 507 | } |
| 508 | val, err := a.convertValue(sctx, e.Value, valType) |
| 509 | if err != nil { |
| 510 | return nil, err |
| 511 | } |
| 512 | keys = append(keys, key) |
| 513 | vals = append(vals, val) |
| 514 | } |
| 515 | if cast == nil { |
| 516 | // If there was no decorator, pull the types out of the first |
| 517 | // entry we just analyed. |
| 518 | if len(keys) == 0 { |
| 519 | // empty set with no decorator |
| 520 | keyType = super.TypeNull |
| 521 | valType = super.TypeNull |
| 522 | } else { |
| 523 | var err error |
| 524 | keys, keyType, err = a.normalizeElems(sctx, keys) |
| 525 | if err != nil { |
| 526 | return nil, err |
| 527 | } |
| 528 | vals, valType, err = a.normalizeElems(sctx, vals) |
| 529 | if err != nil { |
| 530 | return nil, err |
| 531 | } |
| 532 | } |
| 533 | cast = sctx.LookupTypeMap(keyType, valType) |
| 534 | } |
| 535 | entries := make([]Entry, 0, len(keys)) |
| 536 | for i := range keys { |
| 537 | entries = append(entries, Entry{keys[i], vals[i]}) |
| 538 | } |
| 539 | return &Map{ |
| 540 | Type: cast, |
| 541 | Entries: entries, |
| 542 | }, nil |
| 543 | } |
| 544 | |
| 545 | func (a Analyzer) convertTypeValue(sctx *super.Context, tv *ast.TypeValue, cast super.Type) (Value, error) { |
| 546 | if cast != nil { |
no test coverage detected