Expose the thorny part of the oneof metadata interpretation.
This makes it very easy for other code to understand what to do with
incoming data that references oneof fields.
diff --git a/proto/text_parser.go b/proto/text_parser.go
index 2e2a67f..8bce369 100644
--- a/proto/text_parser.go
+++ b/proto/text_parser.go
@@ -532,34 +532,12 @@
fi, props, ok := structFieldByName(sprops, name)
if ok {
dst = sv.Field(fi)
- } else {
- // Maybe it is a oneof.
- // TODO: If this shows in profiles, cache the mapping.
- for _, oot := range sprops.oneofTypes {
- sp := reflect.ValueOf(oot).Type() // *T
- var p Properties
- p.Parse(sp.Elem().Field(0).Tag.Get("protobuf"))
- if p.OrigName != name {
- continue
- }
- nv := reflect.New(sp.Elem())
- dst = nv.Elem().Field(0)
- props = &p
- // There will be exactly one interface field that
- // this new value is assignable to.
- for i := 0; i < st.NumField(); i++ {
- f := st.Field(i)
- if f.Type.Kind() != reflect.Interface {
- continue
- }
- if !nv.Type().AssignableTo(f.Type) {
- continue
- }
- sv.Field(i).Set(nv)
- break
- }
- break
- }
+ } else if oop, ok := sprops.OneofTypes[name]; ok {
+ // It is a oneof.
+ props = oop.Prop
+ nv := reflect.New(oop.Type.Elem())
+ dst = nv.Elem().Field(0)
+ sv.Field(oop.Field).Set(nv)
}
if !dst.IsValid() {
return p.errorf("unknown field name %q in %v", name, st)