func DecodeLengthDelimitedField(fd *desc.FieldDescriptor, bytes []byte, mf MessageFactory) (interface{}, error) {
	switch {
	case fd.GetType() == descriptor.FieldDescriptorProto_TYPE_BYTES:
		return bytes, nil
	case fd.GetType() == descriptor.FieldDescriptorProto_TYPE_STRING:
		return string(bytes), nil
	case fd.GetType() == descriptor.FieldDescriptorProto_TYPE_MESSAGE ||
		fd.GetType() == descriptor.FieldDescriptorProto_TYPE_GROUP:
		msg := mf.NewMessage(fd.GetMessageType())
		err := proto.Unmarshal(bytes, msg)
		if err != nil {
			return nil, err
		} else {
			return msg, nil
		}
	default:
		packedBuf := NewBuffer(bytes)
		var slice []interface{}
		var val interface{}
		for !packedBuf.EOF() {
			var v uint64
			var err error
			if varintTypes[fd.GetType()] {
				v, err = packedBuf.DecodeVarint()
			} else if fixed32Types[fd.GetType()] {
				v, err = packedBuf.DecodeFixed32()
			} else if fixed64Types[fd.GetType()] {
				v, err = packedBuf.DecodeFixed64()
			} else {
				return nil, fmt.Errorf("bad input; cannot parse length-delimited wire type for field %s", fd.GetFullyQualifiedName())
			}
			if err != nil {
				return nil, err
			}
			val, err = DecodeScalarField(fd, v)
			if err != nil {
				return nil, err
			}
			if fd.IsRepeated() {
				slice = append(slice, val)
			}
		}
		if fd.IsRepeated() {
			return slice, nil
		} else {
			return val, nil
		}
	}
}
DecodeLengthDelimitedField extracts a properly-typed value from bytes. The
returned value's type will usually be []byte, string, or, for nested messages,
the type returned from the given message factory. However, since repeated
scalar fields can be length-delimited, when they used packed encoding, it can
also return an []interface{}, where each element is a scalar value. Furthermore,
it could return a scalar type, not in a slice, if the given field descriptor is
not repeated. This is to support cases where a field is changed from optional
to repeated. New code may emit a packed repeated representation, but old code
still expects a single scalar value. In this case, if the actual data in bytes
contains multiple values, only the last value is returned.
package codec
import (
	"errors"
	"fmt"
	"io"
	"math"
	"github.com/golang/protobuf/proto"
	"github.com/golang/protobuf/protoc-gen-go/descriptor"
	"github.com/jhump/protoreflect/desc"
)
var varintTypes = map[descriptor.FieldDescriptorProto_Type]bool{}
var fixed32Types = map[descriptor.FieldDescriptorProto_Type]bool{}
var fixed64Types = map[descriptor.FieldDescriptorProto_Type]bool{}
func init() {
	varintTypes[descriptor.FieldDescriptorProto_TYPE_BOOL] = true
	varintTypes[descriptor.FieldDescriptorProto_TYPE_INT32] = true
	varintTypes[descriptor.FieldDescriptorProto_TYPE_INT64] = true
	varintTypes[descriptor.FieldDescriptorProto_TYPE_UINT32] = true
	varintTypes[descriptor.FieldDescriptorProto_TYPE_UINT64] = true
	varintTypes[descriptor.FieldDescriptorProto_TYPE_SINT32] = true
	varintTypes[descriptor.FieldDescriptorProto_TYPE_SINT64] = true
	varintTypes[descriptor.FieldDescriptorProto_TYPE_ENUM] = true
	fixed32Types[descriptor.FieldDescriptorProto_TYPE_FIXED32] = true
	fixed32Types[descriptor.FieldDescriptorProto_TYPE_SFIXED32] = true
	fixed32Types[descriptor.FieldDescriptorProto_TYPE_FLOAT] = true
	fixed64Types[descriptor.FieldDescriptorProto_TYPE_FIXED64] = true
	fixed64Types[descriptor.FieldDescriptorProto_TYPE_SFIXED64] = true
	fixed64Types[descriptor.FieldDescriptorProto_TYPE_DOUBLE] = true
}
var ErrWireTypeEndGroup = errors.New("unexpected wire type: end group")
type MessageFactory interface {
	NewMessage(md *desc.MessageDescriptor) proto.Message
}
type UnknownField struct {
	Tag int32
	Encoding int8
	Contents []byte
	Value    uint64
}
func DecodeZigZag32(v uint64) int32 {
	return int32((uint32(v) >> 1) ^ uint32((int32(v&1)<<31)>>31))
}
func DecodeZigZag64(v uint64) int64 {
	return int64((v >> 1) ^ uint64((int64(v&1)<<63)>>63))
}
func (cb *Buffer) DecodeFieldValue(fieldFinder func(int32) *desc.FieldDescriptor, fact MessageFactory) (*desc.FieldDescriptor, interface{}, error) {
	if cb.EOF() {
		return nil, nil, io.EOF
	}
	tagNumber, wireType, err := cb.DecodeTagAndWireType()
	if err != nil {
		return nil, nil, err
	}
	if wireType == proto.WireEndGroup {
		return nil, tagNumber, ErrWireTypeEndGroup
	}
	fd := fieldFinder(tagNumber)
	if fd == nil {
		val, err := cb.decodeUnknownField(tagNumber, wireType)
		return nil, val, err
	}
	val, err := cb.decodeKnownField(fd, wireType, fact)
	return fd, val, err
}
func DecodeScalarField(fd *desc.FieldDescriptor, v uint64) (interface{}, error) {
	switch fd.GetType() {
	case descriptor.FieldDescriptorProto_TYPE_BOOL:
		return v != 0, nil
	case descriptor.FieldDescriptorProto_TYPE_UINT32,
		descriptor.FieldDescriptorProto_TYPE_FIXED32:
		if v > math.MaxUint32 {
			return nil, ErrOverflow
		}
		return uint32(v), nil
	case descriptor.FieldDescriptorProto_TYPE_INT32,
		descriptor.FieldDescriptorProto_TYPE_ENUM:
		s := int64(v)
		if s > math.MaxInt32 || s < math.MinInt32 {
			return nil, ErrOverflow
		}
		return int32(s), nil
	case descriptor.FieldDescriptorProto_TYPE_SFIXED32:
		if v > math.MaxUint32 {
			return nil, ErrOverflow
		}
		return int32(v), nil
	case descriptor.FieldDescriptorProto_TYPE_SINT32:
		if v > math.MaxUint32 {
			return nil, ErrOverflow
		}
		return DecodeZigZag32(v), nil
	case descriptor.FieldDescriptorProto_TYPE_UINT64,
		descriptor.FieldDescriptorProto_TYPE_FIXED64:
		return v, nil
	case descriptor.FieldDescriptorProto_TYPE_INT64,
		descriptor.FieldDescriptorProto_TYPE_SFIXED64:
		return int64(v), nil
	case descriptor.FieldDescriptorProto_TYPE_SINT64:
		return DecodeZigZag64(v), nil
	case descriptor.FieldDescriptorProto_TYPE_FLOAT:
		if v > math.MaxUint32 {
			return nil, ErrOverflow
		}
		return math.Float32frombits(uint32(v)), nil
	case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
		return math.Float64frombits(v), nil
	default:
		return nil, fmt.Errorf("bad input; field %s requires length-delimited wire type", fd.GetFullyQualifiedName())
	}
}