TODO add byte, int, long array IMPORTANT!
(list reflect.Value)
| 737 | |
| 738 | // TODO add byte, int, long array IMPORTANT! |
| 739 | func (d *Decoder) decodeList(list reflect.Value) error { |
| 740 | typeId, err := d.readByte() |
| 741 | if err != nil { |
| 742 | return err |
| 743 | } |
| 744 | length, err := d.readInt() |
| 745 | if err != nil { |
| 746 | return err |
| 747 | } |
| 748 | if list.Len() < int(length) { |
| 749 | switch list.Kind() { |
| 750 | case reflect.Slice: |
| 751 | list.Set(reflect.MakeSlice(list.Type(), int(length), int(length))) |
| 752 | case reflect.Array: |
| 753 | return fmt.Errorf("list of size %d is too big for array %s", length, list.Type()) |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | for i := 0; i < int(length); i++ { |
| 758 | switch typeId { |
| 759 | case Byte: |
| 760 | d, err := d.readByte() |
| 761 | if err != nil { |
| 762 | return err |
| 763 | } |
| 764 | |
| 765 | switch list.Type().Elem().Kind() { |
| 766 | case reflect.Uint8: |
| 767 | list.Index(i).Set(reflect.ValueOf(uint8(d))) |
| 768 | default: |
| 769 | if reflect.TypeOf(d).AssignableTo(list.Type().Elem()) { |
| 770 | list.Index(i).Set(reflect.ValueOf(d)) |
| 771 | } else { |
| 772 | return fmt.Errorf("cannot assign byte to type %s for index %d", list.Type().Elem(), i) |
| 773 | } |
| 774 | } |
| 775 | case Short: |
| 776 | d, err := d.readShort() |
| 777 | if err != nil { |
| 778 | return err |
| 779 | } |
| 780 | |
| 781 | switch list.Type().Elem().Kind() { |
| 782 | case reflect.Uint16: |
| 783 | list.Index(i).Set(reflect.ValueOf(uint16(d))) |
| 784 | default: |
| 785 | if reflect.TypeOf(d).AssignableTo(list.Type().Elem()) { |
| 786 | list.Index(i).Set(reflect.ValueOf(d)) |
| 787 | } else { |
| 788 | return fmt.Errorf("cannot assign short to type %s for index %d", list.Type().Elem(), i) |
| 789 | } |
| 790 | } |
| 791 | case Int: |
| 792 | d, err := d.readInt() |
| 793 | if err != nil { |
| 794 | return err |
| 795 | } |
| 796 |
no test coverage detected