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