MCPcopy Create free account
hub / github.com/ZeppelinMC/Zeppelin / decodeList

Method decodeList

protocol/nbt/decoder.go:739–891  ·  view source on GitHub ↗

TODO add byte, int, long array IMPORTANT!

(list reflect.Value)

Source from the content-addressed store, hash-verified

737
738// TODO add byte, int, long array IMPORTANT!
739func (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

Callers 2

decodeCompoundMapMethod · 0.95
decodeCompoundStructMethod · 0.95

Calls 13

readByteMethod · 0.95
readIntMethod · 0.95
readShortMethod · 0.95
readLongMethod · 0.95
readFloatMethod · 0.95
readDoubleMethod · 0.95
readStringMethod · 0.95
decodeCompoundStructMethod · 0.95
decodeCompoundMapMethod · 0.95
generateMapFunction · 0.85
LenMethod · 0.80
SetMethod · 0.45

Tested by

no test coverage detected