Scott Baker | 105df15 | 2020-04-13 15:55:14 -0700 | [diff] [blame^] | 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package impl |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "reflect" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "sync" |
| 13 | "sync/atomic" |
| 14 | |
| 15 | "google.golang.org/protobuf/internal/genname" |
| 16 | "google.golang.org/protobuf/reflect/protoreflect" |
| 17 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 18 | piface "google.golang.org/protobuf/runtime/protoiface" |
| 19 | ) |
| 20 | |
| 21 | // MessageInfo provides protobuf related functionality for a given Go type |
| 22 | // that represents a message. A given instance of MessageInfo is tied to |
| 23 | // exactly one Go type, which must be a pointer to a struct type. |
| 24 | // |
| 25 | // The exported fields must be populated before any methods are called |
| 26 | // and cannot be mutated after set. |
| 27 | type MessageInfo struct { |
| 28 | // GoReflectType is the underlying message Go type and must be populated. |
| 29 | GoReflectType reflect.Type // pointer to struct |
| 30 | |
| 31 | // Desc is the underlying message descriptor type and must be populated. |
| 32 | Desc pref.MessageDescriptor |
| 33 | |
| 34 | // Exporter must be provided in a purego environment in order to provide |
| 35 | // access to unexported fields. |
| 36 | Exporter exporter |
| 37 | |
| 38 | // OneofWrappers is list of pointers to oneof wrapper struct types. |
| 39 | OneofWrappers []interface{} |
| 40 | |
| 41 | initMu sync.Mutex // protects all unexported fields |
| 42 | initDone uint32 |
| 43 | |
| 44 | reflectMessageInfo // for reflection implementation |
| 45 | coderMessageInfo // for fast-path method implementations |
| 46 | } |
| 47 | |
| 48 | // exporter is a function that returns a reference to the ith field of v, |
| 49 | // where v is a pointer to a struct. It returns nil if it does not support |
| 50 | // exporting the requested field (e.g., already exported). |
| 51 | type exporter func(v interface{}, i int) interface{} |
| 52 | |
| 53 | // getMessageInfo returns the MessageInfo for any message type that |
| 54 | // is generated by our implementation of protoc-gen-go (for v2 and on). |
| 55 | // If it is unable to obtain a MessageInfo, it returns nil. |
| 56 | func getMessageInfo(mt reflect.Type) *MessageInfo { |
| 57 | m, ok := reflect.Zero(mt).Interface().(pref.ProtoMessage) |
| 58 | if !ok { |
| 59 | return nil |
| 60 | } |
| 61 | mr, ok := m.ProtoReflect().(interface{ ProtoMessageInfo() *MessageInfo }) |
| 62 | if !ok { |
| 63 | return nil |
| 64 | } |
| 65 | return mr.ProtoMessageInfo() |
| 66 | } |
| 67 | |
| 68 | func (mi *MessageInfo) init() { |
| 69 | // This function is called in the hot path. Inline the sync.Once logic, |
| 70 | // since allocating a closure for Once.Do is expensive. |
| 71 | // Keep init small to ensure that it can be inlined. |
| 72 | if atomic.LoadUint32(&mi.initDone) == 0 { |
| 73 | mi.initOnce() |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func (mi *MessageInfo) initOnce() { |
| 78 | mi.initMu.Lock() |
| 79 | defer mi.initMu.Unlock() |
| 80 | if mi.initDone == 1 { |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | t := mi.GoReflectType |
| 85 | if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct { |
| 86 | panic(fmt.Sprintf("got %v, want *struct kind", t)) |
| 87 | } |
| 88 | t = t.Elem() |
| 89 | |
| 90 | si := mi.makeStructInfo(t) |
| 91 | mi.makeReflectFuncs(t, si) |
| 92 | mi.makeCoderMethods(t, si) |
| 93 | |
| 94 | atomic.StoreUint32(&mi.initDone, 1) |
| 95 | } |
| 96 | |
| 97 | // getPointer returns the pointer for a message, which should be of |
| 98 | // the type of the MessageInfo. If the message is of a different type, |
| 99 | // it returns ok==false. |
| 100 | func (mi *MessageInfo) getPointer(m pref.Message) (p pointer, ok bool) { |
| 101 | switch m := m.(type) { |
| 102 | case *messageState: |
| 103 | return m.pointer(), m.messageInfo() == mi |
| 104 | case *messageReflectWrapper: |
| 105 | return m.pointer(), m.messageInfo() == mi |
| 106 | } |
| 107 | return pointer{}, false |
| 108 | } |
| 109 | |
| 110 | type ( |
| 111 | SizeCache = int32 |
| 112 | WeakFields = map[int32]piface.MessageV1 |
| 113 | UnknownFields = []byte |
| 114 | ExtensionFields = map[int32]ExtensionField |
| 115 | ) |
| 116 | |
| 117 | var ( |
| 118 | sizecacheType = reflect.TypeOf(SizeCache(0)) |
| 119 | weakFieldsType = reflect.TypeOf(WeakFields(nil)) |
| 120 | unknownFieldsType = reflect.TypeOf(UnknownFields(nil)) |
| 121 | extensionFieldsType = reflect.TypeOf(ExtensionFields(nil)) |
| 122 | ) |
| 123 | |
| 124 | type structInfo struct { |
| 125 | sizecacheOffset offset |
| 126 | weakOffset offset |
| 127 | unknownOffset offset |
| 128 | extensionOffset offset |
| 129 | |
| 130 | fieldsByNumber map[pref.FieldNumber]reflect.StructField |
| 131 | oneofsByName map[pref.Name]reflect.StructField |
| 132 | oneofWrappersByType map[reflect.Type]pref.FieldNumber |
| 133 | oneofWrappersByNumber map[pref.FieldNumber]reflect.Type |
| 134 | } |
| 135 | |
| 136 | func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo { |
| 137 | si := structInfo{ |
| 138 | sizecacheOffset: invalidOffset, |
| 139 | weakOffset: invalidOffset, |
| 140 | unknownOffset: invalidOffset, |
| 141 | extensionOffset: invalidOffset, |
| 142 | |
| 143 | fieldsByNumber: map[pref.FieldNumber]reflect.StructField{}, |
| 144 | oneofsByName: map[pref.Name]reflect.StructField{}, |
| 145 | oneofWrappersByType: map[reflect.Type]pref.FieldNumber{}, |
| 146 | oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{}, |
| 147 | } |
| 148 | |
| 149 | fieldLoop: |
| 150 | for i := 0; i < t.NumField(); i++ { |
| 151 | switch f := t.Field(i); f.Name { |
| 152 | case genname.SizeCache, genname.SizeCacheA: |
| 153 | if f.Type == sizecacheType { |
| 154 | si.sizecacheOffset = offsetOf(f, mi.Exporter) |
| 155 | } |
| 156 | case genname.WeakFields, genname.WeakFieldsA: |
| 157 | if f.Type == weakFieldsType { |
| 158 | si.weakOffset = offsetOf(f, mi.Exporter) |
| 159 | } |
| 160 | case genname.UnknownFields, genname.UnknownFieldsA: |
| 161 | if f.Type == unknownFieldsType { |
| 162 | si.unknownOffset = offsetOf(f, mi.Exporter) |
| 163 | } |
| 164 | case genname.ExtensionFields, genname.ExtensionFieldsA, genname.ExtensionFieldsB: |
| 165 | if f.Type == extensionFieldsType { |
| 166 | si.extensionOffset = offsetOf(f, mi.Exporter) |
| 167 | } |
| 168 | default: |
| 169 | for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") { |
| 170 | if len(s) > 0 && strings.Trim(s, "0123456789") == "" { |
| 171 | n, _ := strconv.ParseUint(s, 10, 64) |
| 172 | si.fieldsByNumber[pref.FieldNumber(n)] = f |
| 173 | continue fieldLoop |
| 174 | } |
| 175 | } |
| 176 | if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 { |
| 177 | si.oneofsByName[pref.Name(s)] = f |
| 178 | continue fieldLoop |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Derive a mapping of oneof wrappers to fields. |
| 184 | oneofWrappers := mi.OneofWrappers |
| 185 | for _, method := range []string{"XXX_OneofFuncs", "XXX_OneofWrappers"} { |
| 186 | if fn, ok := reflect.PtrTo(t).MethodByName(method); ok { |
| 187 | for _, v := range fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))}) { |
| 188 | if vs, ok := v.Interface().([]interface{}); ok { |
| 189 | oneofWrappers = vs |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | for _, v := range oneofWrappers { |
| 195 | tf := reflect.TypeOf(v).Elem() |
| 196 | f := tf.Field(0) |
| 197 | for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") { |
| 198 | if len(s) > 0 && strings.Trim(s, "0123456789") == "" { |
| 199 | n, _ := strconv.ParseUint(s, 10, 64) |
| 200 | si.oneofWrappersByType[tf] = pref.FieldNumber(n) |
| 201 | si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf |
| 202 | break |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return si |
| 208 | } |
| 209 | |
| 210 | func (mi *MessageInfo) New() protoreflect.Message { |
| 211 | return mi.MessageOf(reflect.New(mi.GoReflectType.Elem()).Interface()) |
| 212 | } |
| 213 | func (mi *MessageInfo) Zero() protoreflect.Message { |
| 214 | return mi.MessageOf(reflect.Zero(mi.GoReflectType).Interface()) |
| 215 | } |
| 216 | func (mi *MessageInfo) Descriptor() protoreflect.MessageDescriptor { return mi.Desc } |