blob: f305ee0faf12430dee88ed0ef222d49e91126a30 [file] [log] [blame]
Damien Neil8012b442019-01-18 09:32:24 -08001// 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 fileinit constructs protoreflect.FileDescriptors from the encoded
6// file descriptor proto messages. This package uses a custom proto unmarshaler
7// 1) to avoid a dependency on the descriptor proto 2) for performance to keep
8// the initialization cost as low as possible.
9package fileinit
10
11import (
12 "fmt"
13 "reflect"
14 "sync"
15
Joe Tsaia2dd2282019-04-10 16:45:04 -070016 descopts "github.com/golang/protobuf/v2/internal/descopts"
Joe Tsai35ec98f2019-03-25 14:41:32 -070017 pimpl "github.com/golang/protobuf/v2/internal/impl"
Damien Neil8012b442019-01-18 09:32:24 -080018 pragma "github.com/golang/protobuf/v2/internal/pragma"
19 pfmt "github.com/golang/protobuf/v2/internal/typefmt"
20 "github.com/golang/protobuf/v2/proto"
21 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsai08cd8842019-03-18 13:46:39 -070022 preg "github.com/golang/protobuf/v2/reflect/protoregistry"
Joe Tsai4fddeba2019-03-20 18:29:32 -070023 piface "github.com/golang/protobuf/v2/runtime/protoiface"
Damien Neil8012b442019-01-18 09:32:24 -080024)
25
26// FileBuilder construct a protoreflect.FileDescriptor from the
27// raw file descriptor and the Go types for declarations and dependencies.
28//
29//
30// Flattened Ordering
31//
32// The protobuf type system represents declarations as a tree. Certain nodes in
33// the tree require us to either associate it with a concrete Go type or to
34// resolve a dependency, which is information that must be provided separately
35// since it cannot be derived from the file descriptor alone.
36//
37// However, representing a tree as Go literals is difficult to simply do in a
38// space and time efficient way. Thus, we store them as a flattened list of
39// objects where the serialization order from the tree-based form is important.
40//
41// The "flattened ordering" is defined as a tree traversal of all enum, message,
42// extension, and service declarations using the following algorithm:
43//
44// def VisitFileDecls(fd):
45// for e in fd.Enums: yield e
46// for m in fd.Messages: yield m
47// for x in fd.Extensions: yield x
48// for s in fd.Services: yield s
49// for m in fd.Messages: yield from VisitMessageDecls(m)
50//
51// def VisitMessageDecls(md):
52// for e in md.Enums: yield e
53// for m in md.Messages: yield m
54// for x in md.Extensions: yield x
55// for m in md.Messages: yield from VisitMessageDecls(m)
56//
57// The traversal starts at the root file descriptor and yields each direct
58// declaration within each node before traversing into sub-declarations
59// that children themselves may have.
60type FileBuilder struct {
61 // RawDescriptor is the wire-encoded bytes of FileDescriptorProto.
62 RawDescriptor []byte
63
64 // GoTypes is a unique set of the Go types for all declarations and
65 // dependencies. Each type is represented as a zero value of the Go type.
66 //
67 // Declarations are Go types generated for enums and messages directly
68 // declared (not publicly imported) in the proto source file.
69 // Messages for map entries are included, but represented by nil.
70 // Enum declarations in "flattened ordering" come first, followed by
71 // message declarations in "flattened ordering". The length of each sub-list
72 // is len(EnumOutputTypes) and len(MessageOutputTypes), respectively.
73 //
74 // Dependencies are Go types for enums or messages referenced by
75 // message fields (excluding weak fields), for parent extended messages of
76 // extension fields, for enums or messages referenced by extension fields,
77 // and for input and output messages referenced by service methods.
78 // Dependencies must come after declarations, but the ordering of
79 // dependencies themselves is unspecified.
80 GoTypes []interface{}
81
82 // DependencyIndexes is an ordered list of indexes into GoTypes for the
83 // dependencies of messages, extensions, or services. There are 4 sub-lists
84 // each in "flattened ordering" concatenated back-to-back:
85 // * Extension field targets: list of the extended parent message of
86 // every extension. Length is len(ExtensionOutputTypes).
87 // * Message field dependencies: list of the enum or message type
88 // referred to by every message field.
89 // * Extension field dependencies: list of the enum or message type
90 // referred to by every extension field.
91 // * Service method dependencies: list of the input and output message type
92 // referred to by every service method.
93 DependencyIndexes []int32
94
95 // TODO: Provide a list of imported files.
96 // FileDependencies []pref.FileDescriptor
97
98 // TODO: Provide a list of extension types for options extensions.
99 // OptionDependencies []pref.ExtensionType
100
Joe Tsaiafb455e2019-03-14 16:08:22 -0700101 // LegacyExtensions are a list of legacy extension descriptors.
102 // If provided, the pointer to the v1 ExtensionDesc will be stored into the
103 // associated v2 ExtensionType and accessible via a pseudo-internal API.
104 // Also, the v2 ExtensionType will be stored into each v1 ExtensionDesc.
105 // If non-nil, len(LegacyExtensions) must equal len(ExtensionOutputTypes).
Joe Tsai4fddeba2019-03-20 18:29:32 -0700106 LegacyExtensions []piface.ExtensionDescV1
Joe Tsaiafb455e2019-03-14 16:08:22 -0700107
Damien Neil8012b442019-01-18 09:32:24 -0800108 // EnumOutputTypes is where Init stores all initialized enum types
109 // in "flattened ordering".
110 EnumOutputTypes []pref.EnumType
111 // MessageOutputTypes is where Init stores all initialized message types
Joe Tsai4532dd72019-03-19 17:04:06 -0700112 // in "flattened ordering". This includes slots for map entry messages,
113 // which are skipped over.
Joe Tsai35ec98f2019-03-25 14:41:32 -0700114 MessageOutputTypes []pimpl.MessageType
Damien Neil8012b442019-01-18 09:32:24 -0800115 // ExtensionOutputTypes is where Init stores all initialized extension types
116 // in "flattened ordering".
117 ExtensionOutputTypes []pref.ExtensionType
118
Joe Tsai08cd8842019-03-18 13:46:39 -0700119 // FilesRegistry is the file registry to register the file descriptor.
120 // If nil, no registration occurs.
121 FilesRegistry *preg.Files
122 // TypesRegistry is the types registry to register each type descriptor.
123 // If nil, no registration occurs.
124 TypesRegistry *preg.Types
Damien Neil8012b442019-01-18 09:32:24 -0800125}
126
127// Init constructs a FileDescriptor given the parameters set in FileBuilder.
128// It assumes that the inputs are well-formed and panics if any inconsistencies
129// are encountered.
130func (fb FileBuilder) Init() pref.FileDescriptor {
131 fd := newFileDesc(fb)
132
Joe Tsai08cd8842019-03-18 13:46:39 -0700133 // Keep v1 and v2 extension descriptors in sync.
Joe Tsaiafb455e2019-03-14 16:08:22 -0700134 if fb.LegacyExtensions != nil {
135 for i := range fd.allExtensions {
136 fd.allExtensions[i].legacyDesc = &fb.LegacyExtensions[i]
137 fb.LegacyExtensions[i].Type = &fd.allExtensions[i]
138 }
139 }
140
Joe Tsai08cd8842019-03-18 13:46:39 -0700141 // Copy type descriptors to the output.
Joe Tsai35ec98f2019-03-25 14:41:32 -0700142 //
143 // While iterating over the messages, we also determine whether the message
144 // is a map entry type.
145 messageGoTypes := fb.GoTypes[len(fd.allEnums):][:len(fd.allMessages)]
Damien Neil8012b442019-01-18 09:32:24 -0800146 for i := range fd.allEnums {
147 fb.EnumOutputTypes[i] = &fd.allEnums[i]
148 }
149 for i := range fd.allMessages {
Joe Tsai35ec98f2019-03-25 14:41:32 -0700150 if messageGoTypes[i] == nil {
151 fd.allMessages[i].isMapEntry = true
152 } else {
153 fb.MessageOutputTypes[i].GoType = reflect.TypeOf(messageGoTypes[i])
154 fb.MessageOutputTypes[i].PBType = fd.allMessages[i].asDesc().(pref.MessageType)
Joe Tsai4532dd72019-03-19 17:04:06 -0700155 }
Damien Neil8012b442019-01-18 09:32:24 -0800156 }
157 for i := range fd.allExtensions {
158 fb.ExtensionOutputTypes[i] = &fd.allExtensions[i]
159 }
Joe Tsai08cd8842019-03-18 13:46:39 -0700160
Joe Tsaia2dd2282019-04-10 16:45:04 -0700161 // As a special-case for descriptor.proto,
162 // locally register concrete message type for the options.
163 if fd.Path() == "google/protobuf/descriptor.proto" && fd.Package() == "google.protobuf" {
164 for i := range fd.allMessages {
165 switch fd.allMessages[i].Name() {
166 case "FileOptions":
167 descopts.File = messageGoTypes[i].(pref.ProtoMessage)
168 case "EnumOptions":
169 descopts.Enum = messageGoTypes[i].(pref.ProtoMessage)
170 case "EnumValueOptions":
171 descopts.EnumValue = messageGoTypes[i].(pref.ProtoMessage)
172 case "MessageOptions":
173 descopts.Message = messageGoTypes[i].(pref.ProtoMessage)
174 case "FieldOptions":
175 descopts.Field = messageGoTypes[i].(pref.ProtoMessage)
176 case "OneofOptions":
177 descopts.Oneof = messageGoTypes[i].(pref.ProtoMessage)
178 case "ExtensionRangeOptions":
179 descopts.ExtensionRange = messageGoTypes[i].(pref.ProtoMessage)
180 case "ServiceOptions":
181 descopts.Service = messageGoTypes[i].(pref.ProtoMessage)
182 case "MethodOptions":
183 descopts.Method = messageGoTypes[i].(pref.ProtoMessage)
184 }
185 }
186 }
187
Joe Tsai08cd8842019-03-18 13:46:39 -0700188 // Register file and type descriptors.
189 if fb.FilesRegistry != nil {
190 if err := fb.FilesRegistry.Register(fd); err != nil {
191 panic(err)
192 }
193 }
194 if fb.TypesRegistry != nil {
195 for i := range fd.allEnums {
196 if err := fb.TypesRegistry.Register(&fd.allEnums[i]); err != nil {
197 panic(err)
198 }
199 }
200 for i := range fd.allMessages {
Joe Tsai4532dd72019-03-19 17:04:06 -0700201 if mt, _ := fd.allMessages[i].asDesc().(pref.MessageType); mt != nil {
202 if err := fb.TypesRegistry.Register(mt); err != nil {
203 panic(err)
204 }
Joe Tsai08cd8842019-03-18 13:46:39 -0700205 }
206 }
207 for i := range fd.allExtensions {
208 if err := fb.TypesRegistry.Register(&fd.allExtensions[i]); err != nil {
209 panic(err)
210 }
211 }
212 }
213
Damien Neil8012b442019-01-18 09:32:24 -0800214 return fd
215}
216
217type (
218 // fileInit contains a copy of certain fields in FileBuilder for use during
219 // lazy initialization upon first use.
220 fileInit struct {
221 RawDescriptor []byte
222 GoTypes []interface{}
223 DependencyIndexes []int32
224 }
225 fileDesc struct {
226 fileInit
227
228 path string
229 protoPackage pref.FullName
230
231 fileDecls
232
233 enums enumDescs
234 messages messageDescs
235 extensions extensionDescs
236 services serviceDescs
237
238 once sync.Once
239 lazy *fileLazy // protected by once
240 }
241 fileDecls struct {
242 allEnums []enumDesc
243 allMessages []messageDesc
244 allExtensions []extensionDesc
245 }
246 fileLazy struct {
247 syntax pref.Syntax
248 imports fileImports
249 byName map[pref.FullName]pref.Descriptor
250 options []byte
251 }
252)
253
254func (fd *fileDesc) Parent() (pref.Descriptor, bool) { return nil, false }
255func (fd *fileDesc) Index() int { return 0 }
256func (fd *fileDesc) Syntax() pref.Syntax { return fd.lazyInit().syntax }
257func (fd *fileDesc) Name() pref.Name { return fd.Package().Name() }
258func (fd *fileDesc) FullName() pref.FullName { return fd.Package() }
259func (fd *fileDesc) IsPlaceholder() bool { return false }
Joe Tsaia2dd2282019-04-10 16:45:04 -0700260func (fd *fileDesc) Options() pref.ProtoMessage {
261 return unmarshalOptions(descopts.File, fd.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800262}
263func (fd *fileDesc) Path() string { return fd.path }
264func (fd *fileDesc) Package() pref.FullName { return fd.protoPackage }
265func (fd *fileDesc) Imports() pref.FileImports { return &fd.lazyInit().imports }
266func (fd *fileDesc) Enums() pref.EnumDescriptors { return &fd.enums }
267func (fd *fileDesc) Messages() pref.MessageDescriptors { return &fd.messages }
268func (fd *fileDesc) Extensions() pref.ExtensionDescriptors { return &fd.extensions }
269func (fd *fileDesc) Services() pref.ServiceDescriptors { return &fd.services }
270func (fd *fileDesc) DescriptorByName(s pref.FullName) pref.Descriptor { return fd.lazyInit().byName[s] }
271func (fd *fileDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, fd) }
272func (fd *fileDesc) ProtoType(pref.FileDescriptor) {}
273func (fd *fileDesc) ProtoInternal(pragma.DoNotImplement) {}
274
275type (
276 enumDesc struct {
277 baseDesc
278
279 lazy *enumLazy // protected by fileDesc.once
280 }
281 enumLazy struct {
282 typ reflect.Type
283 new func(pref.EnumNumber) pref.Enum
284
285 values enumValueDescs
286 resvNames names
287 resvRanges enumRanges
288 options []byte
289 }
290 enumValueDesc struct {
291 baseDesc
292
293 number pref.EnumNumber
294 options []byte
295 }
296)
297
298func (ed *enumDesc) GoType() reflect.Type { return ed.lazyInit().typ }
299func (ed *enumDesc) New(n pref.EnumNumber) pref.Enum { return ed.lazyInit().new(n) }
Joe Tsaia2dd2282019-04-10 16:45:04 -0700300func (ed *enumDesc) Options() pref.ProtoMessage {
301 return unmarshalOptions(descopts.Enum, ed.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800302}
303func (ed *enumDesc) Values() pref.EnumValueDescriptors { return &ed.lazyInit().values }
304func (ed *enumDesc) ReservedNames() pref.Names { return &ed.lazyInit().resvNames }
305func (ed *enumDesc) ReservedRanges() pref.EnumRanges { return &ed.lazyInit().resvRanges }
306func (ed *enumDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, ed) }
307func (ed *enumDesc) ProtoType(pref.EnumDescriptor) {}
308func (ed *enumDesc) lazyInit() *enumLazy {
309 ed.parentFile.lazyInit() // implicitly initializes enumLazy
310 return ed.lazy
311}
312
Joe Tsaia2dd2282019-04-10 16:45:04 -0700313func (ed *enumValueDesc) Options() pref.ProtoMessage {
314 return unmarshalOptions(descopts.EnumValue, ed.options)
Damien Neil8012b442019-01-18 09:32:24 -0800315}
316func (ed *enumValueDesc) Number() pref.EnumNumber { return ed.number }
317func (ed *enumValueDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, ed) }
318func (ed *enumValueDesc) ProtoType(pref.EnumValueDescriptor) {}
319
320type (
Joe Tsai4532dd72019-03-19 17:04:06 -0700321 messageType struct{ *messageDesc }
322 messageDescriptor struct{ *messageDesc }
323
324 // messageDesc does not implement protoreflect.Descriptor to avoid
325 // accidental usages of it as such. Use the asDesc method to retrieve one.
Damien Neil8012b442019-01-18 09:32:24 -0800326 messageDesc struct {
327 baseDesc
328
329 enums enumDescs
330 messages messageDescs
331 extensions extensionDescs
332
Joe Tsai4532dd72019-03-19 17:04:06 -0700333 isMapEntry bool
334 lazy *messageLazy // protected by fileDesc.once
Damien Neil8012b442019-01-18 09:32:24 -0800335 }
336 messageLazy struct {
337 typ reflect.Type
338 new func() pref.Message
339
Joe Tsai1321a0e2019-03-20 09:46:22 -0700340 isMessageSet bool
Damien Neil8012b442019-01-18 09:32:24 -0800341 fields fieldDescs
342 oneofs oneofDescs
343 resvNames names
344 resvRanges fieldRanges
345 reqNumbers fieldNumbers
346 extRanges fieldRanges
347 extRangeOptions [][]byte
348 options []byte
349 }
350 fieldDesc struct {
351 baseDesc
352
353 number pref.FieldNumber
354 cardinality pref.Cardinality
355 kind pref.Kind
356 hasJSONName bool
357 jsonName string
358 hasPacked bool
359 isPacked bool
360 isWeak bool
361 isMap bool
362 defVal defaultValue
363 oneofType pref.OneofDescriptor
364 enumType pref.EnumDescriptor
365 messageType pref.MessageDescriptor
366 options []byte
367 }
368 oneofDesc struct {
369 baseDesc
370
371 fields oneofFields
372 options []byte
373 }
374)
375
Joe Tsaia2dd2282019-04-10 16:45:04 -0700376func (md *messageDesc) options() pref.ProtoMessage {
377 return unmarshalOptions(descopts.Message, md.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800378}
Joe Tsai4532dd72019-03-19 17:04:06 -0700379func (md *messageDesc) IsMapEntry() bool { return md.isMapEntry }
Damien Neil8012b442019-01-18 09:32:24 -0800380func (md *messageDesc) Fields() pref.FieldDescriptors { return &md.lazyInit().fields }
381func (md *messageDesc) Oneofs() pref.OneofDescriptors { return &md.lazyInit().oneofs }
382func (md *messageDesc) ReservedNames() pref.Names { return &md.lazyInit().resvNames }
383func (md *messageDesc) ReservedRanges() pref.FieldRanges { return &md.lazyInit().resvRanges }
384func (md *messageDesc) RequiredNumbers() pref.FieldNumbers { return &md.lazyInit().reqNumbers }
385func (md *messageDesc) ExtensionRanges() pref.FieldRanges { return &md.lazyInit().extRanges }
Joe Tsaia2dd2282019-04-10 16:45:04 -0700386func (md *messageDesc) ExtensionRangeOptions(i int) pref.ProtoMessage {
387 return unmarshalOptions(descopts.ExtensionRange, md.lazyInit().extRangeOptions[i])
Damien Neil8012b442019-01-18 09:32:24 -0800388}
389func (md *messageDesc) Enums() pref.EnumDescriptors { return &md.enums }
390func (md *messageDesc) Messages() pref.MessageDescriptors { return &md.messages }
391func (md *messageDesc) Extensions() pref.ExtensionDescriptors { return &md.extensions }
Damien Neil8012b442019-01-18 09:32:24 -0800392func (md *messageDesc) ProtoType(pref.MessageDescriptor) {}
Joe Tsai4532dd72019-03-19 17:04:06 -0700393func (md *messageDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, md.asDesc()) }
Damien Neil8012b442019-01-18 09:32:24 -0800394func (md *messageDesc) lazyInit() *messageLazy {
395 md.parentFile.lazyInit() // implicitly initializes messageLazy
396 return md.lazy
397}
398
Joe Tsai1321a0e2019-03-20 09:46:22 -0700399// IsMessageSet is a pseudo-internal API for checking whether a message
400// should serialize in the proto1 message format.
Joe Tsaia5f43e82019-04-10 17:11:20 -0700401//
402// WARNING: This method is exempt from the compatibility promise and may be
403// removed in the future without warning.
Joe Tsai1321a0e2019-03-20 09:46:22 -0700404func (md *messageDesc) IsMessageSet() bool {
405 return md.lazyInit().isMessageSet
406}
407
Joe Tsai4532dd72019-03-19 17:04:06 -0700408// asDesc returns a protoreflect.MessageDescriptor or protoreflect.MessageType
409// depending on whether the message is a map entry or not.
410func (mb *messageDesc) asDesc() pref.MessageDescriptor {
411 if !mb.isMapEntry {
412 return messageType{mb}
413 }
414 return messageDescriptor{mb}
415}
Joe Tsaia2dd2282019-04-10 16:45:04 -0700416func (mt messageType) GoType() reflect.Type { return mt.lazyInit().typ }
417func (mt messageType) New() pref.Message { return mt.lazyInit().new() }
418func (mt messageType) Options() pref.ProtoMessage { return mt.options() }
419func (md messageDescriptor) Options() pref.ProtoMessage { return md.options() }
Joe Tsai4532dd72019-03-19 17:04:06 -0700420
Joe Tsaia2dd2282019-04-10 16:45:04 -0700421func (fd *fieldDesc) Options() pref.ProtoMessage {
422 return unmarshalOptions(descopts.Field, fd.options)
Damien Neil8012b442019-01-18 09:32:24 -0800423}
424func (fd *fieldDesc) Number() pref.FieldNumber { return fd.number }
425func (fd *fieldDesc) Cardinality() pref.Cardinality { return fd.cardinality }
426func (fd *fieldDesc) Kind() pref.Kind { return fd.kind }
427func (fd *fieldDesc) HasJSONName() bool { return fd.hasJSONName }
428func (fd *fieldDesc) JSONName() string { return fd.jsonName }
429func (fd *fieldDesc) IsPacked() bool { return fd.isPacked }
430func (fd *fieldDesc) IsWeak() bool { return fd.isWeak }
431func (fd *fieldDesc) IsMap() bool { return fd.isMap }
432func (fd *fieldDesc) HasDefault() bool { return fd.defVal.has }
433func (fd *fieldDesc) Default() pref.Value { return fd.defVal.get() }
434func (fd *fieldDesc) DefaultEnumValue() pref.EnumValueDescriptor { return fd.defVal.enum }
435func (fd *fieldDesc) OneofType() pref.OneofDescriptor { return fd.oneofType }
436func (fd *fieldDesc) ExtendedType() pref.MessageDescriptor { return nil }
437func (fd *fieldDesc) EnumType() pref.EnumDescriptor { return fd.enumType }
438func (fd *fieldDesc) MessageType() pref.MessageDescriptor { return fd.messageType }
439func (fd *fieldDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, fd) }
440func (fd *fieldDesc) ProtoType(pref.FieldDescriptor) {}
441
Joe Tsaia2dd2282019-04-10 16:45:04 -0700442func (od *oneofDesc) Options() pref.ProtoMessage {
443 return unmarshalOptions(descopts.Oneof, od.options)
Damien Neil8012b442019-01-18 09:32:24 -0800444}
445func (od *oneofDesc) Fields() pref.FieldDescriptors { return &od.fields }
446func (od *oneofDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, od) }
447func (od *oneofDesc) ProtoType(pref.OneofDescriptor) {}
448
449type (
450 extensionDesc struct {
451 baseDesc
452
453 number pref.FieldNumber
454 extendedType pref.MessageDescriptor
455
Joe Tsai4fddeba2019-03-20 18:29:32 -0700456 legacyDesc *piface.ExtensionDescV1
Joe Tsaiafb455e2019-03-14 16:08:22 -0700457
Damien Neil8012b442019-01-18 09:32:24 -0800458 lazy *extensionLazy // protected by fileDesc.once
459 }
460 extensionLazy struct {
461 typ reflect.Type
462 new func() pref.Value
463 valueOf func(interface{}) pref.Value
464 interfaceOf func(pref.Value) interface{}
465
466 cardinality pref.Cardinality
467 kind pref.Kind
468 // Extensions should not have JSON names, but older versions of protoc
469 // used to set one on the descriptor. Preserve it for now to maintain
470 // the property that protoc 3.6.1 descriptors can round-trip through
471 // this package losslessly.
472 //
473 // TODO: Consider whether to drop JSONName parsing from extensions.
474 hasJSONName bool
475 jsonName string
476 isPacked bool
477 defVal defaultValue
478 enumType pref.EnumType
479 messageType pref.MessageType
480 options []byte
481 }
482)
483
484func (xd *extensionDesc) GoType() reflect.Type { return xd.lazyInit().typ }
485func (xd *extensionDesc) New() pref.Value { return xd.lazyInit().new() }
486func (xd *extensionDesc) ValueOf(v interface{}) pref.Value { return xd.lazyInit().valueOf(v) }
487func (xd *extensionDesc) InterfaceOf(v pref.Value) interface{} { return xd.lazyInit().interfaceOf(v) }
Joe Tsaia2dd2282019-04-10 16:45:04 -0700488func (xd *extensionDesc) Options() pref.ProtoMessage {
489 return unmarshalOptions(descopts.Field, xd.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800490}
491func (xd *extensionDesc) Number() pref.FieldNumber { return xd.number }
492func (xd *extensionDesc) Cardinality() pref.Cardinality { return xd.lazyInit().cardinality }
493func (xd *extensionDesc) Kind() pref.Kind { return xd.lazyInit().kind }
494func (xd *extensionDesc) HasJSONName() bool { return xd.lazyInit().hasJSONName }
495func (xd *extensionDesc) JSONName() string { return xd.lazyInit().jsonName }
496func (xd *extensionDesc) IsPacked() bool { return xd.lazyInit().isPacked }
497func (xd *extensionDesc) IsWeak() bool { return false }
498func (xd *extensionDesc) IsMap() bool { return false }
499func (xd *extensionDesc) HasDefault() bool { return xd.lazyInit().defVal.has }
500func (xd *extensionDesc) Default() pref.Value { return xd.lazyInit().defVal.get() }
501func (xd *extensionDesc) DefaultEnumValue() pref.EnumValueDescriptor { return xd.lazyInit().defVal.enum }
502func (xd *extensionDesc) OneofType() pref.OneofDescriptor { return nil }
503func (xd *extensionDesc) ExtendedType() pref.MessageDescriptor { return xd.extendedType }
504func (xd *extensionDesc) EnumType() pref.EnumDescriptor { return xd.lazyInit().enumType }
505func (xd *extensionDesc) MessageType() pref.MessageDescriptor { return xd.lazyInit().messageType }
506func (xd *extensionDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, xd) }
507func (xd *extensionDesc) ProtoType(pref.FieldDescriptor) {}
508func (xd *extensionDesc) ProtoInternal(pragma.DoNotImplement) {}
509func (xd *extensionDesc) lazyInit() *extensionLazy {
510 xd.parentFile.lazyInit() // implicitly initializes extensionLazy
511 return xd.lazy
512}
513
Joe Tsaiafb455e2019-03-14 16:08:22 -0700514// ProtoLegacyExtensionDesc is a pseudo-internal API for allowing the v1 code
515// to be able to retrieve a v1 ExtensionDesc.
Joe Tsaia5f43e82019-04-10 17:11:20 -0700516//
517// WARNING: This method is exempt from the compatibility promise and may be
518// removed in the future without warning.
519func (xd *extensionDesc) ProtoLegacyExtensionDesc() *piface.ExtensionDescV1 {
520 return xd.legacyDesc
521}
Joe Tsaiafb455e2019-03-14 16:08:22 -0700522
Damien Neil8012b442019-01-18 09:32:24 -0800523type (
524 serviceDesc struct {
525 baseDesc
526
527 lazy *serviceLazy // protected by fileDesc.once
528 }
529 serviceLazy struct {
530 methods methodDescs
531 options []byte
532 }
533 methodDesc struct {
534 baseDesc
535
536 inputType pref.MessageDescriptor
537 outputType pref.MessageDescriptor
538 isStreamingClient bool
539 isStreamingServer bool
540 options []byte
541 }
542)
543
Joe Tsaia2dd2282019-04-10 16:45:04 -0700544func (sd *serviceDesc) Options() pref.ProtoMessage {
545 return unmarshalOptions(descopts.Service, sd.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800546}
547func (sd *serviceDesc) Methods() pref.MethodDescriptors { return &sd.lazyInit().methods }
548func (sd *serviceDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, sd) }
549func (sd *serviceDesc) ProtoType(pref.ServiceDescriptor) {}
550func (sd *serviceDesc) ProtoInternal(pragma.DoNotImplement) {}
551func (sd *serviceDesc) lazyInit() *serviceLazy {
552 sd.parentFile.lazyInit() // implicitly initializes serviceLazy
553 return sd.lazy
554}
555
Joe Tsaia2dd2282019-04-10 16:45:04 -0700556func (md *methodDesc) Options() pref.ProtoMessage {
557 return unmarshalOptions(descopts.Method, md.options)
Damien Neil8012b442019-01-18 09:32:24 -0800558}
559func (md *methodDesc) InputType() pref.MessageDescriptor { return md.inputType }
560func (md *methodDesc) OutputType() pref.MessageDescriptor { return md.outputType }
561func (md *methodDesc) IsStreamingClient() bool { return md.isStreamingClient }
562func (md *methodDesc) IsStreamingServer() bool { return md.isStreamingServer }
563func (md *methodDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, md) }
564func (md *methodDesc) ProtoType(pref.MethodDescriptor) {}
565func (md *methodDesc) ProtoInternal(pragma.DoNotImplement) {}
566
567type baseDesc struct {
568 parentFile *fileDesc
569 parent pref.Descriptor
570 index int
571 fullName
572}
573
574func (d *baseDesc) Parent() (pref.Descriptor, bool) { return d.parent, true }
575func (d *baseDesc) Index() int { return d.index }
576func (d *baseDesc) Syntax() pref.Syntax { return d.parentFile.Syntax() }
577func (d *baseDesc) IsPlaceholder() bool { return false }
578func (d *baseDesc) ProtoInternal(pragma.DoNotImplement) {}
579
580type fullName struct {
581 shortLen int
582 fullName pref.FullName
583}
584
585func (s *fullName) Name() pref.Name { return pref.Name(s.fullName[len(s.fullName)-s.shortLen:]) }
586func (s *fullName) FullName() pref.FullName { return s.fullName }
587
Joe Tsaia2dd2282019-04-10 16:45:04 -0700588func unmarshalOptions(p pref.ProtoMessage, b []byte) pref.ProtoMessage {
Damien Neil8012b442019-01-18 09:32:24 -0800589 if b != nil {
590 // TODO: Consider caching the unmarshaled options message.
Joe Tsaia2dd2282019-04-10 16:45:04 -0700591 p = reflect.New(reflect.TypeOf(p).Elem()).Interface().(pref.ProtoMessage)
Damien Neil8012b442019-01-18 09:32:24 -0800592 if err := proto.Unmarshal(b, p.(proto.Message)); err != nil {
593 panic(err)
594 }
595 }
596 return p.(proto.Message)
597}