Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 1 | // Copyright 2019 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 | |
Joe Tsai | 1726b83 | 2020-06-25 10:40:16 -0700 | [diff] [blame] | 11 | "google.golang.org/protobuf/internal/detrand" |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 12 | "google.golang.org/protobuf/internal/pragma" |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 13 | pref "google.golang.org/protobuf/reflect/protoreflect" |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 14 | ) |
| 15 | |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 16 | type reflectMessageInfo struct { |
| 17 | fields map[pref.FieldNumber]*fieldInfo |
| 18 | oneofs map[pref.Name]*oneofInfo |
| 19 | |
Joe Tsai | 9d637ca | 2019-09-18 18:08:52 -0700 | [diff] [blame] | 20 | // denseFields is a subset of fields where: |
| 21 | // 0 < fieldDesc.Number() < len(denseFields) |
| 22 | // It provides faster access to the fieldInfo, but may be incomplete. |
| 23 | denseFields []*fieldInfo |
| 24 | |
| 25 | // rangeInfos is a list of all fields (not belonging to a oneof) and oneofs. |
| 26 | rangeInfos []interface{} // either *fieldInfo or *oneofInfo |
| 27 | |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 28 | getUnknown func(pointer) pref.RawFields |
| 29 | setUnknown func(pointer, pref.RawFields) |
| 30 | extensionMap func(pointer) *extensionMap |
| 31 | |
| 32 | nilMessage atomicNilMessage |
| 33 | } |
| 34 | |
| 35 | // makeReflectFuncs generates the set of functions to support reflection. |
| 36 | func (mi *MessageInfo) makeReflectFuncs(t reflect.Type, si structInfo) { |
| 37 | mi.makeKnownFieldsFunc(si) |
| 38 | mi.makeUnknownFieldsFunc(t, si) |
| 39 | mi.makeExtensionFieldsFunc(t, si) |
| 40 | } |
| 41 | |
| 42 | // makeKnownFieldsFunc generates functions for operations that can be performed |
| 43 | // on each protobuf message field. It takes in a reflect.Type representing the |
| 44 | // Go struct and matches message fields with struct fields. |
| 45 | // |
| 46 | // This code assumes that the struct is well-formed and panics if there are |
| 47 | // any discrepancies. |
| 48 | func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) { |
| 49 | mi.fields = map[pref.FieldNumber]*fieldInfo{} |
| 50 | md := mi.Desc |
Joe Tsai | 9d637ca | 2019-09-18 18:08:52 -0700 | [diff] [blame] | 51 | fds := md.Fields() |
| 52 | for i := 0; i < fds.Len(); i++ { |
| 53 | fd := fds.Get(i) |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 54 | fs := si.fieldsByNumber[fd.Number()] |
| 55 | var fi fieldInfo |
| 56 | switch { |
Joe Tsai | 387873d | 2020-04-28 14:44:38 -0700 | [diff] [blame] | 57 | case fd.ContainingOneof() != nil && !fd.ContainingOneof().IsSynthetic(): |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 58 | fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], mi.Exporter, si.oneofWrappersByNumber[fd.Number()]) |
| 59 | case fd.IsMap(): |
| 60 | fi = fieldInfoForMap(fd, fs, mi.Exporter) |
| 61 | case fd.IsList(): |
| 62 | fi = fieldInfoForList(fd, fs, mi.Exporter) |
| 63 | case fd.IsWeak(): |
| 64 | fi = fieldInfoForWeakMessage(fd, si.weakOffset) |
| 65 | case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind: |
| 66 | fi = fieldInfoForMessage(fd, fs, mi.Exporter) |
| 67 | default: |
| 68 | fi = fieldInfoForScalar(fd, fs, mi.Exporter) |
| 69 | } |
| 70 | mi.fields[fd.Number()] = &fi |
| 71 | } |
| 72 | |
| 73 | mi.oneofs = map[pref.Name]*oneofInfo{} |
| 74 | for i := 0; i < md.Oneofs().Len(); i++ { |
| 75 | od := md.Oneofs().Get(i) |
Joe Tsai | 387873d | 2020-04-28 14:44:38 -0700 | [diff] [blame] | 76 | mi.oneofs[od.Name()] = makeOneofInfo(od, si, mi.Exporter) |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 77 | } |
Joe Tsai | 9d637ca | 2019-09-18 18:08:52 -0700 | [diff] [blame] | 78 | |
| 79 | mi.denseFields = make([]*fieldInfo, fds.Len()*2) |
| 80 | for i := 0; i < fds.Len(); i++ { |
| 81 | if fd := fds.Get(i); int(fd.Number()) < len(mi.denseFields) { |
| 82 | mi.denseFields[fd.Number()] = mi.fields[fd.Number()] |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | for i := 0; i < fds.Len(); { |
| 87 | fd := fds.Get(i) |
Joe Tsai | 387873d | 2020-04-28 14:44:38 -0700 | [diff] [blame] | 88 | if od := fd.ContainingOneof(); od != nil && !od.IsSynthetic() { |
Joe Tsai | 9d637ca | 2019-09-18 18:08:52 -0700 | [diff] [blame] | 89 | mi.rangeInfos = append(mi.rangeInfos, mi.oneofs[od.Name()]) |
| 90 | i += od.Fields().Len() |
| 91 | } else { |
| 92 | mi.rangeInfos = append(mi.rangeInfos, mi.fields[fd.Number()]) |
| 93 | i++ |
| 94 | } |
| 95 | } |
Joe Tsai | 1726b83 | 2020-06-25 10:40:16 -0700 | [diff] [blame] | 96 | |
| 97 | // Introduce instability to iteration order, but keep it deterministic. |
| 98 | if len(mi.rangeInfos) > 1 && detrand.Bool() { |
| 99 | i := detrand.Intn(len(mi.rangeInfos) - 1) |
| 100 | mi.rangeInfos[i], mi.rangeInfos[i+1] = mi.rangeInfos[i+1], mi.rangeInfos[i] |
| 101 | } |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | func (mi *MessageInfo) makeUnknownFieldsFunc(t reflect.Type, si structInfo) { |
| 105 | mi.getUnknown = func(pointer) pref.RawFields { return nil } |
| 106 | mi.setUnknown = func(pointer, pref.RawFields) { return } |
| 107 | if si.unknownOffset.IsValid() { |
| 108 | mi.getUnknown = func(p pointer) pref.RawFields { |
| 109 | if p.IsNil() { |
| 110 | return nil |
| 111 | } |
| 112 | rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType) |
| 113 | return pref.RawFields(*rv.Interface().(*[]byte)) |
| 114 | } |
| 115 | mi.setUnknown = func(p pointer, b pref.RawFields) { |
| 116 | if p.IsNil() { |
| 117 | panic("invalid SetUnknown on nil Message") |
| 118 | } |
| 119 | rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType) |
| 120 | *rv.Interface().(*[]byte) = []byte(b) |
| 121 | } |
| 122 | } else { |
| 123 | mi.getUnknown = func(pointer) pref.RawFields { |
| 124 | return nil |
| 125 | } |
| 126 | mi.setUnknown = func(p pointer, _ pref.RawFields) { |
| 127 | if p.IsNil() { |
| 128 | panic("invalid SetUnknown on nil Message") |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func (mi *MessageInfo) makeExtensionFieldsFunc(t reflect.Type, si structInfo) { |
| 135 | if si.extensionOffset.IsValid() { |
| 136 | mi.extensionMap = func(p pointer) *extensionMap { |
| 137 | if p.IsNil() { |
| 138 | return (*extensionMap)(nil) |
| 139 | } |
| 140 | v := p.Apply(si.extensionOffset).AsValueOf(extensionFieldsType) |
| 141 | return (*extensionMap)(v.Interface().(*map[int32]ExtensionField)) |
| 142 | } |
| 143 | } else { |
| 144 | mi.extensionMap = func(pointer) *extensionMap { |
| 145 | return (*extensionMap)(nil) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | type extensionMap map[int32]ExtensionField |
| 151 | |
| 152 | func (m *extensionMap) Range(f func(pref.FieldDescriptor, pref.Value) bool) { |
| 153 | if m != nil { |
| 154 | for _, x := range *m { |
Damien Neil | a0a54b8 | 2019-11-01 15:18:36 -0700 | [diff] [blame] | 155 | xd := x.Type().TypeDescriptor() |
| 156 | v := x.Value() |
| 157 | if xd.IsList() && v.List().Len() == 0 { |
| 158 | continue |
| 159 | } |
| 160 | if !f(xd, v) { |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 161 | return |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | func (m *extensionMap) Has(xt pref.ExtensionType) (ok bool) { |
Damien Neil | a0a54b8 | 2019-11-01 15:18:36 -0700 | [diff] [blame] | 167 | if m == nil { |
| 168 | return false |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 169 | } |
Damien Neil | a0a54b8 | 2019-11-01 15:18:36 -0700 | [diff] [blame] | 170 | xd := xt.TypeDescriptor() |
| 171 | x, ok := (*m)[int32(xd.Number())] |
| 172 | if !ok { |
| 173 | return false |
| 174 | } |
| 175 | switch { |
| 176 | case xd.IsList(): |
| 177 | return x.Value().List().Len() > 0 |
| 178 | case xd.IsMap(): |
| 179 | return x.Value().Map().Len() > 0 |
Joe Tsai | b57aae9 | 2020-04-22 13:49:10 -0700 | [diff] [blame] | 180 | case xd.Message() != nil: |
| 181 | return x.Value().Message().IsValid() |
Damien Neil | a0a54b8 | 2019-11-01 15:18:36 -0700 | [diff] [blame] | 182 | } |
| 183 | return true |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 184 | } |
| 185 | func (m *extensionMap) Clear(xt pref.ExtensionType) { |
Damien Neil | 79bfdbe | 2019-08-28 11:08:22 -0700 | [diff] [blame] | 186 | delete(*m, int32(xt.TypeDescriptor().Number())) |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 187 | } |
| 188 | func (m *extensionMap) Get(xt pref.ExtensionType) pref.Value { |
Damien Neil | 79bfdbe | 2019-08-28 11:08:22 -0700 | [diff] [blame] | 189 | xd := xt.TypeDescriptor() |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 190 | if m != nil { |
| 191 | if x, ok := (*m)[int32(xd.Number())]; ok { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 192 | return x.Value() |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | return xt.Zero() |
| 196 | } |
| 197 | func (m *extensionMap) Set(xt pref.ExtensionType, v pref.Value) { |
Joe Tsai | b57aae9 | 2020-04-22 13:49:10 -0700 | [diff] [blame] | 198 | xd := xt.TypeDescriptor() |
| 199 | isValid := true |
| 200 | switch { |
| 201 | case !xt.IsValidValue(v): |
| 202 | isValid = false |
| 203 | case xd.IsList(): |
| 204 | isValid = v.List().IsValid() |
| 205 | case xd.IsMap(): |
| 206 | isValid = v.Map().IsValid() |
| 207 | case xd.Message() != nil: |
| 208 | isValid = v.Message().IsValid() |
| 209 | } |
| 210 | if !isValid { |
Joe Tsai | 09217f0 | 2019-09-06 16:57:46 -0700 | [diff] [blame] | 211 | panic(fmt.Sprintf("%v: assigning invalid value", xt.TypeDescriptor().FullName())) |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 212 | } |
Joe Tsai | b57aae9 | 2020-04-22 13:49:10 -0700 | [diff] [blame] | 213 | |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 214 | if *m == nil { |
| 215 | *m = make(map[int32]ExtensionField) |
| 216 | } |
| 217 | var x ExtensionField |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 218 | x.Set(xt, v) |
Joe Tsai | b57aae9 | 2020-04-22 13:49:10 -0700 | [diff] [blame] | 219 | (*m)[int32(xd.Number())] = x |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 220 | } |
| 221 | func (m *extensionMap) Mutable(xt pref.ExtensionType) pref.Value { |
Damien Neil | 79bfdbe | 2019-08-28 11:08:22 -0700 | [diff] [blame] | 222 | xd := xt.TypeDescriptor() |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 223 | if xd.Kind() != pref.MessageKind && xd.Kind() != pref.GroupKind && !xd.IsList() && !xd.IsMap() { |
| 224 | panic("invalid Mutable on field with non-composite type") |
| 225 | } |
| 226 | if x, ok := (*m)[int32(xd.Number())]; ok { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 227 | return x.Value() |
Joe Tsai | 0484b1a | 2019-08-13 15:36:08 -0700 | [diff] [blame] | 228 | } |
| 229 | v := xt.New() |
| 230 | m.Set(xt, v) |
| 231 | return v |
| 232 | } |
| 233 | |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 234 | // MessageState is a data structure that is nested as the first field in a |
| 235 | // concrete message. It provides a way to implement the ProtoReflect method |
| 236 | // in an allocation-free way without needing to have a shadow Go type generated |
| 237 | // for every message type. This technique only works using unsafe. |
| 238 | // |
| 239 | // |
| 240 | // Example generated code: |
| 241 | // |
| 242 | // type M struct { |
| 243 | // state protoimpl.MessageState |
| 244 | // |
| 245 | // Field1 int32 |
| 246 | // Field2 string |
| 247 | // Field3 *BarMessage |
| 248 | // ... |
| 249 | // } |
| 250 | // |
| 251 | // func (m *M) ProtoReflect() protoreflect.Message { |
| 252 | // mi := &file_fizz_buzz_proto_msgInfos[5] |
| 253 | // if protoimpl.UnsafeEnabled && m != nil { |
| 254 | // ms := protoimpl.X.MessageStateOf(Pointer(m)) |
| 255 | // if ms.LoadMessageInfo() == nil { |
| 256 | // ms.StoreMessageInfo(mi) |
| 257 | // } |
| 258 | // return ms |
| 259 | // } |
| 260 | // return mi.MessageOf(m) |
| 261 | // } |
| 262 | // |
| 263 | // The MessageState type holds a *MessageInfo, which must be atomically set to |
| 264 | // the message info associated with a given message instance. |
| 265 | // By unsafely converting a *M into a *MessageState, the MessageState object |
| 266 | // has access to all the information needed to implement protobuf reflection. |
| 267 | // It has access to the message info as its first field, and a pointer to the |
| 268 | // MessageState is identical to a pointer to the concrete message value. |
| 269 | // |
| 270 | // |
| 271 | // Requirements: |
| 272 | // • The type M must implement protoreflect.ProtoMessage. |
| 273 | // • The address of m must not be nil. |
| 274 | // • The address of m and the address of m.state must be equal, |
| 275 | // even though they are different Go types. |
| 276 | type MessageState struct { |
| 277 | pragma.NoUnkeyedLiterals |
| 278 | pragma.DoNotCompare |
| 279 | pragma.DoNotCopy |
| 280 | |
Damien Neil | 075e074 | 2020-02-27 12:33:08 -0800 | [diff] [blame] | 281 | atomicMessageInfo *MessageInfo |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | type messageState MessageState |
| 285 | |
| 286 | var ( |
Damien Neil | 954bd92 | 2019-07-17 16:52:10 -0700 | [diff] [blame] | 287 | _ pref.Message = (*messageState)(nil) |
Joe Tsai | fd528ff | 2019-09-03 16:30:39 -0700 | [diff] [blame] | 288 | _ unwrapper = (*messageState)(nil) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 289 | ) |
| 290 | |
| 291 | // messageDataType is a tuple of a pointer to the message data and |
| 292 | // a pointer to the message type. It is a generalized way of providing a |
| 293 | // reflective view over a message instance. The disadvantage of this approach |
| 294 | // is the need to allocate this tuple of 16B. |
| 295 | type messageDataType struct { |
| 296 | p pointer |
| 297 | mi *MessageInfo |
| 298 | } |
| 299 | |
| 300 | type ( |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 301 | messageReflectWrapper messageDataType |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 302 | messageIfaceWrapper messageDataType |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 303 | ) |
| 304 | |
| 305 | var ( |
| 306 | _ pref.Message = (*messageReflectWrapper)(nil) |
Joe Tsai | fd528ff | 2019-09-03 16:30:39 -0700 | [diff] [blame] | 307 | _ unwrapper = (*messageReflectWrapper)(nil) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 308 | _ pref.ProtoMessage = (*messageIfaceWrapper)(nil) |
Joe Tsai | fd528ff | 2019-09-03 16:30:39 -0700 | [diff] [blame] | 309 | _ unwrapper = (*messageIfaceWrapper)(nil) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 310 | ) |
| 311 | |
| 312 | // MessageOf returns a reflective view over a message. The input must be a |
| 313 | // pointer to a named Go struct. If the provided type has a ProtoReflect method, |
| 314 | // it must be implemented by calling this method. |
| 315 | func (mi *MessageInfo) MessageOf(m interface{}) pref.Message { |
| 316 | // TODO: Switch the input to be an opaque Pointer. |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame] | 317 | if reflect.TypeOf(m) != mi.GoReflectType { |
| 318 | panic(fmt.Sprintf("type mismatch: got %T, want %v", m, mi.GoReflectType)) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 319 | } |
| 320 | p := pointerOfIface(m) |
| 321 | if p.IsNil() { |
| 322 | return mi.nilMessage.Init(mi) |
| 323 | } |
| 324 | return &messageReflectWrapper{p, mi} |
| 325 | } |
| 326 | |
Joe Tsai | 2aea614 | 2019-07-31 12:27:30 -0700 | [diff] [blame] | 327 | func (m *messageReflectWrapper) pointer() pointer { return m.p } |
| 328 | func (m *messageReflectWrapper) messageInfo() *MessageInfo { return m.mi } |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 329 | |
| 330 | func (m *messageIfaceWrapper) ProtoReflect() pref.Message { |
| 331 | return (*messageReflectWrapper)(m) |
| 332 | } |
Joe Tsai | fd528ff | 2019-09-03 16:30:39 -0700 | [diff] [blame] | 333 | func (m *messageIfaceWrapper) protoUnwrap() interface{} { |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame] | 334 | return m.p.AsIfaceOf(m.mi.GoReflectType.Elem()) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 335 | } |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 336 | |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 337 | // checkField verifies that the provided field descriptor is valid. |
| 338 | // Exactly one of the returned values is populated. |
| 339 | func (mi *MessageInfo) checkField(fd pref.FieldDescriptor) (*fieldInfo, pref.ExtensionType) { |
Joe Tsai | 9d637ca | 2019-09-18 18:08:52 -0700 | [diff] [blame] | 340 | var fi *fieldInfo |
| 341 | if n := fd.Number(); 0 < n && int(n) < len(mi.denseFields) { |
| 342 | fi = mi.denseFields[n] |
| 343 | } else { |
| 344 | fi = mi.fields[n] |
| 345 | } |
| 346 | if fi != nil { |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 347 | if fi.fieldDesc != fd { |
Joe Tsai | 1f5b6fe | 2020-04-22 21:58:56 -0700 | [diff] [blame] | 348 | if got, want := fd.FullName(), fi.fieldDesc.FullName(); got != want { |
| 349 | panic(fmt.Sprintf("mismatching field: got %v, want %v", got, want)) |
| 350 | } |
| 351 | panic(fmt.Sprintf("mismatching field: %v", fd.FullName())) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 352 | } |
| 353 | return fi, nil |
| 354 | } |
Joe Tsai | 9d637ca | 2019-09-18 18:08:52 -0700 | [diff] [blame] | 355 | |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 356 | if fd.IsExtension() { |
Joe Tsai | 1f5b6fe | 2020-04-22 21:58:56 -0700 | [diff] [blame] | 357 | if got, want := fd.ContainingMessage().FullName(), mi.Desc.FullName(); got != want { |
Joe Tsai | fb5fde4 | 2020-02-20 11:00:10 -0800 | [diff] [blame] | 358 | // TODO: Should this be exact containing message descriptor match? |
Joe Tsai | 1f5b6fe | 2020-04-22 21:58:56 -0700 | [diff] [blame] | 359 | panic(fmt.Sprintf("extension %v has mismatching containing message: got %v, want %v", fd.FullName(), got, want)) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 360 | } |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame] | 361 | if !mi.Desc.ExtensionRanges().Has(fd.Number()) { |
Joe Tsai | 1f5b6fe | 2020-04-22 21:58:56 -0700 | [diff] [blame] | 362 | panic(fmt.Sprintf("extension %v extends %v outside the extension range", fd.FullName(), mi.Desc.FullName())) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 363 | } |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 364 | xtd, ok := fd.(pref.ExtensionTypeDescriptor) |
| 365 | if !ok { |
Joe Tsai | 1f5b6fe | 2020-04-22 21:58:56 -0700 | [diff] [blame] | 366 | panic(fmt.Sprintf("extension %v does not implement protoreflect.ExtensionTypeDescriptor", fd.FullName())) |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 367 | } |
| 368 | return nil, xtd.Type() |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 369 | } |
Joe Tsai | 1f5b6fe | 2020-04-22 21:58:56 -0700 | [diff] [blame] | 370 | panic(fmt.Sprintf("field %v is invalid", fd.FullName())) |
Joe Tsai | 82760ce | 2019-06-20 03:09:57 -0700 | [diff] [blame] | 371 | } |