protobuf-go/internal/impl/message_reflect_field.go

544 lines
15 KiB
Go
Raw Normal View History

// 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.
package impl
import (
"fmt"
"math"
"reflect"
"sync"
"google.golang.org/protobuf/internal/flags"
pref "google.golang.org/protobuf/reflect/protoreflect"
preg "google.golang.org/protobuf/reflect/protoregistry"
)
type fieldInfo struct {
fieldDesc pref.FieldDescriptor
internal/impl: add fast-path marshal implementation This is a port of the v1 table marshaler, with some substantial cleanup and refactoring. Benchstat results from the protobuf reference benchmark data comparing the v1 package with v2, with AllowPartial:true set for the new package. This is not an apples-to-apples comparison, since v1 doesn't have a way to disable required field checks. Required field checks in v2 package currently go through reflection, which performs terribly; my initial experimentation indicates that fast-path required field checks will not add a large amount of cost; these results are incomplete but not wholly inaccurate. name old time/op new time/op delta /dataset.google_message3_1.pb/Marshal-12 219ms ± 1% 232ms ± 1% +5.85% (p=0.004 n=6+5) /dataset.google_message2.pb/Marshal-12 261µs ± 3% 248µs ± 1% -5.14% (p=0.002 n=6+6) /dataset.google_message1_proto2.pb/Marshal-12 681ns ± 2% 637ns ± 3% -6.53% (p=0.002 n=6+6) /dataset.google_message1_proto3.pb/Marshal-12 1.10µs ± 8% 0.99µs ± 3% -9.63% (p=0.002 n=6+6) /dataset.google_message3_3.pb/Marshal-12 44.2ms ± 3% 35.2ms ± 1% -20.28% (p=0.004 n=6+5) /dataset.google_message4.pb/Marshal-12 91.4ms ± 2% 94.9ms ± 2% +3.78% (p=0.002 n=6+6) /dataset.google_message3_2.pb/Marshal-12 78.7ms ± 6% 80.8ms ± 4% ~ (p=0.310 n=6+6) /dataset.google_message3_4.pb/Marshal-12 10.6ms ± 3% 10.6ms ± 8% ~ (p=0.662 n=5+6) /dataset.google_message3_5.pb/Marshal-12 675ms ± 4% 510ms ± 2% -24.40% (p=0.002 n=6+6) /dataset.google_message3_1.pb/Marshal 219ms ± 1% 236ms ± 7% +8.06% (p=0.004 n=5+6) /dataset.google_message2.pb/Marshal 257µs ± 1% 250µs ± 3% ~ (p=0.052 n=5+6) /dataset.google_message1_proto2.pb/Marshal 685ns ± 1% 628ns ± 1% -8.41% (p=0.008 n=5+5) /dataset.google_message1_proto3.pb/Marshal 1.08µs ± 1% 0.98µs ± 2% -9.31% (p=0.004 n=5+6) /dataset.google_message3_3.pb/Marshal 43.7ms ± 1% 35.1ms ± 1% -19.76% (p=0.002 n=6+6) /dataset.google_message4.pb/Marshal 93.4ms ± 4% 94.9ms ± 2% ~ (p=0.180 n=6+6) /dataset.google_message3_2.pb/Marshal 105ms ± 2% 98ms ± 7% -6.81% (p=0.009 n=5+6) /dataset.google_message3_4.pb/Marshal 16.3ms ± 6% 15.7ms ± 3% -3.44% (p=0.041 n=6+6) /dataset.google_message3_5.pb/Marshal 676ms ± 4% 504ms ± 2% -25.50% (p=0.004 n=6+5) Change-Id: I72cc4597117f4cf5d236ef505777d49dd4a5f75d Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/171020 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-04-01 13:49:56 -07:00
// These fields are used for protobuf reflection support.
has func(pointer) bool
clear func(pointer)
get func(pointer) pref.Value
set func(pointer, pref.Value)
mutable func(pointer) pref.Value
newMessage func() pref.Message
newField func() pref.Value
}
internal/impl: add runtime support for aberrant messages Implement support in the protobuf runtime to better understand message types that are not generated by the official generator. In particular: * Add a best-effort implementation of protobuf reflection for "non-nullable" fields which are supposed to be represented by *T, but are instead represented by a T. "Non-nullable" message fields report presence based on whether the message is the zero Go value. * We do NOT implement support for "non-nullable" fields in the table-driven implementation since we assume that the aberrant messages that we care about have a Marshal and Unmarshal method. * We better handle custom messages that implement Marshal and Unmarshal, but do NOT implement Merge. In that case, we implement merge in terms of a back-to-back marshal and unmarshal. * We better tolerate the situations where a protobuf message field cannot be mapped to a Go struct field since the latter is missing. In such cases, reflection treats the field as if it were unpopulated. Setting such fields will panic. This change allows the runtime to handle all message types declared in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection, Marshal, Unmarshal, Reset, Merge, and Equal all work. The only types that still do not fully work are: * "k8s.io/api/authentication/v1".ExtraValue * "k8s.io/api/authentication/v1beta1".ExtraValue * "k8s.io/api/authorization/v1".ExtraValue * "k8s.io/api/authorization/v1beta1".ExtraValue * "k8s.io/api/certificates/v1".ExtraValue * "k8s.io/api/certificates/v1beta1".ExtraValue * "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime * "k8s.io/apimachinery/pkg/apis/meta/v1".Time * "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs While Marshal, Unmarshal, Reset, and Merge continue to work, protobuf reflection and any functionality that depends on it (e.g., prototext, protojson, Equal, etc.) will not work. Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869 Trust: Joe Tsai <joetsai@digital-static.net> Trust: Joe Tsai <thebrokentoaster@gmail.com> Reviewed-by: Damien Neil <dneil@google.com>
2021-03-11 02:50:41 -08:00
func fieldInfoForMissing(fd pref.FieldDescriptor) fieldInfo {
// This never occurs for generated message types.
// It implies that a hand-crafted type has missing Go fields
// for specific protobuf message fields.
return fieldInfo{
fieldDesc: fd,
has: func(p pointer) bool {
return false
},
clear: func(p pointer) {
panic("missing Go struct field for " + string(fd.FullName()))
},
get: func(p pointer) pref.Value {
return fd.Default()
},
set: func(p pointer, v pref.Value) {
panic("missing Go struct field for " + string(fd.FullName()))
},
mutable: func(p pointer) pref.Value {
panic("missing Go struct field for " + string(fd.FullName()))
},
newMessage: func() pref.Message {
panic("missing Go struct field for " + string(fd.FullName()))
},
newField: func() pref.Value {
if v := fd.Default(); v.IsValid() {
return v
}
panic("missing Go struct field for " + string(fd.FullName()))
},
}
}
func fieldInfoForOneof(fd pref.FieldDescriptor, fs reflect.StructField, x exporter, ot reflect.Type) fieldInfo {
ft := fs.Type
if ft.Kind() != reflect.Interface {
panic(fmt.Sprintf("field %v has invalid type: got %v, want interface kind", fd.FullName(), ft))
}
if ot.Kind() != reflect.Struct {
panic(fmt.Sprintf("field %v has invalid type: got %v, want struct kind", fd.FullName(), ot))
}
if !reflect.PtrTo(ot).Implements(ft) {
panic(fmt.Sprintf("field %v has invalid type: %v does not implement %v", fd.FullName(), ot, ft))
}
conv := NewConverter(ot.Field(0).Type, fd)
isMessage := fd.Message() != nil
// TODO: Implement unsafe fast path?
fieldOffset := offsetOf(fs, x)
return fieldInfo{
// NOTE: The logic below intentionally assumes that oneof fields are
// well-formatted. That is, the oneof interface never contains a
// typed nil pointer to one of the wrapper structs.
fieldDesc: fd,
has: func(p pointer) bool {
if p.IsNil() {
return false
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if rv.IsNil() || rv.Elem().Type().Elem() != ot || rv.Elem().IsNil() {
return false
}
return true
},
clear: func(p pointer) {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if rv.IsNil() || rv.Elem().Type().Elem() != ot {
// NOTE: We intentionally don't check for rv.Elem().IsNil()
// so that (*OneofWrapperType)(nil) gets cleared to nil.
return
}
rv.Set(reflect.Zero(rv.Type()))
},
get: func(p pointer) pref.Value {
if p.IsNil() {
return conv.Zero()
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if rv.IsNil() || rv.Elem().Type().Elem() != ot || rv.Elem().IsNil() {
return conv.Zero()
}
rv = rv.Elem().Elem().Field(0)
return conv.PBValueOf(rv)
},
set: func(p pointer, v pref.Value) {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if rv.IsNil() || rv.Elem().Type().Elem() != ot || rv.Elem().IsNil() {
rv.Set(reflect.New(ot))
}
rv = rv.Elem().Elem().Field(0)
rv.Set(conv.GoValueOf(v))
},
mutable: func(p pointer) pref.Value {
if !isMessage {
panic(fmt.Sprintf("field %v with invalid Mutable call on field with non-composite type", fd.FullName()))
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if rv.IsNil() || rv.Elem().Type().Elem() != ot || rv.Elem().IsNil() {
rv.Set(reflect.New(ot))
}
rv = rv.Elem().Elem().Field(0)
internal/impl: add runtime support for aberrant messages Implement support in the protobuf runtime to better understand message types that are not generated by the official generator. In particular: * Add a best-effort implementation of protobuf reflection for "non-nullable" fields which are supposed to be represented by *T, but are instead represented by a T. "Non-nullable" message fields report presence based on whether the message is the zero Go value. * We do NOT implement support for "non-nullable" fields in the table-driven implementation since we assume that the aberrant messages that we care about have a Marshal and Unmarshal method. * We better handle custom messages that implement Marshal and Unmarshal, but do NOT implement Merge. In that case, we implement merge in terms of a back-to-back marshal and unmarshal. * We better tolerate the situations where a protobuf message field cannot be mapped to a Go struct field since the latter is missing. In such cases, reflection treats the field as if it were unpopulated. Setting such fields will panic. This change allows the runtime to handle all message types declared in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection, Marshal, Unmarshal, Reset, Merge, and Equal all work. The only types that still do not fully work are: * "k8s.io/api/authentication/v1".ExtraValue * "k8s.io/api/authentication/v1beta1".ExtraValue * "k8s.io/api/authorization/v1".ExtraValue * "k8s.io/api/authorization/v1beta1".ExtraValue * "k8s.io/api/certificates/v1".ExtraValue * "k8s.io/api/certificates/v1beta1".ExtraValue * "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime * "k8s.io/apimachinery/pkg/apis/meta/v1".Time * "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs While Marshal, Unmarshal, Reset, and Merge continue to work, protobuf reflection and any functionality that depends on it (e.g., prototext, protojson, Equal, etc.) will not work. Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869 Trust: Joe Tsai <joetsai@digital-static.net> Trust: Joe Tsai <thebrokentoaster@gmail.com> Reviewed-by: Damien Neil <dneil@google.com>
2021-03-11 02:50:41 -08:00
if rv.Kind() == reflect.Ptr && rv.IsNil() {
rv.Set(conv.GoValueOf(pref.ValueOfMessage(conv.New().Message())))
}
return conv.PBValueOf(rv)
},
newMessage: func() pref.Message {
return conv.New().Message()
},
newField: func() pref.Value {
return conv.New()
},
}
}
func fieldInfoForMap(fd pref.FieldDescriptor, fs reflect.StructField, x exporter) fieldInfo {
ft := fs.Type
if ft.Kind() != reflect.Map {
panic(fmt.Sprintf("field %v has invalid type: got %v, want map kind", fd.FullName(), ft))
}
conv := NewConverter(ft, fd)
// TODO: Implement unsafe fast path?
fieldOffset := offsetOf(fs, x)
return fieldInfo{
fieldDesc: fd,
has: func(p pointer) bool {
if p.IsNil() {
return false
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
return rv.Len() > 0
},
clear: func(p pointer) {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
rv.Set(reflect.Zero(rv.Type()))
},
get: func(p pointer) pref.Value {
if p.IsNil() {
return conv.Zero()
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if rv.Len() == 0 {
return conv.Zero()
}
return conv.PBValueOf(rv)
},
set: func(p pointer, v pref.Value) {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
pv := conv.GoValueOf(v)
if pv.IsNil() {
panic(fmt.Sprintf("map field %v cannot be set with read-only value", fd.FullName()))
}
rv.Set(pv)
},
mutable: func(p pointer) pref.Value {
v := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if v.IsNil() {
v.Set(reflect.MakeMap(fs.Type))
}
return conv.PBValueOf(v)
},
newField: func() pref.Value {
return conv.New()
},
}
}
func fieldInfoForList(fd pref.FieldDescriptor, fs reflect.StructField, x exporter) fieldInfo {
ft := fs.Type
if ft.Kind() != reflect.Slice {
panic(fmt.Sprintf("field %v has invalid type: got %v, want slice kind", fd.FullName(), ft))
}
conv := NewConverter(reflect.PtrTo(ft), fd)
// TODO: Implement unsafe fast path?
fieldOffset := offsetOf(fs, x)
return fieldInfo{
fieldDesc: fd,
has: func(p pointer) bool {
if p.IsNil() {
return false
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
return rv.Len() > 0
},
clear: func(p pointer) {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
rv.Set(reflect.Zero(rv.Type()))
},
get: func(p pointer) pref.Value {
if p.IsNil() {
return conv.Zero()
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type)
if rv.Elem().Len() == 0 {
return conv.Zero()
}
return conv.PBValueOf(rv)
},
set: func(p pointer, v pref.Value) {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
pv := conv.GoValueOf(v)
if pv.IsNil() {
panic(fmt.Sprintf("list field %v cannot be set with read-only value", fd.FullName()))
}
rv.Set(pv.Elem())
},
mutable: func(p pointer) pref.Value {
v := p.Apply(fieldOffset).AsValueOf(fs.Type)
return conv.PBValueOf(v)
},
newField: func() pref.Value {
return conv.New()
},
}
}
var (
nilBytes = reflect.ValueOf([]byte(nil))
emptyBytes = reflect.ValueOf([]byte{})
)
func fieldInfoForScalar(fd pref.FieldDescriptor, fs reflect.StructField, x exporter) fieldInfo {
ft := fs.Type
nullable := fd.HasPresence()
isBytes := ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8
if nullable {
if ft.Kind() != reflect.Ptr && ft.Kind() != reflect.Slice {
internal/impl: add runtime support for aberrant messages Implement support in the protobuf runtime to better understand message types that are not generated by the official generator. In particular: * Add a best-effort implementation of protobuf reflection for "non-nullable" fields which are supposed to be represented by *T, but are instead represented by a T. "Non-nullable" message fields report presence based on whether the message is the zero Go value. * We do NOT implement support for "non-nullable" fields in the table-driven implementation since we assume that the aberrant messages that we care about have a Marshal and Unmarshal method. * We better handle custom messages that implement Marshal and Unmarshal, but do NOT implement Merge. In that case, we implement merge in terms of a back-to-back marshal and unmarshal. * We better tolerate the situations where a protobuf message field cannot be mapped to a Go struct field since the latter is missing. In such cases, reflection treats the field as if it were unpopulated. Setting such fields will panic. This change allows the runtime to handle all message types declared in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection, Marshal, Unmarshal, Reset, Merge, and Equal all work. The only types that still do not fully work are: * "k8s.io/api/authentication/v1".ExtraValue * "k8s.io/api/authentication/v1beta1".ExtraValue * "k8s.io/api/authorization/v1".ExtraValue * "k8s.io/api/authorization/v1beta1".ExtraValue * "k8s.io/api/certificates/v1".ExtraValue * "k8s.io/api/certificates/v1beta1".ExtraValue * "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime * "k8s.io/apimachinery/pkg/apis/meta/v1".Time * "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs While Marshal, Unmarshal, Reset, and Merge continue to work, protobuf reflection and any functionality that depends on it (e.g., prototext, protojson, Equal, etc.) will not work. Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869 Trust: Joe Tsai <joetsai@digital-static.net> Trust: Joe Tsai <thebrokentoaster@gmail.com> Reviewed-by: Damien Neil <dneil@google.com>
2021-03-11 02:50:41 -08:00
// This never occurs for generated message types.
// Despite the protobuf type system specifying presence,
// the Go field type cannot represent it.
nullable = false
}
if ft.Kind() == reflect.Ptr {
ft = ft.Elem()
}
}
conv := NewConverter(ft, fd)
// TODO: Implement unsafe fast path?
fieldOffset := offsetOf(fs, x)
return fieldInfo{
fieldDesc: fd,
has: func(p pointer) bool {
if p.IsNil() {
return false
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if nullable {
return !rv.IsNil()
}
switch rv.Kind() {
case reflect.Bool:
return rv.Bool()
case reflect.Int32, reflect.Int64:
return rv.Int() != 0
case reflect.Uint32, reflect.Uint64:
return rv.Uint() != 0
case reflect.Float32, reflect.Float64:
return rv.Float() != 0 || math.Signbit(rv.Float())
case reflect.String, reflect.Slice:
return rv.Len() > 0
default:
panic(fmt.Sprintf("field %v has invalid type: %v", fd.FullName(), rv.Type())) // should never happen
}
},
clear: func(p pointer) {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
rv.Set(reflect.Zero(rv.Type()))
},
get: func(p pointer) pref.Value {
if p.IsNil() {
return conv.Zero()
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if nullable {
if rv.IsNil() {
return conv.Zero()
}
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
}
return conv.PBValueOf(rv)
},
set: func(p pointer, v pref.Value) {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if nullable && rv.Kind() == reflect.Ptr {
if rv.IsNil() {
rv.Set(reflect.New(ft))
}
rv = rv.Elem()
}
rv.Set(conv.GoValueOf(v))
if isBytes && rv.Len() == 0 {
if nullable {
rv.Set(emptyBytes) // preserve presence
} else {
rv.Set(nilBytes) // do not preserve presence
}
}
},
newField: func() pref.Value {
return conv.New()
},
}
}
func fieldInfoForWeakMessage(fd pref.FieldDescriptor, weakOffset offset) fieldInfo {
if !flags.ProtoLegacy {
panic("no support for proto1 weak fields")
}
var once sync.Once
var messageType pref.MessageType
lazyInit := func() {
once.Do(func() {
messageName := fd.Message().FullName()
messageType, _ = preg.GlobalTypes.FindMessageByName(messageName)
if messageType == nil {
panic(fmt.Sprintf("weak message %v for field %v is not linked in", messageName, fd.FullName()))
}
})
}
num := fd.Number()
return fieldInfo{
fieldDesc: fd,
has: func(p pointer) bool {
if p.IsNil() {
return false
}
_, ok := p.Apply(weakOffset).WeakFields().get(num)
return ok
},
clear: func(p pointer) {
p.Apply(weakOffset).WeakFields().clear(num)
},
get: func(p pointer) pref.Value {
lazyInit()
if p.IsNil() {
return pref.ValueOfMessage(messageType.Zero())
}
m, ok := p.Apply(weakOffset).WeakFields().get(num)
if !ok {
return pref.ValueOfMessage(messageType.Zero())
}
return pref.ValueOfMessage(m.ProtoReflect())
},
set: func(p pointer, v pref.Value) {
lazyInit()
m := v.Message()
if m.Descriptor() != messageType.Descriptor() {
if got, want := m.Descriptor().FullName(), messageType.Descriptor().FullName(); got != want {
panic(fmt.Sprintf("field %v has mismatching message descriptor: got %v, want %v", fd.FullName(), got, want))
}
panic(fmt.Sprintf("field %v has mismatching message descriptor: %v", fd.FullName(), m.Descriptor().FullName()))
}
p.Apply(weakOffset).WeakFields().set(num, m.Interface())
},
mutable: func(p pointer) pref.Value {
lazyInit()
fs := p.Apply(weakOffset).WeakFields()
m, ok := fs.get(num)
if !ok {
m = messageType.New().Interface()
fs.set(num, m)
}
return pref.ValueOfMessage(m.ProtoReflect())
},
newMessage: func() pref.Message {
lazyInit()
return messageType.New()
},
newField: func() pref.Value {
lazyInit()
return pref.ValueOfMessage(messageType.New())
},
}
}
func fieldInfoForMessage(fd pref.FieldDescriptor, fs reflect.StructField, x exporter) fieldInfo {
ft := fs.Type
conv := NewConverter(ft, fd)
// TODO: Implement unsafe fast path?
fieldOffset := offsetOf(fs, x)
return fieldInfo{
fieldDesc: fd,
has: func(p pointer) bool {
if p.IsNil() {
return false
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
internal/impl: add runtime support for aberrant messages Implement support in the protobuf runtime to better understand message types that are not generated by the official generator. In particular: * Add a best-effort implementation of protobuf reflection for "non-nullable" fields which are supposed to be represented by *T, but are instead represented by a T. "Non-nullable" message fields report presence based on whether the message is the zero Go value. * We do NOT implement support for "non-nullable" fields in the table-driven implementation since we assume that the aberrant messages that we care about have a Marshal and Unmarshal method. * We better handle custom messages that implement Marshal and Unmarshal, but do NOT implement Merge. In that case, we implement merge in terms of a back-to-back marshal and unmarshal. * We better tolerate the situations where a protobuf message field cannot be mapped to a Go struct field since the latter is missing. In such cases, reflection treats the field as if it were unpopulated. Setting such fields will panic. This change allows the runtime to handle all message types declared in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection, Marshal, Unmarshal, Reset, Merge, and Equal all work. The only types that still do not fully work are: * "k8s.io/api/authentication/v1".ExtraValue * "k8s.io/api/authentication/v1beta1".ExtraValue * "k8s.io/api/authorization/v1".ExtraValue * "k8s.io/api/authorization/v1beta1".ExtraValue * "k8s.io/api/certificates/v1".ExtraValue * "k8s.io/api/certificates/v1beta1".ExtraValue * "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime * "k8s.io/apimachinery/pkg/apis/meta/v1".Time * "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs While Marshal, Unmarshal, Reset, and Merge continue to work, protobuf reflection and any functionality that depends on it (e.g., prototext, protojson, Equal, etc.) will not work. Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869 Trust: Joe Tsai <joetsai@digital-static.net> Trust: Joe Tsai <thebrokentoaster@gmail.com> Reviewed-by: Damien Neil <dneil@google.com>
2021-03-11 02:50:41 -08:00
if fs.Type.Kind() != reflect.Ptr {
return !isZero(rv)
}
return !rv.IsNil()
},
clear: func(p pointer) {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
rv.Set(reflect.Zero(rv.Type()))
},
get: func(p pointer) pref.Value {
if p.IsNil() {
return conv.Zero()
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
return conv.PBValueOf(rv)
},
set: func(p pointer, v pref.Value) {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
rv.Set(conv.GoValueOf(v))
internal/impl: add runtime support for aberrant messages Implement support in the protobuf runtime to better understand message types that are not generated by the official generator. In particular: * Add a best-effort implementation of protobuf reflection for "non-nullable" fields which are supposed to be represented by *T, but are instead represented by a T. "Non-nullable" message fields report presence based on whether the message is the zero Go value. * We do NOT implement support for "non-nullable" fields in the table-driven implementation since we assume that the aberrant messages that we care about have a Marshal and Unmarshal method. * We better handle custom messages that implement Marshal and Unmarshal, but do NOT implement Merge. In that case, we implement merge in terms of a back-to-back marshal and unmarshal. * We better tolerate the situations where a protobuf message field cannot be mapped to a Go struct field since the latter is missing. In such cases, reflection treats the field as if it were unpopulated. Setting such fields will panic. This change allows the runtime to handle all message types declared in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection, Marshal, Unmarshal, Reset, Merge, and Equal all work. The only types that still do not fully work are: * "k8s.io/api/authentication/v1".ExtraValue * "k8s.io/api/authentication/v1beta1".ExtraValue * "k8s.io/api/authorization/v1".ExtraValue * "k8s.io/api/authorization/v1beta1".ExtraValue * "k8s.io/api/certificates/v1".ExtraValue * "k8s.io/api/certificates/v1beta1".ExtraValue * "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime * "k8s.io/apimachinery/pkg/apis/meta/v1".Time * "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs While Marshal, Unmarshal, Reset, and Merge continue to work, protobuf reflection and any functionality that depends on it (e.g., prototext, protojson, Equal, etc.) will not work. Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869 Trust: Joe Tsai <joetsai@digital-static.net> Trust: Joe Tsai <thebrokentoaster@gmail.com> Reviewed-by: Damien Neil <dneil@google.com>
2021-03-11 02:50:41 -08:00
if fs.Type.Kind() == reflect.Ptr && rv.IsNil() {
panic(fmt.Sprintf("field %v has invalid nil pointer", fd.FullName()))
reflect/protoreflect: clarify Get semantics on unpopulated fields Clearly specify that Get on an unpopulated field: * returns the default value for scalars * returns a mutable (but empty) List for repeated fields * returns a mutable (but empty) Map for map fields * returns an invalid value for message fields The difference in semantics between List+Maps and Messages is because protobuf semantics provide no distinction between an unpopulated and empty list or map. On the other hand, there is a semantic difference between an unpopulated message and an empty message. Default values for scalars is trivial to implement with FieldDescriptor.Default. A mutable, but empty List and Map is easy to implement for known fields since known fields are generated as a slice or map field in a struct. Since struct fields are addressable, the implementation can just return a reference to the slice or map. Repeated, extension fields are a little more tricky since extension fields are implemented under the hood as a map[FieldNumber]Extension. Rather than allocating an empty list in KnownFields.Get upon first retrieval (which presents a race), delegate the work to ExtensionFieldTypes.Register, which must occur before any Get operation. Register is not a concurrent-safe operation, so that is an excellent time to initilize empty lists. The implementation of extensions will need to be careful that Clear on a repeated field simply truncates it zero instead of deleting the object. For unpopulated messages, we return an invalid value, instead of the prior behavior of returning a typed nil-pointer to the Go type for the message. The approach is problematic because it assumes that 1) all messages are always implemented on a pointer reciever 2) a typed nil-pointer is an appropriate "read-only, but empty" message These assumptions are not true of all message types (e.g., dynamic messages). Change-Id: Ie96e6744c890308d9de738b6cf01d3b19e7e7c6a Reviewed-on: https://go-review.googlesource.com/c/150319 Reviewed-by: Damien Neil <dneil@google.com>
2018-11-19 14:26:06 -08:00
}
},
mutable: func(p pointer) pref.Value {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
internal/impl: add runtime support for aberrant messages Implement support in the protobuf runtime to better understand message types that are not generated by the official generator. In particular: * Add a best-effort implementation of protobuf reflection for "non-nullable" fields which are supposed to be represented by *T, but are instead represented by a T. "Non-nullable" message fields report presence based on whether the message is the zero Go value. * We do NOT implement support for "non-nullable" fields in the table-driven implementation since we assume that the aberrant messages that we care about have a Marshal and Unmarshal method. * We better handle custom messages that implement Marshal and Unmarshal, but do NOT implement Merge. In that case, we implement merge in terms of a back-to-back marshal and unmarshal. * We better tolerate the situations where a protobuf message field cannot be mapped to a Go struct field since the latter is missing. In such cases, reflection treats the field as if it were unpopulated. Setting such fields will panic. This change allows the runtime to handle all message types declared in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection, Marshal, Unmarshal, Reset, Merge, and Equal all work. The only types that still do not fully work are: * "k8s.io/api/authentication/v1".ExtraValue * "k8s.io/api/authentication/v1beta1".ExtraValue * "k8s.io/api/authorization/v1".ExtraValue * "k8s.io/api/authorization/v1beta1".ExtraValue * "k8s.io/api/certificates/v1".ExtraValue * "k8s.io/api/certificates/v1beta1".ExtraValue * "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime * "k8s.io/apimachinery/pkg/apis/meta/v1".Time * "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs While Marshal, Unmarshal, Reset, and Merge continue to work, protobuf reflection and any functionality that depends on it (e.g., prototext, protojson, Equal, etc.) will not work. Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869 Trust: Joe Tsai <joetsai@digital-static.net> Trust: Joe Tsai <thebrokentoaster@gmail.com> Reviewed-by: Damien Neil <dneil@google.com>
2021-03-11 02:50:41 -08:00
if fs.Type.Kind() == reflect.Ptr && rv.IsNil() {
rv.Set(conv.GoValueOf(conv.New()))
}
return conv.PBValueOf(rv)
},
newMessage: func() pref.Message {
return conv.New().Message()
},
newField: func() pref.Value {
return conv.New()
},
}
}
type oneofInfo struct {
oneofDesc pref.OneofDescriptor
which func(pointer) pref.FieldNumber
}
func makeOneofInfo(od pref.OneofDescriptor, si structInfo, x exporter) *oneofInfo {
oi := &oneofInfo{oneofDesc: od}
if od.IsSynthetic() {
fs := si.fieldsByNumber[od.Fields().Get(0).Number()]
fieldOffset := offsetOf(fs, x)
oi.which = func(p pointer) pref.FieldNumber {
if p.IsNil() {
return 0
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if rv.IsNil() { // valid on either *T or []byte
return 0
}
return od.Fields().Get(0).Number()
}
} else {
fs := si.oneofsByName[od.Name()]
fieldOffset := offsetOf(fs, x)
oi.which = func(p pointer) pref.FieldNumber {
if p.IsNil() {
return 0
}
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if rv.IsNil() {
return 0
}
rv = rv.Elem()
if rv.IsNil() {
return 0
}
return si.oneofWrappersByType[rv.Type().Elem()]
}
}
return oi
}
internal/impl: add runtime support for aberrant messages Implement support in the protobuf runtime to better understand message types that are not generated by the official generator. In particular: * Add a best-effort implementation of protobuf reflection for "non-nullable" fields which are supposed to be represented by *T, but are instead represented by a T. "Non-nullable" message fields report presence based on whether the message is the zero Go value. * We do NOT implement support for "non-nullable" fields in the table-driven implementation since we assume that the aberrant messages that we care about have a Marshal and Unmarshal method. * We better handle custom messages that implement Marshal and Unmarshal, but do NOT implement Merge. In that case, we implement merge in terms of a back-to-back marshal and unmarshal. * We better tolerate the situations where a protobuf message field cannot be mapped to a Go struct field since the latter is missing. In such cases, reflection treats the field as if it were unpopulated. Setting such fields will panic. This change allows the runtime to handle all message types declared in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection, Marshal, Unmarshal, Reset, Merge, and Equal all work. The only types that still do not fully work are: * "k8s.io/api/authentication/v1".ExtraValue * "k8s.io/api/authentication/v1beta1".ExtraValue * "k8s.io/api/authorization/v1".ExtraValue * "k8s.io/api/authorization/v1beta1".ExtraValue * "k8s.io/api/certificates/v1".ExtraValue * "k8s.io/api/certificates/v1beta1".ExtraValue * "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime * "k8s.io/apimachinery/pkg/apis/meta/v1".Time * "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs While Marshal, Unmarshal, Reset, and Merge continue to work, protobuf reflection and any functionality that depends on it (e.g., prototext, protojson, Equal, etc.) will not work. Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869 Trust: Joe Tsai <joetsai@digital-static.net> Trust: Joe Tsai <thebrokentoaster@gmail.com> Reviewed-by: Damien Neil <dneil@google.com>
2021-03-11 02:50:41 -08:00
// isZero is identical to reflect.Value.IsZero.
// TODO: Remove this when Go1.13 is the minimally supported Go version.
func isZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return math.Float64bits(v.Float()) == 0
case reflect.Complex64, reflect.Complex128:
c := v.Complex()
return math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0
case reflect.Array:
for i := 0; i < v.Len(); i++ {
if !isZero(v.Index(i)) {
return false
}
}
return true
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
return v.IsNil()
case reflect.String:
return v.Len() == 0
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
if !isZero(v.Field(i)) {
return false
}
}
return true
default:
panic(&reflect.ValueError{"reflect.Value.IsZero", v.Kind()})
}
}