findType recursively finds type information.
(typ reflect.Type)
| 553 | |
| 554 | // findType recursively finds type information. |
| 555 | func (es *encodeState) findType(typ reflect.Type) wire.TypeSpec { |
| 556 | // First: check if this is a proper type. It's possible for pointers, |
| 557 | // slices, arrays, maps, etc to all have some different type. |
| 558 | te, ok := es.types.Lookup(typ) |
| 559 | if te != nil { |
| 560 | if !ok { |
| 561 | // See encodeStruct. |
| 562 | es.pendingTypes = append(es.pendingTypes, te.Type) |
| 563 | } |
| 564 | return wire.TypeID(te.ID) |
| 565 | } |
| 566 | |
| 567 | switch typ.Kind() { |
| 568 | case reflect.Ptr: |
| 569 | return &wire.TypeSpecPointer{ |
| 570 | Type: es.findType(typ.Elem()), |
| 571 | } |
| 572 | case reflect.Slice: |
| 573 | return &wire.TypeSpecSlice{ |
| 574 | Type: es.findType(typ.Elem()), |
| 575 | } |
| 576 | case reflect.Array: |
| 577 | return &wire.TypeSpecArray{ |
| 578 | Count: wire.Uint(typ.Len()), |
| 579 | Type: es.findType(typ.Elem()), |
| 580 | } |
| 581 | case reflect.Map: |
| 582 | return &wire.TypeSpecMap{ |
| 583 | Key: es.findType(typ.Key()), |
| 584 | Value: es.findType(typ.Elem()), |
| 585 | } |
| 586 | default: |
| 587 | // After potentially chasing many pointers, the |
| 588 | // ultimate type of the object is not known. |
| 589 | Failf("type %q is not known", typ) |
| 590 | } |
| 591 | panic("unreachable") |
| 592 | } |
| 593 | |
| 594 | // encodeInterface encodes an interface. |
| 595 | func (es *encodeState) encodeInterface(obj reflect.Value, dest *wire.Object) { |