reflect/protoreflect: add alternative message reflection API
Added API:
Message.Len
Message.Range
Message.Has
Message.Clear
Message.Get
Message.Set
Message.Mutable
Message.NewMessage
Message.WhichOneof
Message.GetUnknown
Message.SetUnknown
Deprecated API (to be removed in subsequent CL):
Message.KnownFields
Message.UnknownFields
The primary difference with the new API is that the top-level
Message methods are keyed by FieldDescriptor rather than FieldNumber
with the following semantics:
* For known fields, the FieldDescriptor must exactly match the
field descriptor known by the message.
* For extension fields, the FieldDescriptor must implement ExtensionType,
where ContainingMessage.FullName matches the message name, and
the field number is within the message's extension range.
When setting an extension field, it automatically stores
the extension type information.
* Extension fields are always considered nullable,
implying that repeated extension fields are nullable.
That is, you can distinguish between a unpopulated list and an empty list.
* Message.Get always returns a valid Value even if unpopulated.
The behavior is already well-defined for scalars, but for unpopulated
composite types, it now returns an empty read-only version of it.
Change-Id: Ia120630b4db221aeaaf743d0f64160e1a61a0f61
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/175458
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/proto/decode_gen.go b/proto/decode_gen.go
index 36071e7..0589473 100644
--- a/proto/decode_gen.go
+++ b/proto/decode_gen.go
@@ -18,8 +18,8 @@
// 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, num wire.Number, field protoreflect.FieldDescriptor) (val protoreflect.Value, n int, err error) {
- switch field.Kind() {
+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
@@ -154,9 +154,9 @@
if n < 0 {
return val, 0, wire.ParseError(n)
}
- if field.Syntax() == protoreflect.Proto3 && !utf8.Valid(v) {
+ if fd.Syntax() == protoreflect.Proto3 && !utf8.Valid(v) {
var nerr errors.NonFatal
- nerr.AppendInvalidUTF8(string(field.FullName()))
+ nerr.AppendInvalidUTF8(string(fd.FullName()))
return protoreflect.ValueOf(string(v)), n, nerr.E
}
return protoreflect.ValueOf(string(v)), n, nil
@@ -182,7 +182,7 @@
if wtyp != wire.StartGroupType {
return val, 0, errUnknown
}
- v, n := wire.ConsumeGroup(num, b)
+ v, n := wire.ConsumeGroup(fd.Number(), b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
@@ -192,9 +192,9 @@
}
}
-func (o UnmarshalOptions) unmarshalList(b []byte, wtyp wire.Type, num wire.Number, list protoreflect.List, field protoreflect.FieldDescriptor) (n int, err error) {
+func (o UnmarshalOptions) unmarshalList(b []byte, wtyp wire.Type, list protoreflect.List, fd protoreflect.FieldDescriptor) (n int, err error) {
var nerr errors.NonFatal
- switch field.Kind() {
+ switch fd.Kind() {
case protoreflect.BoolKind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
@@ -553,8 +553,8 @@
if n < 0 {
return 0, wire.ParseError(n)
}
- if field.Syntax() == protoreflect.Proto3 && !utf8.Valid(v) {
- nerr.AppendInvalidUTF8(string(field.FullName()))
+ if fd.Syntax() == protoreflect.Proto3 && !utf8.Valid(v) {
+ nerr.AppendInvalidUTF8(string(fd.FullName()))
}
list.Append(protoreflect.ValueOf(string(v)))
return n, nerr.E
@@ -586,7 +586,7 @@
if wtyp != wire.StartGroupType {
return 0, errUnknown
}
- v, n := wire.ConsumeGroup(num, b)
+ v, n := wire.ConsumeGroup(fd.Number(), b)
if n < 0 {
return 0, wire.ParseError(n)
}