Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -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 ( |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 8 | "fmt" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 9 | "reflect" |
| 10 | "strconv" |
| 11 | "strings" |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 12 | "sync" |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 13 | "sync/atomic" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 14 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 15 | pvalue "google.golang.org/protobuf/internal/value" |
| 16 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 17 | piface "google.golang.org/protobuf/runtime/protoiface" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 18 | ) |
| 19 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 20 | // MessageInfo provides protobuf related functionality for a given Go type |
| 21 | // that represents a message. A given instance of MessageInfo is tied to |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 22 | // exactly one Go type, which must be a pointer to a struct type. |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 23 | type MessageInfo struct { |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 24 | // GoType is the underlying message Go type and must be populated. |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 25 | // Once set, this field must never be mutated. |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 26 | GoType reflect.Type // pointer to struct |
| 27 | |
| 28 | // PBType is the underlying message descriptor type and must be populated. |
| 29 | // Once set, this field must never be mutated. |
| 30 | PBType pref.MessageType |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 31 | |
Joe Tsai | 0991227 | 2019-07-08 10:38:11 -0700 | [diff] [blame^] | 32 | // OneofWrappers is list of pointers to oneof wrapper struct types. |
| 33 | OneofWrappers []interface{} |
| 34 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 35 | initMu sync.Mutex // protects all unexported fields |
| 36 | initDone uint32 |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 37 | |
Damien Neil | 4ae30bb | 2019-06-20 10:12:23 -0700 | [diff] [blame] | 38 | fields map[pref.FieldNumber]*fieldInfo |
Joe Tsai | 4ec39c7 | 2019-04-03 13:40:53 -0700 | [diff] [blame] | 39 | oneofs map[pref.Name]*oneofInfo |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 40 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 41 | getUnknown func(pointer) pref.RawFields |
| 42 | setUnknown func(pointer, pref.RawFields) |
| 43 | |
| 44 | extensionMap func(pointer) *extensionMap |
| 45 | |
Damien Neil | 4ae30bb | 2019-06-20 10:12:23 -0700 | [diff] [blame] | 46 | // Information used by the fast-path methods. |
Joe Tsai | 4a539f4 | 2019-06-17 12:30:25 -0700 | [diff] [blame] | 47 | methods piface.Methods |
Damien Neil | 4ae30bb | 2019-06-20 10:12:23 -0700 | [diff] [blame] | 48 | coderMessageInfo |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 49 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 50 | extensionFieldInfosMu sync.RWMutex |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 51 | extensionFieldInfos map[pref.ExtensionType]*extensionFieldInfo |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | var prefMessageType = reflect.TypeOf((*pref.Message)(nil)).Elem() |
| 55 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 56 | // getMessageInfo returns the MessageInfo (if any) for a type. |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 57 | // |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 58 | // We find the MessageInfo by calling the ProtoReflect method on the type's |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 59 | // zero value and looking at the returned type to see if it is a |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 60 | // messageReflectWrapper. Note that the MessageInfo may still be uninitialized |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 61 | // at this point. |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 62 | func getMessageInfo(mt reflect.Type) (mi *MessageInfo, ok bool) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 63 | method, ok := mt.MethodByName("ProtoReflect") |
| 64 | if !ok { |
| 65 | return nil, false |
| 66 | } |
| 67 | if method.Type.NumIn() != 1 || method.Type.NumOut() != 1 || method.Type.Out(0) != prefMessageType { |
| 68 | return nil, false |
| 69 | } |
| 70 | ret := reflect.Zero(mt).Method(method.Index).Call(nil) |
| 71 | m, ok := ret[0].Elem().Interface().(*messageReflectWrapper) |
| 72 | if !ok { |
| 73 | return nil, ok |
| 74 | } |
| 75 | return m.mi, true |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 78 | func (mi *MessageInfo) init() { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 79 | // This function is called in the hot path. Inline the sync.Once |
| 80 | // logic, since allocating a closure for Once.Do is expensive. |
| 81 | // Keep init small to ensure that it can be inlined. |
| 82 | if atomic.LoadUint32(&mi.initDone) == 1 { |
| 83 | return |
| 84 | } |
| 85 | mi.initOnce() |
| 86 | } |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 87 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 88 | func (mi *MessageInfo) initOnce() { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 89 | mi.initMu.Lock() |
| 90 | defer mi.initMu.Unlock() |
| 91 | if mi.initDone == 1 { |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | t := mi.GoType |
| 96 | if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct { |
| 97 | panic(fmt.Sprintf("got %v, want *struct kind", t)) |
| 98 | } |
| 99 | |
| 100 | si := mi.makeStructInfo(t.Elem()) |
| 101 | mi.makeKnownFieldsFunc(si) |
Joe Tsai | 93fd968 | 2019-07-06 13:02:14 -0700 | [diff] [blame] | 102 | mi.makeUnknownFieldsFunc(t.Elem(), si) |
| 103 | mi.makeExtensionFieldsFunc(t.Elem(), si) |
Damien Neil | 4ae30bb | 2019-06-20 10:12:23 -0700 | [diff] [blame] | 104 | mi.makeMethods(t.Elem(), si) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 105 | |
| 106 | atomic.StoreUint32(&mi.initDone, 1) |
| 107 | } |
| 108 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 109 | type ( |
| 110 | SizeCache = int32 |
| 111 | UnknownFields = []byte |
| 112 | ExtensionFields = map[int32]ExtensionField |
| 113 | ) |
| 114 | |
| 115 | var ( |
| 116 | sizecacheType = reflect.TypeOf(SizeCache(0)) |
| 117 | unknownFieldsType = reflect.TypeOf(UnknownFields(nil)) |
| 118 | extensionFieldsType = reflect.TypeOf(ExtensionFields(nil)) |
| 119 | ) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 120 | |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 121 | type structInfo struct { |
Joe Tsai | 93fd968 | 2019-07-06 13:02:14 -0700 | [diff] [blame] | 122 | sizecacheOffset offset |
| 123 | extensionOffset offset |
| 124 | unknownOffset offset |
| 125 | |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 126 | fieldsByNumber map[pref.FieldNumber]reflect.StructField |
| 127 | oneofsByName map[pref.Name]reflect.StructField |
| 128 | oneofWrappersByType map[reflect.Type]pref.FieldNumber |
| 129 | oneofWrappersByNumber map[pref.FieldNumber]reflect.Type |
| 130 | } |
| 131 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 132 | func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo { |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 133 | si := structInfo{ |
Joe Tsai | 93fd968 | 2019-07-06 13:02:14 -0700 | [diff] [blame] | 134 | sizecacheOffset: invalidOffset, |
| 135 | extensionOffset: invalidOffset, |
| 136 | unknownOffset: invalidOffset, |
| 137 | |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 138 | fieldsByNumber: map[pref.FieldNumber]reflect.StructField{}, |
| 139 | oneofsByName: map[pref.Name]reflect.StructField{}, |
| 140 | oneofWrappersByType: map[reflect.Type]pref.FieldNumber{}, |
| 141 | oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{}, |
| 142 | } |
Joe Tsai | 93fd968 | 2019-07-06 13:02:14 -0700 | [diff] [blame] | 143 | |
| 144 | if f, _ := t.FieldByName("XXX_sizecache"); f.Type == sizecacheType { |
| 145 | si.sizecacheOffset = offsetOf(f) |
| 146 | } |
| 147 | if f, _ := t.FieldByName("XXX_InternalExtensions"); f.Type == extensionFieldsType { |
| 148 | si.extensionOffset = offsetOf(f) |
| 149 | } |
| 150 | if f, _ := t.FieldByName("XXX_extensions"); f.Type == extensionFieldsType { |
| 151 | si.extensionOffset = offsetOf(f) |
| 152 | } |
| 153 | if f, _ := t.FieldByName("XXX_unrecognized"); f.Type == unknownFieldsType { |
| 154 | si.unknownOffset = offsetOf(f) |
| 155 | } |
| 156 | |
| 157 | // Generate a mapping of field numbers and names to Go struct field or type. |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 158 | fieldLoop: |
| 159 | for i := 0; i < t.NumField(); i++ { |
| 160 | f := t.Field(i) |
| 161 | for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") { |
| 162 | if len(s) > 0 && strings.Trim(s, "0123456789") == "" { |
| 163 | n, _ := strconv.ParseUint(s, 10, 64) |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 164 | si.fieldsByNumber[pref.FieldNumber(n)] = f |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 165 | continue fieldLoop |
| 166 | } |
| 167 | } |
| 168 | if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 { |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 169 | si.oneofsByName[pref.Name(s)] = f |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 170 | continue fieldLoop |
| 171 | } |
| 172 | } |
Joe Tsai | 93fd968 | 2019-07-06 13:02:14 -0700 | [diff] [blame] | 173 | |
| 174 | // Derive a mapping of oneof wrappers to fields. |
Joe Tsai | 0991227 | 2019-07-08 10:38:11 -0700 | [diff] [blame^] | 175 | oneofWrappers := mi.OneofWrappers |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 176 | if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok { |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 177 | oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{}) |
| 178 | } |
| 179 | if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok { |
| 180 | oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{}) |
| 181 | } |
| 182 | for _, v := range oneofWrappers { |
| 183 | tf := reflect.TypeOf(v).Elem() |
| 184 | f := tf.Field(0) |
| 185 | for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") { |
| 186 | if len(s) > 0 && strings.Trim(s, "0123456789") == "" { |
| 187 | n, _ := strconv.ParseUint(s, 10, 64) |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 188 | si.oneofWrappersByType[tf] = pref.FieldNumber(n) |
| 189 | si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 190 | break |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
Joe Tsai | 93fd968 | 2019-07-06 13:02:14 -0700 | [diff] [blame] | 194 | |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 195 | return si |
| 196 | } |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 197 | |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 198 | // makeKnownFieldsFunc generates functions for operations that can be performed |
| 199 | // on each protobuf message field. It takes in a reflect.Type representing the |
| 200 | // Go struct and matches message fields with struct fields. |
| 201 | // |
| 202 | // This code assumes that the struct is well-formed and panics if there are |
| 203 | // any discrepancies. |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 204 | func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) { |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 205 | mi.fields = map[pref.FieldNumber]*fieldInfo{} |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 206 | for i := 0; i < mi.PBType.Descriptor().Fields().Len(); i++ { |
| 207 | fd := mi.PBType.Descriptor().Fields().Get(i) |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 208 | fs := si.fieldsByNumber[fd.Number()] |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 209 | var fi fieldInfo |
| 210 | switch { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 211 | case fd.ContainingOneof() != nil: |
| 212 | fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], si.oneofWrappersByNumber[fd.Number()]) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 213 | case fd.IsMap(): |
| 214 | fi = fieldInfoForMap(fd, fs) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 215 | case fd.IsList(): |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 216 | fi = fieldInfoForList(fd, fs) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 217 | case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind: |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 218 | fi = fieldInfoForMessage(fd, fs) |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 219 | default: |
| 220 | fi = fieldInfoForScalar(fd, fs) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 221 | } |
| 222 | mi.fields[fd.Number()] = &fi |
| 223 | } |
Joe Tsai | 4ec39c7 | 2019-04-03 13:40:53 -0700 | [diff] [blame] | 224 | |
| 225 | mi.oneofs = map[pref.Name]*oneofInfo{} |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 226 | for i := 0; i < mi.PBType.Descriptor().Oneofs().Len(); i++ { |
| 227 | od := mi.PBType.Descriptor().Oneofs().Get(i) |
Damien Neil | 3eaddf0 | 2019-05-09 11:33:55 -0700 | [diff] [blame] | 228 | mi.oneofs[od.Name()] = makeOneofInfo(od, si.oneofsByName[od.Name()], si.oneofWrappersByType) |
Joe Tsai | 4ec39c7 | 2019-04-03 13:40:53 -0700 | [diff] [blame] | 229 | } |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 230 | } |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 231 | |
Joe Tsai | 93fd968 | 2019-07-06 13:02:14 -0700 | [diff] [blame] | 232 | func (mi *MessageInfo) makeUnknownFieldsFunc(t reflect.Type, si structInfo) { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 233 | mi.getUnknown = func(pointer) pref.RawFields { return nil } |
| 234 | mi.setUnknown = func(pointer, pref.RawFields) { return } |
Joe Tsai | 93fd968 | 2019-07-06 13:02:14 -0700 | [diff] [blame] | 235 | if si.unknownOffset.IsValid() { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 236 | mi.getUnknown = func(p pointer) pref.RawFields { |
| 237 | if p.IsNil() { |
| 238 | return nil |
| 239 | } |
Joe Tsai | 93fd968 | 2019-07-06 13:02:14 -0700 | [diff] [blame] | 240 | rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType) |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 241 | return pref.RawFields(*rv.Interface().(*[]byte)) |
| 242 | } |
| 243 | mi.setUnknown = func(p pointer, b pref.RawFields) { |
| 244 | if p.IsNil() { |
| 245 | panic("invalid SetUnknown on nil Message") |
| 246 | } |
Joe Tsai | 93fd968 | 2019-07-06 13:02:14 -0700 | [diff] [blame] | 247 | rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType) |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 248 | *rv.Interface().(*[]byte) = []byte(b) |
| 249 | } |
| 250 | } else { |
| 251 | mi.getUnknown = func(pointer) pref.RawFields { |
| 252 | return nil |
| 253 | } |
| 254 | mi.setUnknown = func(p pointer, _ pref.RawFields) { |
| 255 | if p.IsNil() { |
| 256 | panic("invalid SetUnknown on nil Message") |
| 257 | } |
| 258 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Joe Tsai | 93fd968 | 2019-07-06 13:02:14 -0700 | [diff] [blame] | 262 | func (mi *MessageInfo) makeExtensionFieldsFunc(t reflect.Type, si structInfo) { |
| 263 | if si.extensionOffset.IsValid() { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 264 | mi.extensionMap = func(p pointer) *extensionMap { |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 265 | if p.IsNil() { |
| 266 | return (*extensionMap)(nil) |
| 267 | } |
Joe Tsai | 93fd968 | 2019-07-06 13:02:14 -0700 | [diff] [blame] | 268 | v := p.Apply(si.extensionOffset).AsValueOf(extensionFieldsType) |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 269 | return (*extensionMap)(v.Interface().(*map[int32]ExtensionField)) |
| 270 | } |
| 271 | } else { |
| 272 | mi.extensionMap = func(pointer) *extensionMap { |
| 273 | return (*extensionMap)(nil) |
| 274 | } |
Joe Tsai | be5348c | 2018-10-23 18:31:18 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 278 | func (mi *MessageInfo) MessageOf(p interface{}) pref.Message { |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 279 | return (*messageReflectWrapper)(mi.dataTypeOf(p)) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 282 | func (mi *MessageInfo) Methods() *piface.Methods { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 283 | mi.init() |
| 284 | return &mi.methods |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 287 | func (mi *MessageInfo) dataTypeOf(p interface{}) *messageDataType { |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 288 | // TODO: Remove this check? This API is primarily used by generated code, |
| 289 | // and should not violate this assumption. Leave this check in for now to |
| 290 | // provide some sanity checks during development. This can be removed if |
| 291 | // it proves to be detrimental to performance. |
| 292 | if reflect.TypeOf(p) != mi.GoType { |
| 293 | panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.GoType)) |
| 294 | } |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 295 | return &messageDataType{pointerOfIface(p), mi} |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // messageDataType is a tuple of a pointer to the message data and |
| 299 | // a pointer to the message type. |
| 300 | // |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 301 | // TODO: Unfortunately, we need to close over a pointer and MessageInfo, |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 302 | // which incurs an an allocation. This pair is similar to a Go interface, |
| 303 | // which is essentially a tuple of the same thing. We can make this efficient |
| 304 | // with reflect.NamedOf (see https://golang.org/issues/16522). |
| 305 | // |
| 306 | // With that hypothetical API, we could dynamically create a new named type |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 307 | // that has the same underlying type as MessageInfo.GoType, and |
| 308 | // dynamically create methods that close over MessageInfo. |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 309 | // Since the new type would have the same underlying type, we could directly |
| 310 | // convert between pointers of those types, giving us an efficient way to swap |
| 311 | // out the method set. |
| 312 | // |
| 313 | // Barring the ability to dynamically create named types, the workaround is |
| 314 | // 1. either to accept the cost of an allocation for this wrapper struct or |
| 315 | // 2. generate more types and methods, at the expense of binary size increase. |
| 316 | type messageDataType struct { |
| 317 | p pointer |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 318 | mi *MessageInfo |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 321 | type messageReflectWrapper messageDataType |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 322 | |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 323 | func (m *messageReflectWrapper) Descriptor() pref.MessageDescriptor { |
| 324 | return m.mi.PBType.Descriptor() |
| 325 | } |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 326 | func (m *messageReflectWrapper) New() pref.Message { |
| 327 | return m.mi.PBType.New() |
| 328 | } |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 329 | func (m *messageReflectWrapper) Interface() pref.ProtoMessage { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 330 | if m, ok := m.ProtoUnwrap().(pref.ProtoMessage); ok { |
| 331 | return m |
| 332 | } |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 333 | return (*messageIfaceWrapper)(m) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 334 | } |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 335 | func (m *messageReflectWrapper) ProtoUnwrap() interface{} { |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 336 | return m.p.AsIfaceOf(m.mi.GoType.Elem()) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 337 | } |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 338 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 339 | func (m *messageReflectWrapper) Range(f func(pref.FieldDescriptor, pref.Value) bool) { |
| 340 | m.mi.init() |
| 341 | for _, fi := range m.mi.fields { |
| 342 | if fi.has(m.p) { |
| 343 | if !f(fi.fieldDesc, fi.get(m.p)) { |
| 344 | return |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | m.mi.extensionMap(m.p).Range(f) |
| 349 | } |
| 350 | func (m *messageReflectWrapper) Has(fd pref.FieldDescriptor) bool { |
| 351 | if fi, xt := m.checkField(fd); fi != nil { |
| 352 | return fi.has(m.p) |
| 353 | } else { |
| 354 | return m.mi.extensionMap(m.p).Has(xt) |
| 355 | } |
| 356 | } |
| 357 | func (m *messageReflectWrapper) Clear(fd pref.FieldDescriptor) { |
| 358 | if fi, xt := m.checkField(fd); fi != nil { |
| 359 | fi.clear(m.p) |
| 360 | } else { |
| 361 | m.mi.extensionMap(m.p).Clear(xt) |
| 362 | } |
| 363 | } |
| 364 | func (m *messageReflectWrapper) Get(fd pref.FieldDescriptor) pref.Value { |
| 365 | if fi, xt := m.checkField(fd); fi != nil { |
| 366 | return fi.get(m.p) |
| 367 | } else { |
| 368 | return m.mi.extensionMap(m.p).Get(xt) |
| 369 | } |
| 370 | } |
| 371 | func (m *messageReflectWrapper) Set(fd pref.FieldDescriptor, v pref.Value) { |
| 372 | if fi, xt := m.checkField(fd); fi != nil { |
| 373 | fi.set(m.p, v) |
| 374 | } else { |
| 375 | m.mi.extensionMap(m.p).Set(xt, v) |
| 376 | } |
| 377 | } |
| 378 | func (m *messageReflectWrapper) Mutable(fd pref.FieldDescriptor) pref.Value { |
| 379 | if fi, xt := m.checkField(fd); fi != nil { |
| 380 | return fi.mutable(m.p) |
| 381 | } else { |
| 382 | return m.mi.extensionMap(m.p).Mutable(xt) |
| 383 | } |
| 384 | } |
| 385 | func (m *messageReflectWrapper) NewMessage(fd pref.FieldDescriptor) pref.Message { |
| 386 | if fi, xt := m.checkField(fd); fi != nil { |
| 387 | return fi.newMessage() |
| 388 | } else { |
| 389 | return xt.New().Message() |
| 390 | } |
| 391 | } |
| 392 | func (m *messageReflectWrapper) WhichOneof(od pref.OneofDescriptor) pref.FieldDescriptor { |
| 393 | m.mi.init() |
| 394 | if oi := m.mi.oneofs[od.Name()]; oi != nil && oi.oneofDesc == od { |
| 395 | return od.Fields().ByNumber(oi.which(m.p)) |
| 396 | } |
| 397 | panic("invalid oneof descriptor") |
| 398 | } |
| 399 | func (m *messageReflectWrapper) GetUnknown() pref.RawFields { |
| 400 | m.mi.init() |
| 401 | return m.mi.getUnknown(m.p) |
| 402 | } |
| 403 | func (m *messageReflectWrapper) SetUnknown(b pref.RawFields) { |
| 404 | m.mi.init() |
| 405 | m.mi.setUnknown(m.p, b) |
| 406 | } |
| 407 | |
| 408 | // checkField verifies that the provided field descriptor is valid. |
| 409 | // Exactly one of the returned values is populated. |
| 410 | func (m *messageReflectWrapper) checkField(fd pref.FieldDescriptor) (*fieldInfo, pref.ExtensionType) { |
| 411 | m.mi.init() |
| 412 | if fi := m.mi.fields[fd.Number()]; fi != nil { |
| 413 | if fi.fieldDesc != fd { |
| 414 | panic("mismatching field descriptor") |
| 415 | } |
| 416 | return fi, nil |
| 417 | } |
| 418 | if fd.IsExtension() { |
| 419 | if fd.ContainingMessage().FullName() != m.mi.PBType.FullName() { |
| 420 | // TODO: Should this be exact containing message descriptor match? |
| 421 | panic("mismatching containing message") |
| 422 | } |
| 423 | if !m.mi.PBType.ExtensionRanges().Has(fd.Number()) { |
| 424 | panic("invalid extension field") |
| 425 | } |
| 426 | return nil, fd.(pref.ExtensionType) |
| 427 | } |
| 428 | panic("invalid field descriptor") |
| 429 | } |
| 430 | |
| 431 | type extensionMap map[int32]ExtensionField |
| 432 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 433 | func (m *extensionMap) Range(f func(pref.FieldDescriptor, pref.Value) bool) { |
| 434 | if m != nil { |
| 435 | for _, x := range *m { |
| 436 | xt := x.GetType() |
| 437 | if !f(xt, xt.ValueOf(x.GetValue())) { |
| 438 | return |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | func (m *extensionMap) Has(xt pref.ExtensionType) (ok bool) { |
| 444 | if m != nil { |
| 445 | _, ok = (*m)[int32(xt.Number())] |
| 446 | } |
| 447 | return ok |
| 448 | } |
| 449 | func (m *extensionMap) Clear(xt pref.ExtensionType) { |
| 450 | delete(*m, int32(xt.Number())) |
| 451 | } |
| 452 | func (m *extensionMap) Get(xt pref.ExtensionType) pref.Value { |
| 453 | if m != nil { |
| 454 | if x, ok := (*m)[int32(xt.Number())]; ok { |
| 455 | return xt.ValueOf(x.GetValue()) |
| 456 | } |
| 457 | } |
| 458 | if !isComposite(xt) { |
| 459 | return defaultValueOf(xt) |
| 460 | } |
| 461 | return frozenValueOf(xt.New()) |
| 462 | } |
| 463 | func (m *extensionMap) Set(xt pref.ExtensionType, v pref.Value) { |
| 464 | if *m == nil { |
| 465 | *m = make(map[int32]ExtensionField) |
| 466 | } |
| 467 | var x ExtensionField |
| 468 | x.SetType(xt) |
| 469 | x.SetEagerValue(xt.InterfaceOf(v)) |
| 470 | (*m)[int32(xt.Number())] = x |
| 471 | } |
| 472 | func (m *extensionMap) Mutable(xt pref.ExtensionType) pref.Value { |
| 473 | if !isComposite(xt) { |
| 474 | panic("invalid Mutable on field with non-composite type") |
| 475 | } |
| 476 | if x, ok := (*m)[int32(xt.Number())]; ok { |
| 477 | return xt.ValueOf(x.GetValue()) |
| 478 | } |
| 479 | v := xt.New() |
| 480 | m.Set(xt, v) |
| 481 | return v |
| 482 | } |
| 483 | |
| 484 | func isComposite(fd pref.FieldDescriptor) bool { |
| 485 | return fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind || fd.IsList() || fd.IsMap() |
| 486 | } |
| 487 | |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 488 | var _ pvalue.Unwrapper = (*messageReflectWrapper)(nil) |
| 489 | |
| 490 | type messageIfaceWrapper messageDataType |
| 491 | |
| 492 | func (m *messageIfaceWrapper) ProtoReflect() pref.Message { |
| 493 | return (*messageReflectWrapper)(m) |
| 494 | } |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 495 | func (m *messageIfaceWrapper) XXX_Methods() *piface.Methods { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 496 | // TODO: Consider not recreating this on every call. |
| 497 | m.mi.init() |
| 498 | return &piface.Methods{ |
| 499 | Flags: piface.MethodFlagDeterministicMarshal, |
| 500 | MarshalAppend: m.marshalAppend, |
| 501 | Size: m.size, |
| 502 | } |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 503 | } |
Joe Tsai | 22b1ebd | 2019-03-11 13:45:14 -0700 | [diff] [blame] | 504 | func (m *messageIfaceWrapper) ProtoUnwrap() interface{} { |
| 505 | return m.p.AsIfaceOf(m.mi.GoType.Elem()) |
| 506 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 507 | func (m *messageIfaceWrapper) marshalAppend(b []byte, _ pref.ProtoMessage, opts piface.MarshalOptions) ([]byte, error) { |
| 508 | return m.mi.marshalAppendPointer(b, m.p, newMarshalOptions(opts)) |
| 509 | } |
| 510 | func (m *messageIfaceWrapper) size(msg pref.ProtoMessage) (size int) { |
| 511 | return m.mi.sizePointer(m.p, 0) |
| 512 | } |