mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2024-12-28 18:25:46 +00:00
c5060d2fe6
Passing a non-pointer type to protoreflect.NewValue causes an unnecessary allocation in order to store the value in an interface{}. While this allocation could be avoided by a smarter compiler, no such compiler exists today. Add functions for creating new values of a specific type, avoiding the allocation. (And also adding a small amount of type safety, although this is unlikely to be important.) Update the proto and internal/impl packages to use these functions. Change-Id: Ic733de22ddf19c530189166c853348e1b54b7391 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/191457 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
601 lines
14 KiB
Go
601 lines
14 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style.
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Code generated by generate-types. DO NOT EDIT.
|
|
|
|
package proto
|
|
|
|
import (
|
|
"math"
|
|
"unicode/utf8"
|
|
|
|
"google.golang.org/protobuf/internal/encoding/wire"
|
|
"google.golang.org/protobuf/internal/errors"
|
|
"google.golang.org/protobuf/internal/strs"
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
)
|
|
|
|
// unmarshalScalar decodes a value of the given kind.
|
|
//
|
|
// Message values are decoded into a []byte which aliases the input data.
|
|
func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp wire.Type, fd protoreflect.FieldDescriptor) (val protoreflect.Value, n int, err error) {
|
|
switch fd.Kind() {
|
|
case protoreflect.BoolKind:
|
|
if wtyp != wire.VarintType {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfBool(wire.DecodeBool(v)), n, nil
|
|
case protoreflect.EnumKind:
|
|
if wtyp != wire.VarintType {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfEnum(protoreflect.EnumNumber(v)), n, nil
|
|
case protoreflect.Int32Kind:
|
|
if wtyp != wire.VarintType {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfInt32(int32(v)), n, nil
|
|
case protoreflect.Sint32Kind:
|
|
if wtyp != wire.VarintType {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfInt32(int32(wire.DecodeZigZag(v & math.MaxUint32))), n, nil
|
|
case protoreflect.Uint32Kind:
|
|
if wtyp != wire.VarintType {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfUint32(uint32(v)), n, nil
|
|
case protoreflect.Int64Kind:
|
|
if wtyp != wire.VarintType {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfInt64(int64(v)), n, nil
|
|
case protoreflect.Sint64Kind:
|
|
if wtyp != wire.VarintType {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfInt64(wire.DecodeZigZag(v)), n, nil
|
|
case protoreflect.Uint64Kind:
|
|
if wtyp != wire.VarintType {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfUint64(v), n, nil
|
|
case protoreflect.Sfixed32Kind:
|
|
if wtyp != wire.Fixed32Type {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeFixed32(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfInt32(int32(v)), n, nil
|
|
case protoreflect.Fixed32Kind:
|
|
if wtyp != wire.Fixed32Type {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeFixed32(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfUint32(uint32(v)), n, nil
|
|
case protoreflect.FloatKind:
|
|
if wtyp != wire.Fixed32Type {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeFixed32(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v))), n, nil
|
|
case protoreflect.Sfixed64Kind:
|
|
if wtyp != wire.Fixed64Type {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeFixed64(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfInt64(int64(v)), n, nil
|
|
case protoreflect.Fixed64Kind:
|
|
if wtyp != wire.Fixed64Type {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeFixed64(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfUint64(v), n, nil
|
|
case protoreflect.DoubleKind:
|
|
if wtyp != wire.Fixed64Type {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeFixed64(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfFloat64(math.Float64frombits(v)), n, nil
|
|
case protoreflect.StringKind:
|
|
if wtyp != wire.BytesType {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
if strs.EnforceUTF8(fd) && !utf8.Valid(v) {
|
|
return protoreflect.Value{}, 0, errors.InvalidUTF8(string(fd.FullName()))
|
|
}
|
|
return protoreflect.ValueOfString(string(v)), n, nil
|
|
case protoreflect.BytesKind:
|
|
if wtyp != wire.BytesType {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfBytes(append(([]byte)(nil), v...)), n, nil
|
|
case protoreflect.MessageKind:
|
|
if wtyp != wire.BytesType {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfBytes(v), n, nil
|
|
case protoreflect.GroupKind:
|
|
if wtyp != wire.StartGroupType {
|
|
return val, 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeGroup(fd.Number(), b)
|
|
if n < 0 {
|
|
return val, 0, wire.ParseError(n)
|
|
}
|
|
return protoreflect.ValueOfBytes(v), n, nil
|
|
default:
|
|
return val, 0, errUnknown
|
|
}
|
|
}
|
|
|
|
func (o UnmarshalOptions) unmarshalList(b []byte, wtyp wire.Type, list protoreflect.List, fd protoreflect.FieldDescriptor) (n int, err error) {
|
|
switch fd.Kind() {
|
|
case protoreflect.BoolKind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeVarint(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfBool(wire.DecodeBool(v)))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.VarintType {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfBool(wire.DecodeBool(v)))
|
|
return n, nil
|
|
case protoreflect.EnumKind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeVarint(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfEnum(protoreflect.EnumNumber(v)))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.VarintType {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfEnum(protoreflect.EnumNumber(v)))
|
|
return n, nil
|
|
case protoreflect.Int32Kind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeVarint(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfInt32(int32(v)))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.VarintType {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfInt32(int32(v)))
|
|
return n, nil
|
|
case protoreflect.Sint32Kind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeVarint(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfInt32(int32(wire.DecodeZigZag(v & math.MaxUint32))))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.VarintType {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfInt32(int32(wire.DecodeZigZag(v & math.MaxUint32))))
|
|
return n, nil
|
|
case protoreflect.Uint32Kind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeVarint(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfUint32(uint32(v)))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.VarintType {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfUint32(uint32(v)))
|
|
return n, nil
|
|
case protoreflect.Int64Kind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeVarint(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfInt64(int64(v)))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.VarintType {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfInt64(int64(v)))
|
|
return n, nil
|
|
case protoreflect.Sint64Kind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeVarint(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfInt64(wire.DecodeZigZag(v)))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.VarintType {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfInt64(wire.DecodeZigZag(v)))
|
|
return n, nil
|
|
case protoreflect.Uint64Kind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeVarint(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfUint64(v))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.VarintType {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeVarint(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfUint64(v))
|
|
return n, nil
|
|
case protoreflect.Sfixed32Kind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeFixed32(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfInt32(int32(v)))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.Fixed32Type {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeFixed32(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfInt32(int32(v)))
|
|
return n, nil
|
|
case protoreflect.Fixed32Kind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeFixed32(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfUint32(uint32(v)))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.Fixed32Type {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeFixed32(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfUint32(uint32(v)))
|
|
return n, nil
|
|
case protoreflect.FloatKind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeFixed32(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v))))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.Fixed32Type {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeFixed32(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v))))
|
|
return n, nil
|
|
case protoreflect.Sfixed64Kind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeFixed64(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfInt64(int64(v)))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.Fixed64Type {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeFixed64(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfInt64(int64(v)))
|
|
return n, nil
|
|
case protoreflect.Fixed64Kind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeFixed64(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfUint64(v))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.Fixed64Type {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeFixed64(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfUint64(v))
|
|
return n, nil
|
|
case protoreflect.DoubleKind:
|
|
if wtyp == wire.BytesType {
|
|
buf, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
for len(buf) > 0 {
|
|
v, n := wire.ConsumeFixed64(buf)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
buf = buf[n:]
|
|
list.Append(protoreflect.ValueOfFloat64(math.Float64frombits(v)))
|
|
}
|
|
return n, nil
|
|
}
|
|
if wtyp != wire.Fixed64Type {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeFixed64(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfFloat64(math.Float64frombits(v)))
|
|
return n, nil
|
|
case protoreflect.StringKind:
|
|
if wtyp != wire.BytesType {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
if strs.EnforceUTF8(fd) && !utf8.Valid(v) {
|
|
return 0, errors.InvalidUTF8(string(fd.FullName()))
|
|
}
|
|
list.Append(protoreflect.ValueOfString(string(v)))
|
|
return n, nil
|
|
case protoreflect.BytesKind:
|
|
if wtyp != wire.BytesType {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
list.Append(protoreflect.ValueOfBytes(append(([]byte)(nil), v...)))
|
|
return n, nil
|
|
case protoreflect.MessageKind:
|
|
if wtyp != wire.BytesType {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeBytes(b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
m := list.NewMessage()
|
|
if err := o.unmarshalMessage(v, m); err != nil {
|
|
return 0, err
|
|
}
|
|
list.Append(protoreflect.ValueOf(m))
|
|
return n, nil
|
|
case protoreflect.GroupKind:
|
|
if wtyp != wire.StartGroupType {
|
|
return 0, errUnknown
|
|
}
|
|
v, n := wire.ConsumeGroup(fd.Number(), b)
|
|
if n < 0 {
|
|
return 0, wire.ParseError(n)
|
|
}
|
|
m := list.NewMessage()
|
|
if err := o.unmarshalMessage(v, m); err != nil {
|
|
return 0, err
|
|
}
|
|
list.Append(protoreflect.ValueOf(m))
|
|
return n, nil
|
|
default:
|
|
return 0, errUnknown
|
|
}
|
|
}
|