blob: cdf3164686367da2e0a6bb48319cb553a23e94f4 [file] [log] [blame]
Scott Baker105df152020-04-13 15:55:14 -07001// 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
5package filedesc
6
7import (
8 "reflect"
9 "sync"
10
11 "google.golang.org/protobuf/encoding/protowire"
12 "google.golang.org/protobuf/internal/descopts"
13 "google.golang.org/protobuf/internal/fieldnum"
14 "google.golang.org/protobuf/internal/strs"
15 "google.golang.org/protobuf/proto"
16 pref "google.golang.org/protobuf/reflect/protoreflect"
17)
18
19func (fd *File) lazyRawInit() {
20 fd.unmarshalFull(fd.builder.RawDescriptor)
21 fd.resolveMessages()
22 fd.resolveExtensions()
23 fd.resolveServices()
24}
25
26func (file *File) resolveMessages() {
27 var depIdx int32
28 for i := range file.allMessages {
29 md := &file.allMessages[i]
30
31 // Resolve message field dependencies.
32 for j := range md.L2.Fields.List {
33 fd := &md.L2.Fields.List[j]
34
35 // Weak fields are resolved upon actual use.
36 if fd.L1.IsWeak {
37 continue
38 }
39
40 // Resolve message field dependency.
41 switch fd.L1.Kind {
42 case pref.EnumKind:
43 fd.L1.Enum = file.resolveEnumDependency(fd.L1.Enum, listFieldDeps, depIdx)
44 depIdx++
45 case pref.MessageKind, pref.GroupKind:
46 fd.L1.Message = file.resolveMessageDependency(fd.L1.Message, listFieldDeps, depIdx)
47 depIdx++
48 }
49
50 // Default is resolved here since it depends on Enum being resolved.
51 if v := fd.L1.Default.val; v.IsValid() {
52 fd.L1.Default = unmarshalDefault(v.Bytes(), fd.L1.Kind, file, fd.L1.Enum)
53 }
54 }
55 }
56}
57
58func (file *File) resolveExtensions() {
59 var depIdx int32
60 for i := range file.allExtensions {
61 xd := &file.allExtensions[i]
62
63 // Resolve extension field dependency.
64 switch xd.L1.Kind {
65 case pref.EnumKind:
66 xd.L2.Enum = file.resolveEnumDependency(xd.L2.Enum, listExtDeps, depIdx)
67 depIdx++
68 case pref.MessageKind, pref.GroupKind:
69 xd.L2.Message = file.resolveMessageDependency(xd.L2.Message, listExtDeps, depIdx)
70 depIdx++
71 }
72
73 // Default is resolved here since it depends on Enum being resolved.
74 if v := xd.L2.Default.val; v.IsValid() {
75 xd.L2.Default = unmarshalDefault(v.Bytes(), xd.L1.Kind, file, xd.L2.Enum)
76 }
77 }
78}
79
80func (file *File) resolveServices() {
81 var depIdx int32
82 for i := range file.allServices {
83 sd := &file.allServices[i]
84
85 // Resolve method dependencies.
86 for j := range sd.L2.Methods.List {
87 md := &sd.L2.Methods.List[j]
88 md.L1.Input = file.resolveMessageDependency(md.L1.Input, listMethInDeps, depIdx)
89 md.L1.Output = file.resolveMessageDependency(md.L1.Output, listMethOutDeps, depIdx)
90 depIdx++
91 }
92 }
93}
94
95func (file *File) resolveEnumDependency(ed pref.EnumDescriptor, i, j int32) pref.EnumDescriptor {
96 r := file.builder.FileRegistry
97 if r, ok := r.(resolverByIndex); ok {
98 if ed2 := r.FindEnumByIndex(i, j, file.allEnums, file.allMessages); ed2 != nil {
99 return ed2
100 }
101 }
102 for i := range file.allEnums {
103 if ed2 := &file.allEnums[i]; ed2.L0.FullName == ed.FullName() {
104 return ed2
105 }
106 }
107 if d, _ := r.FindDescriptorByName(ed.FullName()); d != nil {
108 return d.(pref.EnumDescriptor)
109 }
110 return ed
111}
112
113func (file *File) resolveMessageDependency(md pref.MessageDescriptor, i, j int32) pref.MessageDescriptor {
114 r := file.builder.FileRegistry
115 if r, ok := r.(resolverByIndex); ok {
116 if md2 := r.FindMessageByIndex(i, j, file.allEnums, file.allMessages); md2 != nil {
117 return md2
118 }
119 }
120 for i := range file.allMessages {
121 if md2 := &file.allMessages[i]; md2.L0.FullName == md.FullName() {
122 return md2
123 }
124 }
125 if d, _ := r.FindDescriptorByName(md.FullName()); d != nil {
126 return d.(pref.MessageDescriptor)
127 }
128 return md
129}
130
131func (fd *File) unmarshalFull(b []byte) {
132 sb := getBuilder()
133 defer putBuilder(sb)
134
135 var enumIdx, messageIdx, extensionIdx, serviceIdx int
136 var rawOptions []byte
137 fd.L2 = new(FileL2)
138 for len(b) > 0 {
139 num, typ, n := protowire.ConsumeTag(b)
140 b = b[n:]
141 switch typ {
142 case protowire.VarintType:
143 v, m := protowire.ConsumeVarint(b)
144 b = b[m:]
145 switch num {
146 case fieldnum.FileDescriptorProto_PublicDependency:
147 fd.L2.Imports[v].IsPublic = true
148 case fieldnum.FileDescriptorProto_WeakDependency:
149 fd.L2.Imports[v].IsWeak = true
150 }
151 case protowire.BytesType:
152 v, m := protowire.ConsumeBytes(b)
153 b = b[m:]
154 switch num {
155 case fieldnum.FileDescriptorProto_Dependency:
156 path := sb.MakeString(v)
157 imp, _ := fd.builder.FileRegistry.FindFileByPath(path)
158 if imp == nil {
159 imp = PlaceholderFile(path)
160 }
161 fd.L2.Imports = append(fd.L2.Imports, pref.FileImport{FileDescriptor: imp})
162 case fieldnum.FileDescriptorProto_EnumType:
163 fd.L1.Enums.List[enumIdx].unmarshalFull(v, sb)
164 enumIdx++
165 case fieldnum.FileDescriptorProto_MessageType:
166 fd.L1.Messages.List[messageIdx].unmarshalFull(v, sb)
167 messageIdx++
168 case fieldnum.FileDescriptorProto_Extension:
169 fd.L1.Extensions.List[extensionIdx].unmarshalFull(v, sb)
170 extensionIdx++
171 case fieldnum.FileDescriptorProto_Service:
172 fd.L1.Services.List[serviceIdx].unmarshalFull(v, sb)
173 serviceIdx++
174 case fieldnum.FileDescriptorProto_Options:
175 rawOptions = appendOptions(rawOptions, v)
176 }
177 default:
178 m := protowire.ConsumeFieldValue(num, typ, b)
179 b = b[m:]
180 }
181 }
182 fd.L2.Options = fd.builder.optionsUnmarshaler(&descopts.File, rawOptions)
183}
184
185func (ed *Enum) unmarshalFull(b []byte, sb *strs.Builder) {
186 var rawValues [][]byte
187 var rawOptions []byte
188 if !ed.L1.eagerValues {
189 ed.L2 = new(EnumL2)
190 }
191 for len(b) > 0 {
192 num, typ, n := protowire.ConsumeTag(b)
193 b = b[n:]
194 switch typ {
195 case protowire.BytesType:
196 v, m := protowire.ConsumeBytes(b)
197 b = b[m:]
198 switch num {
199 case fieldnum.EnumDescriptorProto_Value:
200 rawValues = append(rawValues, v)
201 case fieldnum.EnumDescriptorProto_ReservedName:
202 ed.L2.ReservedNames.List = append(ed.L2.ReservedNames.List, pref.Name(sb.MakeString(v)))
203 case fieldnum.EnumDescriptorProto_ReservedRange:
204 ed.L2.ReservedRanges.List = append(ed.L2.ReservedRanges.List, unmarshalEnumReservedRange(v))
205 case fieldnum.EnumDescriptorProto_Options:
206 rawOptions = appendOptions(rawOptions, v)
207 }
208 default:
209 m := protowire.ConsumeFieldValue(num, typ, b)
210 b = b[m:]
211 }
212 }
213 if !ed.L1.eagerValues && len(rawValues) > 0 {
214 ed.L2.Values.List = make([]EnumValue, len(rawValues))
215 for i, b := range rawValues {
216 ed.L2.Values.List[i].unmarshalFull(b, sb, ed.L0.ParentFile, ed, i)
217 }
218 }
219 ed.L2.Options = ed.L0.ParentFile.builder.optionsUnmarshaler(&descopts.Enum, rawOptions)
220}
221
222func unmarshalEnumReservedRange(b []byte) (r [2]pref.EnumNumber) {
223 for len(b) > 0 {
224 num, typ, n := protowire.ConsumeTag(b)
225 b = b[n:]
226 switch typ {
227 case protowire.VarintType:
228 v, m := protowire.ConsumeVarint(b)
229 b = b[m:]
230 switch num {
231 case fieldnum.EnumDescriptorProto_EnumReservedRange_Start:
232 r[0] = pref.EnumNumber(v)
233 case fieldnum.EnumDescriptorProto_EnumReservedRange_End:
234 r[1] = pref.EnumNumber(v)
235 }
236 default:
237 m := protowire.ConsumeFieldValue(num, typ, b)
238 b = b[m:]
239 }
240 }
241 return r
242}
243
244func (vd *EnumValue) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd pref.Descriptor, i int) {
245 vd.L0.ParentFile = pf
246 vd.L0.Parent = pd
247 vd.L0.Index = i
248
249 var rawOptions []byte
250 for len(b) > 0 {
251 num, typ, n := protowire.ConsumeTag(b)
252 b = b[n:]
253 switch typ {
254 case protowire.VarintType:
255 v, m := protowire.ConsumeVarint(b)
256 b = b[m:]
257 switch num {
258 case fieldnum.EnumValueDescriptorProto_Number:
259 vd.L1.Number = pref.EnumNumber(v)
260 }
261 case protowire.BytesType:
262 v, m := protowire.ConsumeBytes(b)
263 b = b[m:]
264 switch num {
265 case fieldnum.EnumValueDescriptorProto_Name:
266 // NOTE: Enum values are in the same scope as the enum parent.
267 vd.L0.FullName = appendFullName(sb, pd.Parent().FullName(), v)
268 case fieldnum.EnumValueDescriptorProto_Options:
269 rawOptions = appendOptions(rawOptions, v)
270 }
271 default:
272 m := protowire.ConsumeFieldValue(num, typ, b)
273 b = b[m:]
274 }
275 }
276 vd.L1.Options = pf.builder.optionsUnmarshaler(&descopts.EnumValue, rawOptions)
277}
278
279func (md *Message) unmarshalFull(b []byte, sb *strs.Builder) {
280 var rawFields, rawOneofs [][]byte
281 var enumIdx, messageIdx, extensionIdx int
282 var rawOptions []byte
283 md.L2 = new(MessageL2)
284 for len(b) > 0 {
285 num, typ, n := protowire.ConsumeTag(b)
286 b = b[n:]
287 switch typ {
288 case protowire.BytesType:
289 v, m := protowire.ConsumeBytes(b)
290 b = b[m:]
291 switch num {
292 case fieldnum.DescriptorProto_Field:
293 rawFields = append(rawFields, v)
294 case fieldnum.DescriptorProto_OneofDecl:
295 rawOneofs = append(rawOneofs, v)
296 case fieldnum.DescriptorProto_ReservedName:
297 md.L2.ReservedNames.List = append(md.L2.ReservedNames.List, pref.Name(sb.MakeString(v)))
298 case fieldnum.DescriptorProto_ReservedRange:
299 md.L2.ReservedRanges.List = append(md.L2.ReservedRanges.List, unmarshalMessageReservedRange(v))
300 case fieldnum.DescriptorProto_ExtensionRange:
301 r, rawOptions := unmarshalMessageExtensionRange(v)
302 opts := md.L0.ParentFile.builder.optionsUnmarshaler(&descopts.ExtensionRange, rawOptions)
303 md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, r)
304 md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, opts)
305 case fieldnum.DescriptorProto_EnumType:
306 md.L1.Enums.List[enumIdx].unmarshalFull(v, sb)
307 enumIdx++
308 case fieldnum.DescriptorProto_NestedType:
309 md.L1.Messages.List[messageIdx].unmarshalFull(v, sb)
310 messageIdx++
311 case fieldnum.DescriptorProto_Extension:
312 md.L1.Extensions.List[extensionIdx].unmarshalFull(v, sb)
313 extensionIdx++
314 case fieldnum.DescriptorProto_Options:
315 md.unmarshalOptions(v)
316 rawOptions = appendOptions(rawOptions, v)
317 }
318 default:
319 m := protowire.ConsumeFieldValue(num, typ, b)
320 b = b[m:]
321 }
322 }
323 if len(rawFields) > 0 || len(rawOneofs) > 0 {
324 md.L2.Fields.List = make([]Field, len(rawFields))
325 md.L2.Oneofs.List = make([]Oneof, len(rawOneofs))
326 for i, b := range rawFields {
327 fd := &md.L2.Fields.List[i]
328 fd.unmarshalFull(b, sb, md.L0.ParentFile, md, i)
329 if fd.L1.Cardinality == pref.Required {
330 md.L2.RequiredNumbers.List = append(md.L2.RequiredNumbers.List, fd.L1.Number)
331 }
332 }
333 for i, b := range rawOneofs {
334 od := &md.L2.Oneofs.List[i]
335 od.unmarshalFull(b, sb, md.L0.ParentFile, md, i)
336 }
337 }
338 md.L2.Options = md.L0.ParentFile.builder.optionsUnmarshaler(&descopts.Message, rawOptions)
339}
340
341func (md *Message) unmarshalOptions(b []byte) {
342 for len(b) > 0 {
343 num, typ, n := protowire.ConsumeTag(b)
344 b = b[n:]
345 switch typ {
346 case protowire.VarintType:
347 v, m := protowire.ConsumeVarint(b)
348 b = b[m:]
349 switch num {
350 case fieldnum.MessageOptions_MapEntry:
351 md.L1.IsMapEntry = protowire.DecodeBool(v)
352 case fieldnum.MessageOptions_MessageSetWireFormat:
353 md.L1.IsMessageSet = protowire.DecodeBool(v)
354 }
355 default:
356 m := protowire.ConsumeFieldValue(num, typ, b)
357 b = b[m:]
358 }
359 }
360}
361
362func unmarshalMessageReservedRange(b []byte) (r [2]pref.FieldNumber) {
363 for len(b) > 0 {
364 num, typ, n := protowire.ConsumeTag(b)
365 b = b[n:]
366 switch typ {
367 case protowire.VarintType:
368 v, m := protowire.ConsumeVarint(b)
369 b = b[m:]
370 switch num {
371 case fieldnum.DescriptorProto_ReservedRange_Start:
372 r[0] = pref.FieldNumber(v)
373 case fieldnum.DescriptorProto_ReservedRange_End:
374 r[1] = pref.FieldNumber(v)
375 }
376 default:
377 m := protowire.ConsumeFieldValue(num, typ, b)
378 b = b[m:]
379 }
380 }
381 return r
382}
383
384func unmarshalMessageExtensionRange(b []byte) (r [2]pref.FieldNumber, rawOptions []byte) {
385 for len(b) > 0 {
386 num, typ, n := protowire.ConsumeTag(b)
387 b = b[n:]
388 switch typ {
389 case protowire.VarintType:
390 v, m := protowire.ConsumeVarint(b)
391 b = b[m:]
392 switch num {
393 case fieldnum.DescriptorProto_ExtensionRange_Start:
394 r[0] = pref.FieldNumber(v)
395 case fieldnum.DescriptorProto_ExtensionRange_End:
396 r[1] = pref.FieldNumber(v)
397 }
398 case protowire.BytesType:
399 v, m := protowire.ConsumeBytes(b)
400 b = b[m:]
401 switch num {
402 case fieldnum.DescriptorProto_ExtensionRange_Options:
403 rawOptions = appendOptions(rawOptions, v)
404 }
405 default:
406 m := protowire.ConsumeFieldValue(num, typ, b)
407 b = b[m:]
408 }
409 }
410 return r, rawOptions
411}
412
413func (fd *Field) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd pref.Descriptor, i int) {
414 fd.L0.ParentFile = pf
415 fd.L0.Parent = pd
416 fd.L0.Index = i
417
418 var rawTypeName []byte
419 var rawOptions []byte
420 for len(b) > 0 {
421 num, typ, n := protowire.ConsumeTag(b)
422 b = b[n:]
423 switch typ {
424 case protowire.VarintType:
425 v, m := protowire.ConsumeVarint(b)
426 b = b[m:]
427 switch num {
428 case fieldnum.FieldDescriptorProto_Number:
429 fd.L1.Number = pref.FieldNumber(v)
430 case fieldnum.FieldDescriptorProto_Label:
431 fd.L1.Cardinality = pref.Cardinality(v)
432 case fieldnum.FieldDescriptorProto_Type:
433 fd.L1.Kind = pref.Kind(v)
434 case fieldnum.FieldDescriptorProto_OneofIndex:
435 // In Message.unmarshalFull, we allocate slices for both
436 // the field and oneof descriptors before unmarshaling either
437 // of them. This ensures pointers to slice elements are stable.
438 od := &pd.(*Message).L2.Oneofs.List[v]
439 od.L1.Fields.List = append(od.L1.Fields.List, fd)
440 if fd.L1.ContainingOneof != nil {
441 panic("oneof type already set")
442 }
443 fd.L1.ContainingOneof = od
444 }
445 case protowire.BytesType:
446 v, m := protowire.ConsumeBytes(b)
447 b = b[m:]
448 switch num {
449 case fieldnum.FieldDescriptorProto_Name:
450 fd.L0.FullName = appendFullName(sb, pd.FullName(), v)
451 case fieldnum.FieldDescriptorProto_JsonName:
452 fd.L1.JSONName.Init(sb.MakeString(v))
453 case fieldnum.FieldDescriptorProto_DefaultValue:
454 fd.L1.Default.val = pref.ValueOfBytes(v) // temporarily store as bytes; later resolved in resolveMessages
455 case fieldnum.FieldDescriptorProto_TypeName:
456 rawTypeName = v
457 case fieldnum.FieldDescriptorProto_Options:
458 fd.unmarshalOptions(v)
459 rawOptions = appendOptions(rawOptions, v)
460 }
461 default:
462 m := protowire.ConsumeFieldValue(num, typ, b)
463 b = b[m:]
464 }
465 }
466 if rawTypeName != nil {
467 name := makeFullName(sb, rawTypeName)
468 switch fd.L1.Kind {
469 case pref.EnumKind:
470 fd.L1.Enum = PlaceholderEnum(name)
471 case pref.MessageKind, pref.GroupKind:
472 fd.L1.Message = PlaceholderMessage(name)
473 }
474 }
475 fd.L1.Options = pf.builder.optionsUnmarshaler(&descopts.Field, rawOptions)
476}
477
478func (fd *Field) unmarshalOptions(b []byte) {
479 const FieldOptions_EnforceUTF8 = 13
480
481 for len(b) > 0 {
482 num, typ, n := protowire.ConsumeTag(b)
483 b = b[n:]
484 switch typ {
485 case protowire.VarintType:
486 v, m := protowire.ConsumeVarint(b)
487 b = b[m:]
488 switch num {
489 case fieldnum.FieldOptions_Packed:
490 fd.L1.HasPacked = true
491 fd.L1.IsPacked = protowire.DecodeBool(v)
492 case fieldnum.FieldOptions_Weak:
493 fd.L1.IsWeak = protowire.DecodeBool(v)
494 case FieldOptions_EnforceUTF8:
495 fd.L1.HasEnforceUTF8 = true
496 fd.L1.EnforceUTF8 = protowire.DecodeBool(v)
497 }
498 default:
499 m := protowire.ConsumeFieldValue(num, typ, b)
500 b = b[m:]
501 }
502 }
503}
504
505func (od *Oneof) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd pref.Descriptor, i int) {
506 od.L0.ParentFile = pf
507 od.L0.Parent = pd
508 od.L0.Index = i
509
510 var rawOptions []byte
511 for len(b) > 0 {
512 num, typ, n := protowire.ConsumeTag(b)
513 b = b[n:]
514 switch typ {
515 case protowire.BytesType:
516 v, m := protowire.ConsumeBytes(b)
517 b = b[m:]
518 switch num {
519 case fieldnum.OneofDescriptorProto_Name:
520 od.L0.FullName = appendFullName(sb, pd.FullName(), v)
521 case fieldnum.OneofDescriptorProto_Options:
522 rawOptions = appendOptions(rawOptions, v)
523 }
524 default:
525 m := protowire.ConsumeFieldValue(num, typ, b)
526 b = b[m:]
527 }
528 }
529 od.L1.Options = pf.builder.optionsUnmarshaler(&descopts.Oneof, rawOptions)
530}
531
532func (xd *Extension) unmarshalFull(b []byte, sb *strs.Builder) {
533 var rawTypeName []byte
534 var rawOptions []byte
535 xd.L2 = new(ExtensionL2)
536 for len(b) > 0 {
537 num, typ, n := protowire.ConsumeTag(b)
538 b = b[n:]
539 switch typ {
540 case protowire.BytesType:
541 v, m := protowire.ConsumeBytes(b)
542 b = b[m:]
543 switch num {
544 case fieldnum.FieldDescriptorProto_JsonName:
545 xd.L2.JSONName.Init(sb.MakeString(v))
546 case fieldnum.FieldDescriptorProto_DefaultValue:
547 xd.L2.Default.val = pref.ValueOfBytes(v) // temporarily store as bytes; later resolved in resolveExtensions
548 case fieldnum.FieldDescriptorProto_TypeName:
549 rawTypeName = v
550 case fieldnum.FieldDescriptorProto_Options:
551 xd.unmarshalOptions(v)
552 rawOptions = appendOptions(rawOptions, v)
553 }
554 default:
555 m := protowire.ConsumeFieldValue(num, typ, b)
556 b = b[m:]
557 }
558 }
559 if rawTypeName != nil {
560 name := makeFullName(sb, rawTypeName)
561 switch xd.L1.Kind {
562 case pref.EnumKind:
563 xd.L2.Enum = PlaceholderEnum(name)
564 case pref.MessageKind, pref.GroupKind:
565 xd.L2.Message = PlaceholderMessage(name)
566 }
567 }
568 xd.L2.Options = xd.L0.ParentFile.builder.optionsUnmarshaler(&descopts.Field, rawOptions)
569}
570
571func (xd *Extension) unmarshalOptions(b []byte) {
572 for len(b) > 0 {
573 num, typ, n := protowire.ConsumeTag(b)
574 b = b[n:]
575 switch typ {
576 case protowire.VarintType:
577 v, m := protowire.ConsumeVarint(b)
578 b = b[m:]
579 switch num {
580 case fieldnum.FieldOptions_Packed:
581 xd.L2.IsPacked = protowire.DecodeBool(v)
582 }
583 default:
584 m := protowire.ConsumeFieldValue(num, typ, b)
585 b = b[m:]
586 }
587 }
588}
589
590func (sd *Service) unmarshalFull(b []byte, sb *strs.Builder) {
591 var rawMethods [][]byte
592 var rawOptions []byte
593 sd.L2 = new(ServiceL2)
594 for len(b) > 0 {
595 num, typ, n := protowire.ConsumeTag(b)
596 b = b[n:]
597 switch typ {
598 case protowire.BytesType:
599 v, m := protowire.ConsumeBytes(b)
600 b = b[m:]
601 switch num {
602 case fieldnum.ServiceDescriptorProto_Method:
603 rawMethods = append(rawMethods, v)
604 case fieldnum.ServiceDescriptorProto_Options:
605 rawOptions = appendOptions(rawOptions, v)
606 }
607 default:
608 m := protowire.ConsumeFieldValue(num, typ, b)
609 b = b[m:]
610 }
611 }
612 if len(rawMethods) > 0 {
613 sd.L2.Methods.List = make([]Method, len(rawMethods))
614 for i, b := range rawMethods {
615 sd.L2.Methods.List[i].unmarshalFull(b, sb, sd.L0.ParentFile, sd, i)
616 }
617 }
618 sd.L2.Options = sd.L0.ParentFile.builder.optionsUnmarshaler(&descopts.Service, rawOptions)
619}
620
621func (md *Method) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd pref.Descriptor, i int) {
622 md.L0.ParentFile = pf
623 md.L0.Parent = pd
624 md.L0.Index = i
625
626 var rawOptions []byte
627 for len(b) > 0 {
628 num, typ, n := protowire.ConsumeTag(b)
629 b = b[n:]
630 switch typ {
631 case protowire.VarintType:
632 v, m := protowire.ConsumeVarint(b)
633 b = b[m:]
634 switch num {
635 case fieldnum.MethodDescriptorProto_ClientStreaming:
636 md.L1.IsStreamingClient = protowire.DecodeBool(v)
637 case fieldnum.MethodDescriptorProto_ServerStreaming:
638 md.L1.IsStreamingServer = protowire.DecodeBool(v)
639 }
640 case protowire.BytesType:
641 v, m := protowire.ConsumeBytes(b)
642 b = b[m:]
643 switch num {
644 case fieldnum.MethodDescriptorProto_Name:
645 md.L0.FullName = appendFullName(sb, pd.FullName(), v)
646 case fieldnum.MethodDescriptorProto_InputType:
647 md.L1.Input = PlaceholderMessage(makeFullName(sb, v))
648 case fieldnum.MethodDescriptorProto_OutputType:
649 md.L1.Output = PlaceholderMessage(makeFullName(sb, v))
650 case fieldnum.MethodDescriptorProto_Options:
651 rawOptions = appendOptions(rawOptions, v)
652 }
653 default:
654 m := protowire.ConsumeFieldValue(num, typ, b)
655 b = b[m:]
656 }
657 }
658 md.L1.Options = pf.builder.optionsUnmarshaler(&descopts.Method, rawOptions)
659}
660
661// appendOptions appends src to dst, where the returned slice is never nil.
662// This is necessary to distinguish between empty and unpopulated options.
663func appendOptions(dst, src []byte) []byte {
664 if dst == nil {
665 dst = []byte{}
666 }
667 return append(dst, src...)
668}
669
670// optionsUnmarshaler constructs a lazy unmarshal function for an options message.
671//
672// The type of message to unmarshal to is passed as a pointer since the
673// vars in descopts may not yet be populated at the time this function is called.
674func (db *Builder) optionsUnmarshaler(p *pref.ProtoMessage, b []byte) func() pref.ProtoMessage {
675 if b == nil {
676 return nil
677 }
678 var opts pref.ProtoMessage
679 var once sync.Once
680 return func() pref.ProtoMessage {
681 once.Do(func() {
682 if *p == nil {
683 panic("Descriptor.Options called without importing the descriptor package")
684 }
685 opts = reflect.New(reflect.TypeOf(*p).Elem()).Interface().(pref.ProtoMessage)
686 if err := (proto.UnmarshalOptions{
687 AllowPartial: true,
688 Resolver: db.TypeResolver,
689 }).Unmarshal(b, opts); err != nil {
690 panic(err)
691 }
692 })
693 return opts
694 }
695}