blob: 0e9ef53016ae9f6ae861a12c373cbbce4480603d [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
Joe Tsai0fc49f82019-05-01 12:29:25 -0700305func (ed *enumDesc) Descriptor() pref.EnumDescriptor { return ed }
Damien Neil8012b442019-01-18 09:32:24 -0800306func (ed *enumDesc) GoType() reflect.Type { return ed.lazyInit().typ }
307func (ed *enumDesc) New(n pref.EnumNumber) pref.Enum { return ed.lazyInit().new(n) }
Joe Tsaia2dd2282019-04-10 16:45:04 -0700308func (ed *enumDesc) Options() pref.ProtoMessage {
309 return unmarshalOptions(descopts.Enum, ed.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800310}
311func (ed *enumDesc) Values() pref.EnumValueDescriptors { return &ed.lazyInit().values }
312func (ed *enumDesc) ReservedNames() pref.Names { return &ed.lazyInit().resvNames }
313func (ed *enumDesc) ReservedRanges() pref.EnumRanges { return &ed.lazyInit().resvRanges }
314func (ed *enumDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, ed) }
315func (ed *enumDesc) ProtoType(pref.EnumDescriptor) {}
316func (ed *enumDesc) lazyInit() *enumLazy {
317 ed.parentFile.lazyInit() // implicitly initializes enumLazy
318 return ed.lazy
319}
320
Joe Tsaia2dd2282019-04-10 16:45:04 -0700321func (ed *enumValueDesc) Options() pref.ProtoMessage {
322 return unmarshalOptions(descopts.EnumValue, ed.options)
Damien Neil8012b442019-01-18 09:32:24 -0800323}
324func (ed *enumValueDesc) Number() pref.EnumNumber { return ed.number }
325func (ed *enumValueDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, ed) }
326func (ed *enumValueDesc) ProtoType(pref.EnumValueDescriptor) {}
327
328type (
Joe Tsai4532dd72019-03-19 17:04:06 -0700329 messageType struct{ *messageDesc }
330 messageDescriptor struct{ *messageDesc }
331
332 // messageDesc does not implement protoreflect.Descriptor to avoid
333 // accidental usages of it as such. Use the asDesc method to retrieve one.
Damien Neil8012b442019-01-18 09:32:24 -0800334 messageDesc struct {
335 baseDesc
336
337 enums enumDescs
338 messages messageDescs
339 extensions extensionDescs
340
Joe Tsai4532dd72019-03-19 17:04:06 -0700341 isMapEntry bool
342 lazy *messageLazy // protected by fileDesc.once
Damien Neil8012b442019-01-18 09:32:24 -0800343 }
344 messageLazy struct {
345 typ reflect.Type
346 new func() pref.Message
347
Joe Tsai1321a0e2019-03-20 09:46:22 -0700348 isMessageSet bool
Damien Neil8012b442019-01-18 09:32:24 -0800349 fields fieldDescs
350 oneofs oneofDescs
351 resvNames names
352 resvRanges fieldRanges
353 reqNumbers fieldNumbers
354 extRanges fieldRanges
355 extRangeOptions [][]byte
356 options []byte
357 }
358 fieldDesc struct {
359 baseDesc
360
361 number pref.FieldNumber
362 cardinality pref.Cardinality
363 kind pref.Kind
364 hasJSONName bool
365 jsonName string
366 hasPacked bool
367 isPacked bool
368 isWeak bool
369 isMap bool
370 defVal defaultValue
371 oneofType pref.OneofDescriptor
372 enumType pref.EnumDescriptor
373 messageType pref.MessageDescriptor
374 options []byte
375 }
376 oneofDesc struct {
377 baseDesc
378
379 fields oneofFields
380 options []byte
381 }
382)
383
Joe Tsaia2dd2282019-04-10 16:45:04 -0700384func (md *messageDesc) options() pref.ProtoMessage {
385 return unmarshalOptions(descopts.Message, md.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800386}
Joe Tsai4532dd72019-03-19 17:04:06 -0700387func (md *messageDesc) IsMapEntry() bool { return md.isMapEntry }
Damien Neil8012b442019-01-18 09:32:24 -0800388func (md *messageDesc) Fields() pref.FieldDescriptors { return &md.lazyInit().fields }
389func (md *messageDesc) Oneofs() pref.OneofDescriptors { return &md.lazyInit().oneofs }
390func (md *messageDesc) ReservedNames() pref.Names { return &md.lazyInit().resvNames }
391func (md *messageDesc) ReservedRanges() pref.FieldRanges { return &md.lazyInit().resvRanges }
392func (md *messageDesc) RequiredNumbers() pref.FieldNumbers { return &md.lazyInit().reqNumbers }
393func (md *messageDesc) ExtensionRanges() pref.FieldRanges { return &md.lazyInit().extRanges }
Joe Tsaia2dd2282019-04-10 16:45:04 -0700394func (md *messageDesc) ExtensionRangeOptions(i int) pref.ProtoMessage {
395 return unmarshalOptions(descopts.ExtensionRange, md.lazyInit().extRangeOptions[i])
Damien Neil8012b442019-01-18 09:32:24 -0800396}
397func (md *messageDesc) Enums() pref.EnumDescriptors { return &md.enums }
398func (md *messageDesc) Messages() pref.MessageDescriptors { return &md.messages }
399func (md *messageDesc) Extensions() pref.ExtensionDescriptors { return &md.extensions }
Damien Neil8012b442019-01-18 09:32:24 -0800400func (md *messageDesc) ProtoType(pref.MessageDescriptor) {}
Joe Tsai4532dd72019-03-19 17:04:06 -0700401func (md *messageDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, md.asDesc()) }
Damien Neil8012b442019-01-18 09:32:24 -0800402func (md *messageDesc) lazyInit() *messageLazy {
403 md.parentFile.lazyInit() // implicitly initializes messageLazy
404 return md.lazy
405}
406
Joe Tsai1321a0e2019-03-20 09:46:22 -0700407// IsMessageSet is a pseudo-internal API for checking whether a message
408// should serialize in the proto1 message format.
Joe Tsaia5f43e82019-04-10 17:11:20 -0700409//
410// WARNING: This method is exempt from the compatibility promise and may be
411// removed in the future without warning.
Joe Tsai1321a0e2019-03-20 09:46:22 -0700412func (md *messageDesc) IsMessageSet() bool {
413 return md.lazyInit().isMessageSet
414}
415
Joe Tsai4532dd72019-03-19 17:04:06 -0700416// asDesc returns a protoreflect.MessageDescriptor or protoreflect.MessageType
417// depending on whether the message is a map entry or not.
418func (mb *messageDesc) asDesc() pref.MessageDescriptor {
419 if !mb.isMapEntry {
420 return messageType{mb}
421 }
422 return messageDescriptor{mb}
423}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700424func (mt messageType) Descriptor() pref.MessageDescriptor { return messageDescriptor{mt.messageDesc} }
425func (mt messageType) GoType() reflect.Type { return mt.lazyInit().typ }
426func (mt messageType) New() pref.Message { return mt.lazyInit().new() }
427func (mt messageType) Options() pref.ProtoMessage { return mt.options() }
428func (md messageDescriptor) Options() pref.ProtoMessage { return md.options() }
Joe Tsai4532dd72019-03-19 17:04:06 -0700429
Joe Tsaia2dd2282019-04-10 16:45:04 -0700430func (fd *fieldDesc) Options() pref.ProtoMessage {
431 return unmarshalOptions(descopts.Field, fd.options)
Damien Neil8012b442019-01-18 09:32:24 -0800432}
433func (fd *fieldDesc) Number() pref.FieldNumber { return fd.number }
434func (fd *fieldDesc) Cardinality() pref.Cardinality { return fd.cardinality }
435func (fd *fieldDesc) Kind() pref.Kind { return fd.kind }
436func (fd *fieldDesc) HasJSONName() bool { return fd.hasJSONName }
437func (fd *fieldDesc) JSONName() string { return fd.jsonName }
438func (fd *fieldDesc) IsPacked() bool { return fd.isPacked }
439func (fd *fieldDesc) IsWeak() bool { return fd.isWeak }
440func (fd *fieldDesc) IsMap() bool { return fd.isMap }
441func (fd *fieldDesc) HasDefault() bool { return fd.defVal.has }
442func (fd *fieldDesc) Default() pref.Value { return fd.defVal.get() }
443func (fd *fieldDesc) DefaultEnumValue() pref.EnumValueDescriptor { return fd.defVal.enum }
Joe Tsaid24bc722019-04-15 23:39:09 -0700444func (fd *fieldDesc) Oneof() pref.OneofDescriptor { return fd.oneofType }
445func (fd *fieldDesc) Extendee() pref.MessageDescriptor { return nil }
446func (fd *fieldDesc) Enum() pref.EnumDescriptor { return fd.enumType }
447func (fd *fieldDesc) Message() pref.MessageDescriptor { return fd.messageType }
Damien Neil8012b442019-01-18 09:32:24 -0800448func (fd *fieldDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, fd) }
449func (fd *fieldDesc) ProtoType(pref.FieldDescriptor) {}
450
Joe Tsaia2dd2282019-04-10 16:45:04 -0700451func (od *oneofDesc) Options() pref.ProtoMessage {
452 return unmarshalOptions(descopts.Oneof, od.options)
Damien Neil8012b442019-01-18 09:32:24 -0800453}
454func (od *oneofDesc) Fields() pref.FieldDescriptors { return &od.fields }
455func (od *oneofDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, od) }
456func (od *oneofDesc) ProtoType(pref.OneofDescriptor) {}
457
458type (
459 extensionDesc struct {
460 baseDesc
461
462 number pref.FieldNumber
463 extendedType pref.MessageDescriptor
464
Joe Tsai4fddeba2019-03-20 18:29:32 -0700465 legacyDesc *piface.ExtensionDescV1
Joe Tsaiafb455e2019-03-14 16:08:22 -0700466
Damien Neil8012b442019-01-18 09:32:24 -0800467 lazy *extensionLazy // protected by fileDesc.once
468 }
469 extensionLazy struct {
470 typ reflect.Type
471 new func() pref.Value
472 valueOf func(interface{}) pref.Value
473 interfaceOf func(pref.Value) interface{}
474
475 cardinality pref.Cardinality
476 kind pref.Kind
477 // Extensions should not have JSON names, but older versions of protoc
478 // used to set one on the descriptor. Preserve it for now to maintain
479 // the property that protoc 3.6.1 descriptors can round-trip through
480 // this package losslessly.
481 //
482 // TODO: Consider whether to drop JSONName parsing from extensions.
483 hasJSONName bool
484 jsonName string
485 isPacked bool
486 defVal defaultValue
Joe Tsai0fc49f82019-05-01 12:29:25 -0700487 enumType pref.EnumDescriptor
488 messageType pref.MessageDescriptor
Damien Neil8012b442019-01-18 09:32:24 -0800489 options []byte
490 }
491)
492
Joe Tsai0fc49f82019-05-01 12:29:25 -0700493func (xd *extensionDesc) Descriptor() pref.ExtensionDescriptor { return xd }
Damien Neil8012b442019-01-18 09:32:24 -0800494func (xd *extensionDesc) GoType() reflect.Type { return xd.lazyInit().typ }
495func (xd *extensionDesc) New() pref.Value { return xd.lazyInit().new() }
496func (xd *extensionDesc) ValueOf(v interface{}) pref.Value { return xd.lazyInit().valueOf(v) }
497func (xd *extensionDesc) InterfaceOf(v pref.Value) interface{} { return xd.lazyInit().interfaceOf(v) }
Joe Tsaia2dd2282019-04-10 16:45:04 -0700498func (xd *extensionDesc) Options() pref.ProtoMessage {
499 return unmarshalOptions(descopts.Field, xd.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800500}
501func (xd *extensionDesc) Number() pref.FieldNumber { return xd.number }
502func (xd *extensionDesc) Cardinality() pref.Cardinality { return xd.lazyInit().cardinality }
503func (xd *extensionDesc) Kind() pref.Kind { return xd.lazyInit().kind }
504func (xd *extensionDesc) HasJSONName() bool { return xd.lazyInit().hasJSONName }
505func (xd *extensionDesc) JSONName() string { return xd.lazyInit().jsonName }
506func (xd *extensionDesc) IsPacked() bool { return xd.lazyInit().isPacked }
507func (xd *extensionDesc) IsWeak() bool { return false }
508func (xd *extensionDesc) IsMap() bool { return false }
509func (xd *extensionDesc) HasDefault() bool { return xd.lazyInit().defVal.has }
510func (xd *extensionDesc) Default() pref.Value { return xd.lazyInit().defVal.get() }
511func (xd *extensionDesc) DefaultEnumValue() pref.EnumValueDescriptor { return xd.lazyInit().defVal.enum }
Joe Tsaid24bc722019-04-15 23:39:09 -0700512func (xd *extensionDesc) Oneof() pref.OneofDescriptor { return nil }
513func (xd *extensionDesc) Extendee() pref.MessageDescriptor { return xd.extendedType }
514func (xd *extensionDesc) Enum() pref.EnumDescriptor { return xd.lazyInit().enumType }
515func (xd *extensionDesc) Message() pref.MessageDescriptor { return xd.lazyInit().messageType }
Damien Neil8012b442019-01-18 09:32:24 -0800516func (xd *extensionDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, xd) }
517func (xd *extensionDesc) ProtoType(pref.FieldDescriptor) {}
518func (xd *extensionDesc) ProtoInternal(pragma.DoNotImplement) {}
519func (xd *extensionDesc) lazyInit() *extensionLazy {
520 xd.parentFile.lazyInit() // implicitly initializes extensionLazy
521 return xd.lazy
522}
523
Joe Tsaiafb455e2019-03-14 16:08:22 -0700524// ProtoLegacyExtensionDesc is a pseudo-internal API for allowing the v1 code
525// to be able to retrieve a v1 ExtensionDesc.
Joe Tsaia5f43e82019-04-10 17:11:20 -0700526//
527// WARNING: This method is exempt from the compatibility promise and may be
528// removed in the future without warning.
529func (xd *extensionDesc) ProtoLegacyExtensionDesc() *piface.ExtensionDescV1 {
530 return xd.legacyDesc
531}
Joe Tsaiafb455e2019-03-14 16:08:22 -0700532
Damien Neil8012b442019-01-18 09:32:24 -0800533type (
534 serviceDesc struct {
535 baseDesc
536
537 lazy *serviceLazy // protected by fileDesc.once
538 }
539 serviceLazy struct {
540 methods methodDescs
541 options []byte
542 }
543 methodDesc struct {
544 baseDesc
545
546 inputType pref.MessageDescriptor
547 outputType pref.MessageDescriptor
548 isStreamingClient bool
549 isStreamingServer bool
550 options []byte
551 }
552)
553
Joe Tsaia2dd2282019-04-10 16:45:04 -0700554func (sd *serviceDesc) Options() pref.ProtoMessage {
555 return unmarshalOptions(descopts.Service, sd.lazyInit().options)
Damien Neil8012b442019-01-18 09:32:24 -0800556}
557func (sd *serviceDesc) Methods() pref.MethodDescriptors { return &sd.lazyInit().methods }
558func (sd *serviceDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, sd) }
559func (sd *serviceDesc) ProtoType(pref.ServiceDescriptor) {}
560func (sd *serviceDesc) ProtoInternal(pragma.DoNotImplement) {}
561func (sd *serviceDesc) lazyInit() *serviceLazy {
562 sd.parentFile.lazyInit() // implicitly initializes serviceLazy
563 return sd.lazy
564}
565
Joe Tsaia2dd2282019-04-10 16:45:04 -0700566func (md *methodDesc) Options() pref.ProtoMessage {
567 return unmarshalOptions(descopts.Method, md.options)
Damien Neil8012b442019-01-18 09:32:24 -0800568}
Joe Tsaid24bc722019-04-15 23:39:09 -0700569func (md *methodDesc) Input() pref.MessageDescriptor { return md.inputType }
570func (md *methodDesc) Output() pref.MessageDescriptor { return md.outputType }
Damien Neil8012b442019-01-18 09:32:24 -0800571func (md *methodDesc) IsStreamingClient() bool { return md.isStreamingClient }
572func (md *methodDesc) IsStreamingServer() bool { return md.isStreamingServer }
573func (md *methodDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, md) }
574func (md *methodDesc) ProtoType(pref.MethodDescriptor) {}
575func (md *methodDesc) ProtoInternal(pragma.DoNotImplement) {}
576
577type baseDesc struct {
578 parentFile *fileDesc
579 parent pref.Descriptor
580 index int
581 fullName
582}
583
584func (d *baseDesc) Parent() (pref.Descriptor, bool) { return d.parent, true }
585func (d *baseDesc) Index() int { return d.index }
586func (d *baseDesc) Syntax() pref.Syntax { return d.parentFile.Syntax() }
587func (d *baseDesc) IsPlaceholder() bool { return false }
588func (d *baseDesc) ProtoInternal(pragma.DoNotImplement) {}
589
590type fullName struct {
591 shortLen int
592 fullName pref.FullName
593}
594
595func (s *fullName) Name() pref.Name { return pref.Name(s.fullName[len(s.fullName)-s.shortLen:]) }
596func (s *fullName) FullName() pref.FullName { return s.fullName }
597
Joe Tsaia2dd2282019-04-10 16:45:04 -0700598func unmarshalOptions(p pref.ProtoMessage, b []byte) pref.ProtoMessage {
Damien Neil8012b442019-01-18 09:32:24 -0800599 if b != nil {
600 // TODO: Consider caching the unmarshaled options message.
Joe Tsaia2dd2282019-04-10 16:45:04 -0700601 p = reflect.New(reflect.TypeOf(p).Elem()).Interface().(pref.ProtoMessage)
Damien Neil8012b442019-01-18 09:32:24 -0800602 if err := proto.Unmarshal(b, p.(proto.Message)); err != nil {
603 panic(err)
604 }
605 }
606 return p.(proto.Message)
607}