func()

in list.go [344:402]


func (d *Decoder) readTypedListValue(length int, listTyp string, isVariableArr bool) (interface{}, error) {
	// return when no element
	if length < 0 {
		return nil, nil
	}

	var (
		aryValue reflect.Value
		arrType  reflect.Type
	)

	t, err := strconv.Atoi(listTyp)
	if err == nil {
		// find the ref list type
		arrType = d.typeRefs.Get(t)
		if arrType == nil {
			return nil, perrors.Errorf("can't find ref list type at index %d", t)
		}
		aryValue = reflect.MakeSlice(arrType, length, length)
	} else {
		// try to find the registered list type
		arrType = getListType(listTyp)
		if arrType != nil {
			aryValue = reflect.MakeSlice(arrType, length, length)
			d.typeRefs.appendTypeRefs(listTyp, arrType)
		} else {
			// using default generic list type if not found registered
			aryValue = reflect.ValueOf(make([]interface{}, length, length))
			d.typeRefs.appendTypeRefs(listTyp, aryValue.Type())
		}
	}

	holder := d.appendRefs(aryValue)
	for j := 0; j < length || isVariableArr; j++ {
		it, err := d.DecodeValue()
		if err != nil {
			if perrors.Is(err, io.EOF) && isVariableArr {
				break
			}
			return nil, perrors.WithStack(err)
		}

		if isVariableArr {
			if it != nil {
				aryValue = reflect.Append(aryValue, EnsureRawValue(it))
			} else {
				aryValue = reflect.Append(aryValue, reflect.Zero(aryValue.Type().Elem()))
			}
			holder.change(aryValue)
		} else {
			if it != nil {
				//aryValue.Index(j).Set(EnsureRawValue(it))
				SetValue(aryValue.Index(j), EnsureRawValue(it))
			}
		}
	}

	return holder, nil
}