blob: 120b7b90ff94d849270a4b6afc3300e7efd3411d [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 {
Damien Neil8012b442019-01-18 09:32:24 -0800221 GoTypes []interface{}
222 DependencyIndexes []int32
223 }
224 fileDesc struct {
225 fileInit
Joe Tsaie089c0f2019-04-16 01:46:14 -0700226 rawDesc []byte
Damien Neil8012b442019-01-18 09:32:24 -0800227
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
Damien Neil8012b442019-01-18 09:32:24 -0800249 options []byte
250 }
251)
252
253func (fd *fileDesc) Parent() (pref.Descriptor, bool) { return nil, false }
254func (fd *fileDesc) Index() int { return 0 }
255func (fd *fileDesc) Syntax() pref.Syntax { return fd.lazyInit().syntax }
256func (fd *fileDesc) Name() pref.Name { return fd.Package().Name() }
257func (fd *fileDesc) FullName() pref.FullName { return fd.Package() }
258func (fd *fileDesc) IsPlaceholder() bool { return false }
Joe Tsaia2dd2282019-04-10 16:45:04 -0700259func (fd *fileDesc) Options() pref.ProtoMessage {
260 return unmarshalOptions(descopts.File, fd.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800261}
Damien Neil2300c182019-04-15 13:05:13 -0700262func (fd *fileDesc) Path() string { return fd.path }
263func (fd *fileDesc) Package() pref.FullName { return fd.protoPackage }
264func (fd *fileDesc) Imports() pref.FileImports { return &fd.lazyInit().imports }
265func (fd *fileDesc) Enums() pref.EnumDescriptors { return &fd.enums }
266func (fd *fileDesc) Messages() pref.MessageDescriptors { return &fd.messages }
267func (fd *fileDesc) Extensions() pref.ExtensionDescriptors { return &fd.extensions }
268func (fd *fileDesc) Services() pref.ServiceDescriptors { return &fd.services }
269func (fd *fileDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, fd) }
270func (fd *fileDesc) ProtoType(pref.FileDescriptor) {}
271func (fd *fileDesc) ProtoInternal(pragma.DoNotImplement) {}
Damien Neil8012b442019-01-18 09:32:24 -0800272
Joe Tsaie089c0f2019-04-16 01:46:14 -0700273// ProtoLegacyRawDesc is a pseudo-internal API for allowing the v1 code
274// to be able to retrieve the raw descriptor.
275//
276// WARNING: This method is exempt from the compatibility promise and may be
277// removed in the future without warning.
278func (fd *fileDesc) ProtoLegacyRawDesc() []byte {
279 return fd.rawDesc
280}
281
Damien Neil8012b442019-01-18 09:32:24 -0800282type (
283 enumDesc struct {
284 baseDesc
285
286 lazy *enumLazy // protected by fileDesc.once
287 }
288 enumLazy struct {
289 typ reflect.Type
290 new func(pref.EnumNumber) pref.Enum
291
292 values enumValueDescs
293 resvNames names
294 resvRanges enumRanges
295 options []byte
296 }
297 enumValueDesc struct {
298 baseDesc
299
300 number pref.EnumNumber
301 options []byte
302 }
303)
304
305func (ed *enumDesc) GoType() reflect.Type { return ed.lazyInit().typ }
306func (ed *enumDesc) New(n pref.EnumNumber) pref.Enum { return ed.lazyInit().new(n) }
Joe Tsaia2dd2282019-04-10 16:45:04 -0700307func (ed *enumDesc) Options() pref.ProtoMessage {
308 return unmarshalOptions(descopts.Enum, ed.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800309}
310func (ed *enumDesc) Values() pref.EnumValueDescriptors { return &ed.lazyInit().values }
311func (ed *enumDesc) ReservedNames() pref.Names { return &ed.lazyInit().resvNames }
312func (ed *enumDesc) ReservedRanges() pref.EnumRanges { return &ed.lazyInit().resvRanges }
313func (ed *enumDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, ed) }
314func (ed *enumDesc) ProtoType(pref.EnumDescriptor) {}
315func (ed *enumDesc) lazyInit() *enumLazy {
316 ed.parentFile.lazyInit() // implicitly initializes enumLazy
317 return ed.lazy
318}
319
Joe Tsaia2dd2282019-04-10 16:45:04 -0700320func (ed *enumValueDesc) Options() pref.ProtoMessage {
321 return unmarshalOptions(descopts.EnumValue, ed.options)
Damien Neil8012b442019-01-18 09:32:24 -0800322}
323func (ed *enumValueDesc) Number() pref.EnumNumber { return ed.number }
324func (ed *enumValueDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, ed) }
325func (ed *enumValueDesc) ProtoType(pref.EnumValueDescriptor) {}
326
327type (
Joe Tsai4532dd72019-03-19 17:04:06 -0700328 messageType struct{ *messageDesc }
329 messageDescriptor struct{ *messageDesc }
330
331 // messageDesc does not implement protoreflect.Descriptor to avoid
332 // accidental usages of it as such. Use the asDesc method to retrieve one.
Damien Neil8012b442019-01-18 09:32:24 -0800333 messageDesc struct {
334 baseDesc
335
336 enums enumDescs
337 messages messageDescs
338 extensions extensionDescs
339
Joe Tsai4532dd72019-03-19 17:04:06 -0700340 isMapEntry bool
341 lazy *messageLazy // protected by fileDesc.once
Damien Neil8012b442019-01-18 09:32:24 -0800342 }
343 messageLazy struct {
344 typ reflect.Type
345 new func() pref.Message
346
Joe Tsai1321a0e2019-03-20 09:46:22 -0700347 isMessageSet bool
Damien Neil8012b442019-01-18 09:32:24 -0800348 fields fieldDescs
349 oneofs oneofDescs
350 resvNames names
351 resvRanges fieldRanges
352 reqNumbers fieldNumbers
353 extRanges fieldRanges
354 extRangeOptions [][]byte
355 options []byte
356 }
357 fieldDesc struct {
358 baseDesc
359
360 number pref.FieldNumber
361 cardinality pref.Cardinality
362 kind pref.Kind
363 hasJSONName bool
364 jsonName string
365 hasPacked bool
366 isPacked bool
367 isWeak bool
368 isMap bool
369 defVal defaultValue
370 oneofType pref.OneofDescriptor
371 enumType pref.EnumDescriptor
372 messageType pref.MessageDescriptor
373 options []byte
374 }
375 oneofDesc struct {
376 baseDesc
377
378 fields oneofFields
379 options []byte
380 }
381)
382
Joe Tsaia2dd2282019-04-10 16:45:04 -0700383func (md *messageDesc) options() pref.ProtoMessage {
384 return unmarshalOptions(descopts.Message, md.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800385}
Joe Tsai4532dd72019-03-19 17:04:06 -0700386func (md *messageDesc) IsMapEntry() bool { return md.isMapEntry }
Damien Neil8012b442019-01-18 09:32:24 -0800387func (md *messageDesc) Fields() pref.FieldDescriptors { return &md.lazyInit().fields }
388func (md *messageDesc) Oneofs() pref.OneofDescriptors { return &md.lazyInit().oneofs }
389func (md *messageDesc) ReservedNames() pref.Names { return &md.lazyInit().resvNames }
390func (md *messageDesc) ReservedRanges() pref.FieldRanges { return &md.lazyInit().resvRanges }
391func (md *messageDesc) RequiredNumbers() pref.FieldNumbers { return &md.lazyInit().reqNumbers }
392func (md *messageDesc) ExtensionRanges() pref.FieldRanges { return &md.lazyInit().extRanges }
Joe Tsaia2dd2282019-04-10 16:45:04 -0700393func (md *messageDesc) ExtensionRangeOptions(i int) pref.ProtoMessage {
394 return unmarshalOptions(descopts.ExtensionRange, md.lazyInit().extRangeOptions[i])
Damien Neil8012b442019-01-18 09:32:24 -0800395}
396func (md *messageDesc) Enums() pref.EnumDescriptors { return &md.enums }
397func (md *messageDesc) Messages() pref.MessageDescriptors { return &md.messages }
398func (md *messageDesc) Extensions() pref.ExtensionDescriptors { return &md.extensions }
Damien Neil8012b442019-01-18 09:32:24 -0800399func (md *messageDesc) ProtoType(pref.MessageDescriptor) {}
Joe Tsai4532dd72019-03-19 17:04:06 -0700400func (md *messageDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, md.asDesc()) }
Damien Neil8012b442019-01-18 09:32:24 -0800401func (md *messageDesc) lazyInit() *messageLazy {
402 md.parentFile.lazyInit() // implicitly initializes messageLazy
403 return md.lazy
404}
405
Joe Tsai1321a0e2019-03-20 09:46:22 -0700406// IsMessageSet is a pseudo-internal API for checking whether a message
407// should serialize in the proto1 message format.
Joe Tsaia5f43e82019-04-10 17:11:20 -0700408//
409// WARNING: This method is exempt from the compatibility promise and may be
410// removed in the future without warning.
Joe Tsai1321a0e2019-03-20 09:46:22 -0700411func (md *messageDesc) IsMessageSet() bool {
412 return md.lazyInit().isMessageSet
413}
414
Joe Tsai4532dd72019-03-19 17:04:06 -0700415// asDesc returns a protoreflect.MessageDescriptor or protoreflect.MessageType
416// depending on whether the message is a map entry or not.
417func (mb *messageDesc) asDesc() pref.MessageDescriptor {
418 if !mb.isMapEntry {
419 return messageType{mb}
420 }
421 return messageDescriptor{mb}
422}
Joe Tsaia2dd2282019-04-10 16:45:04 -0700423func (mt messageType) GoType() reflect.Type { return mt.lazyInit().typ }
424func (mt messageType) New() pref.Message { return mt.lazyInit().new() }
425func (mt messageType) Options() pref.ProtoMessage { return mt.options() }
426func (md messageDescriptor) Options() pref.ProtoMessage { return md.options() }
Joe Tsai4532dd72019-03-19 17:04:06 -0700427
Joe Tsaia2dd2282019-04-10 16:45:04 -0700428func (fd *fieldDesc) Options() pref.ProtoMessage {
429 return unmarshalOptions(descopts.Field, fd.options)
Damien Neil8012b442019-01-18 09:32:24 -0800430}
431func (fd *fieldDesc) Number() pref.FieldNumber { return fd.number }
432func (fd *fieldDesc) Cardinality() pref.Cardinality { return fd.cardinality }
433func (fd *fieldDesc) Kind() pref.Kind { return fd.kind }
434func (fd *fieldDesc) HasJSONName() bool { return fd.hasJSONName }
435func (fd *fieldDesc) JSONName() string { return fd.jsonName }
436func (fd *fieldDesc) IsPacked() bool { return fd.isPacked }
437func (fd *fieldDesc) IsWeak() bool { return fd.isWeak }
438func (fd *fieldDesc) IsMap() bool { return fd.isMap }
439func (fd *fieldDesc) HasDefault() bool { return fd.defVal.has }
440func (fd *fieldDesc) Default() pref.Value { return fd.defVal.get() }
441func (fd *fieldDesc) DefaultEnumValue() pref.EnumValueDescriptor { return fd.defVal.enum }
442func (fd *fieldDesc) OneofType() pref.OneofDescriptor { return fd.oneofType }
443func (fd *fieldDesc) ExtendedType() pref.MessageDescriptor { return nil }
444func (fd *fieldDesc) EnumType() pref.EnumDescriptor { return fd.enumType }
445func (fd *fieldDesc) MessageType() pref.MessageDescriptor { return fd.messageType }
446func (fd *fieldDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, fd) }
447func (fd *fieldDesc) ProtoType(pref.FieldDescriptor) {}
448
Joe Tsaia2dd2282019-04-10 16:45:04 -0700449func (od *oneofDesc) Options() pref.ProtoMessage {
450 return unmarshalOptions(descopts.Oneof, od.options)
Damien Neil8012b442019-01-18 09:32:24 -0800451}
452func (od *oneofDesc) Fields() pref.FieldDescriptors { return &od.fields }
453func (od *oneofDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, od) }
454func (od *oneofDesc) ProtoType(pref.OneofDescriptor) {}
455
456type (
457 extensionDesc struct {
458 baseDesc
459
460 number pref.FieldNumber
461 extendedType pref.MessageDescriptor
462
Joe Tsai4fddeba2019-03-20 18:29:32 -0700463 legacyDesc *piface.ExtensionDescV1
Joe Tsaiafb455e2019-03-14 16:08:22 -0700464
Damien Neil8012b442019-01-18 09:32:24 -0800465 lazy *extensionLazy // protected by fileDesc.once
466 }
467 extensionLazy struct {
468 typ reflect.Type
469 new func() pref.Value
470 valueOf func(interface{}) pref.Value
471 interfaceOf func(pref.Value) interface{}
472
473 cardinality pref.Cardinality
474 kind pref.Kind
475 // Extensions should not have JSON names, but older versions of protoc
476 // used to set one on the descriptor. Preserve it for now to maintain
477 // the property that protoc 3.6.1 descriptors can round-trip through
478 // this package losslessly.
479 //
480 // TODO: Consider whether to drop JSONName parsing from extensions.
481 hasJSONName bool
482 jsonName string
483 isPacked bool
484 defVal defaultValue
485 enumType pref.EnumType
486 messageType pref.MessageType
487 options []byte
488 }
489)
490
491func (xd *extensionDesc) GoType() reflect.Type { return xd.lazyInit().typ }
492func (xd *extensionDesc) New() pref.Value { return xd.lazyInit().new() }
493func (xd *extensionDesc) ValueOf(v interface{}) pref.Value { return xd.lazyInit().valueOf(v) }
494func (xd *extensionDesc) InterfaceOf(v pref.Value) interface{} { return xd.lazyInit().interfaceOf(v) }
Joe Tsaia2dd2282019-04-10 16:45:04 -0700495func (xd *extensionDesc) Options() pref.ProtoMessage {
496 return unmarshalOptions(descopts.Field, xd.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800497}
498func (xd *extensionDesc) Number() pref.FieldNumber { return xd.number }
499func (xd *extensionDesc) Cardinality() pref.Cardinality { return xd.lazyInit().cardinality }
500func (xd *extensionDesc) Kind() pref.Kind { return xd.lazyInit().kind }
501func (xd *extensionDesc) HasJSONName() bool { return xd.lazyInit().hasJSONName }
502func (xd *extensionDesc) JSONName() string { return xd.lazyInit().jsonName }
503func (xd *extensionDesc) IsPacked() bool { return xd.lazyInit().isPacked }
504func (xd *extensionDesc) IsWeak() bool { return false }
505func (xd *extensionDesc) IsMap() bool { return false }
506func (xd *extensionDesc) HasDefault() bool { return xd.lazyInit().defVal.has }
507func (xd *extensionDesc) Default() pref.Value { return xd.lazyInit().defVal.get() }
508func (xd *extensionDesc) DefaultEnumValue() pref.EnumValueDescriptor { return xd.lazyInit().defVal.enum }
509func (xd *extensionDesc) OneofType() pref.OneofDescriptor { return nil }
510func (xd *extensionDesc) ExtendedType() pref.MessageDescriptor { return xd.extendedType }
511func (xd *extensionDesc) EnumType() pref.EnumDescriptor { return xd.lazyInit().enumType }
512func (xd *extensionDesc) MessageType() pref.MessageDescriptor { return xd.lazyInit().messageType }
513func (xd *extensionDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, xd) }
514func (xd *extensionDesc) ProtoType(pref.FieldDescriptor) {}
515func (xd *extensionDesc) ProtoInternal(pragma.DoNotImplement) {}
516func (xd *extensionDesc) lazyInit() *extensionLazy {
517 xd.parentFile.lazyInit() // implicitly initializes extensionLazy
518 return xd.lazy
519}
520
Joe Tsaiafb455e2019-03-14 16:08:22 -0700521// ProtoLegacyExtensionDesc is a pseudo-internal API for allowing the v1 code
522// to be able to retrieve a v1 ExtensionDesc.
Joe Tsaia5f43e82019-04-10 17:11:20 -0700523//
524// WARNING: This method is exempt from the compatibility promise and may be
525// removed in the future without warning.
526func (xd *extensionDesc) ProtoLegacyExtensionDesc() *piface.ExtensionDescV1 {
527 return xd.legacyDesc
528}
Joe Tsaiafb455e2019-03-14 16:08:22 -0700529
Damien Neil8012b442019-01-18 09:32:24 -0800530type (
531 serviceDesc struct {
532 baseDesc
533
534 lazy *serviceLazy // protected by fileDesc.once
535 }
536 serviceLazy struct {
537 methods methodDescs
538 options []byte
539 }
540 methodDesc struct {
541 baseDesc
542
543 inputType pref.MessageDescriptor
544 outputType pref.MessageDescriptor
545 isStreamingClient bool
546 isStreamingServer bool
547 options []byte
548 }
549)
550
Joe Tsaia2dd2282019-04-10 16:45:04 -0700551func (sd *serviceDesc) Options() pref.ProtoMessage {
552 return unmarshalOptions(descopts.Service, sd.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800553}
554func (sd *serviceDesc) Methods() pref.MethodDescriptors { return &sd.lazyInit().methods }
555func (sd *serviceDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, sd) }
556func (sd *serviceDesc) ProtoType(pref.ServiceDescriptor) {}
557func (sd *serviceDesc) ProtoInternal(pragma.DoNotImplement) {}
558func (sd *serviceDesc) lazyInit() *serviceLazy {
559 sd.parentFile.lazyInit() // implicitly initializes serviceLazy
560 return sd.lazy
561}
562
Joe Tsaia2dd2282019-04-10 16:45:04 -0700563func (md *methodDesc) Options() pref.ProtoMessage {
564 return unmarshalOptions(descopts.Method, md.options)
Damien Neil8012b442019-01-18 09:32:24 -0800565}
566func (md *methodDesc) InputType() pref.MessageDescriptor { return md.inputType }
567func (md *methodDesc) OutputType() pref.MessageDescriptor { return md.outputType }
568func (md *methodDesc) IsStreamingClient() bool { return md.isStreamingClient }
569func (md *methodDesc) IsStreamingServer() bool { return md.isStreamingServer }
570func (md *methodDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, md) }
571func (md *methodDesc) ProtoType(pref.MethodDescriptor) {}
572func (md *methodDesc) ProtoInternal(pragma.DoNotImplement) {}
573
574type baseDesc struct {
575 parentFile *fileDesc
576 parent pref.Descriptor
577 index int
578 fullName
579}
580
581func (d *baseDesc) Parent() (pref.Descriptor, bool) { return d.parent, true }
582func (d *baseDesc) Index() int { return d.index }
583func (d *baseDesc) Syntax() pref.Syntax { return d.parentFile.Syntax() }
584func (d *baseDesc) IsPlaceholder() bool { return false }
585func (d *baseDesc) ProtoInternal(pragma.DoNotImplement) {}
586
587type fullName struct {
588 shortLen int
589 fullName pref.FullName
590}
591
592func (s *fullName) Name() pref.Name { return pref.Name(s.fullName[len(s.fullName)-s.shortLen:]) }
593func (s *fullName) FullName() pref.FullName { return s.fullName }
594
Joe Tsaia2dd2282019-04-10 16:45:04 -0700595func unmarshalOptions(p pref.ProtoMessage, b []byte) pref.ProtoMessage {
Damien Neil8012b442019-01-18 09:32:24 -0800596 if b != nil {
597 // TODO: Consider caching the unmarshaled options message.
Joe Tsaia2dd2282019-04-10 16:45:04 -0700598 p = reflect.New(reflect.TypeOf(p).Elem()).Interface().(pref.ProtoMessage)
Damien Neil8012b442019-01-18 09:32:24 -0800599 if err := proto.Unmarshal(b, p.(proto.Message)); err != nil {
600 panic(err)
601 }
602 }
603 return p.(proto.Message)
604}