blob: 41d560e738f82e8279fbe230fcb4410f3dabb42b [file] [log] [blame]
Scott Baker105df152020-04-13 15:55:14 -07001// 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
5package protoreflect
6
7// Descriptor provides a set of accessors that are common to every descriptor.
8// Each descriptor type wraps the equivalent google.protobuf.XXXDescriptorProto,
9// but provides efficient lookup and immutability.
10//
11// Each descriptor is comparable. Equality implies that the two types are
12// exactly identical. However, it is possible for the same semantically
13// identical proto type to be represented by multiple type descriptors.
14//
15// For example, suppose we have t1 and t2 which are both MessageDescriptors.
16// If t1 == t2, then the types are definitely equal and all accessors return
17// the same information. However, if t1 != t2, then it is still possible that
18// they still represent the same proto type (e.g., t1.FullName == t2.FullName).
19// This can occur if a descriptor type is created dynamically, or multiple
20// versions of the same proto type are accidentally linked into the Go binary.
21type Descriptor interface {
22 // ParentFile returns the parent file descriptor that this descriptor
23 // is declared within. The parent file for the file descriptor is itself.
24 //
25 // Support for this functionality is optional and may return nil.
26 ParentFile() FileDescriptor
27
28 // Parent returns the parent containing this descriptor declaration.
29 // The following shows the mapping from child type to possible parent types:
30 //
31 // ╔═════════════════════╤═══════════════════════════════════╗
32 // ║ Child type │ Possible parent types ║
33 // ╠═════════════════════╪═══════════════════════════════════╣
34 // ║ FileDescriptor │ nil ║
35 // ║ MessageDescriptor │ FileDescriptor, MessageDescriptor ║
36 // ║ FieldDescriptor │ FileDescriptor, MessageDescriptor ║
37 // ║ OneofDescriptor │ MessageDescriptor ║
38 // ║ EnumDescriptor │ FileDescriptor, MessageDescriptor ║
39 // ║ EnumValueDescriptor │ EnumDescriptor ║
40 // ║ ServiceDescriptor │ FileDescriptor ║
41 // ║ MethodDescriptor │ ServiceDescriptor ║
42 // ╚═════════════════════╧═══════════════════════════════════╝
43 //
44 // Support for this functionality is optional and may return nil.
45 Parent() Descriptor
46
47 // Index returns the index of this descriptor within its parent.
48 // It returns 0 if the descriptor does not have a parent or if the parent
49 // is unknown.
50 Index() int
51
52 // Syntax is the protobuf syntax.
53 Syntax() Syntax // e.g., Proto2 or Proto3
54
55 // Name is the short name of the declaration (i.e., FullName.Name).
56 Name() Name // e.g., "Any"
57
58 // FullName is the fully-qualified name of the declaration.
59 //
60 // The FullName is a concatenation of the full name of the type that this
61 // type is declared within and the declaration name. For example,
62 // field "foo_field" in message "proto.package.MyMessage" is
63 // uniquely identified as "proto.package.MyMessage.foo_field".
64 // Enum values are an exception to the rule (see EnumValueDescriptor).
65 FullName() FullName // e.g., "google.protobuf.Any"
66
67 // IsPlaceholder reports whether type information is missing since a
68 // dependency is not resolved, in which case only name information is known.
69 //
70 // Placeholder types may only be returned by the following accessors
71 // as a result of unresolved dependencies or weak imports:
72 //
73 // ╔═══════════════════════════════════╤═════════════════════╗
74 // ║ Accessor │ Descriptor ║
75 // ╠═══════════════════════════════════╪═════════════════════╣
76 // ║ FileImports.FileDescriptor │ FileDescriptor ║
77 // ║ FieldDescriptor.Enum │ EnumDescriptor ║
78 // ║ FieldDescriptor.Message │ MessageDescriptor ║
79 // ║ FieldDescriptor.DefaultEnumValue │ EnumValueDescriptor ║
80 // ║ FieldDescriptor.ContainingMessage │ MessageDescriptor ║
81 // ║ MethodDescriptor.Input │ MessageDescriptor ║
82 // ║ MethodDescriptor.Output │ MessageDescriptor ║
83 // ╚═══════════════════════════════════╧═════════════════════╝
84 //
85 // If true, only Name and FullName are valid.
86 // For FileDescriptor, the Path is also valid.
87 IsPlaceholder() bool
88
89 // Options returns the descriptor options. The caller must not modify
90 // the returned value.
91 //
92 // To avoid a dependency cycle, this function returns a proto.Message value.
93 // The proto message type returned for each descriptor type is as follows:
94 // ╔═════════════════════╤══════════════════════════════════════════╗
95 // ║ Go type │ Protobuf message type ║
96 // ╠═════════════════════╪══════════════════════════════════════════╣
97 // ║ FileDescriptor │ google.protobuf.FileOptions ║
98 // ║ EnumDescriptor │ google.protobuf.EnumOptions ║
99 // ║ EnumValueDescriptor │ google.protobuf.EnumValueOptions ║
100 // ║ MessageDescriptor │ google.protobuf.MessageOptions ║
101 // ║ FieldDescriptor │ google.protobuf.FieldOptions ║
102 // ║ OneofDescriptor │ google.protobuf.OneofOptions ║
103 // ║ ServiceDescriptor │ google.protobuf.ServiceOptions ║
104 // ║ MethodDescriptor │ google.protobuf.MethodOptions ║
105 // ╚═════════════════════╧══════════════════════════════════════════╝
106 //
107 // This method returns a typed nil-pointer if no options are present.
108 // The caller must import the descriptorpb package to use this.
109 Options() ProtoMessage
110
111 doNotImplement
112}
113
114// FileDescriptor describes the types in a complete proto file and
115// corresponds with the google.protobuf.FileDescriptorProto message.
116//
117// Top-level declarations:
118// EnumDescriptor, MessageDescriptor, FieldDescriptor, and/or ServiceDescriptor.
119type FileDescriptor interface {
120 Descriptor // Descriptor.FullName is identical to Package
121
122 // Path returns the file name, relative to the source tree root.
123 Path() string // e.g., "path/to/file.proto"
124 // Package returns the protobuf package namespace.
125 Package() FullName // e.g., "google.protobuf"
126
127 // Imports is a list of imported proto files.
128 Imports() FileImports
129
130 // Enums is a list of the top-level enum declarations.
131 Enums() EnumDescriptors
132 // Messages is a list of the top-level message declarations.
133 Messages() MessageDescriptors
134 // Extensions is a list of the top-level extension declarations.
135 Extensions() ExtensionDescriptors
136 // Services is a list of the top-level service declarations.
137 Services() ServiceDescriptors
138
139 // SourceLocations is a list of source locations.
140 SourceLocations() SourceLocations
141
142 isFileDescriptor
143}
144type isFileDescriptor interface{ ProtoType(FileDescriptor) }
145
146// FileImports is a list of file imports.
147type FileImports interface {
148 // Len reports the number of files imported by this proto file.
149 Len() int
150 // Get returns the ith FileImport. It panics if out of bounds.
151 Get(i int) FileImport
152
153 doNotImplement
154}
155
156// FileImport is the declaration for a proto file import.
157type FileImport struct {
158 // FileDescriptor is the file type for the given import.
159 // It is a placeholder descriptor if IsWeak is set or if a dependency has
160 // not been regenerated to implement the new reflection APIs.
161 FileDescriptor
162
163 // IsPublic reports whether this is a public import, which causes this file
164 // to alias declarations within the imported file. The intended use cases
165 // for this feature is the ability to move proto files without breaking
166 // existing dependencies.
167 //
168 // The current file and the imported file must be within proto package.
169 IsPublic bool
170
171 // IsWeak reports whether this is a weak import, which does not impose
172 // a direct dependency on the target file.
173 //
174 // Weak imports are a legacy proto1 feature. Equivalent behavior is
175 // achieved using proto2 extension fields or proto3 Any messages.
176 IsWeak bool
177}
178
179// MessageDescriptor describes a message and
180// corresponds with the google.protobuf.DescriptorProto message.
181//
182// Nested declarations:
183// FieldDescriptor, OneofDescriptor, FieldDescriptor, EnumDescriptor,
184// and/or MessageDescriptor.
185type MessageDescriptor interface {
186 Descriptor
187
188 // IsMapEntry indicates that this is an auto-generated message type to
189 // represent the entry type for a map field.
190 //
191 // Map entry messages have only two fields:
192 // • a "key" field with a field number of 1
193 // • a "value" field with a field number of 2
194 // The key and value types are determined by these two fields.
195 //
196 // If IsMapEntry is true, it implies that FieldDescriptor.IsMap is true
197 // for some field with this message type.
198 IsMapEntry() bool
199
200 // Fields is a list of nested field declarations.
201 Fields() FieldDescriptors
202 // Oneofs is a list of nested oneof declarations.
203 Oneofs() OneofDescriptors
204
205 // ReservedNames is a list of reserved field names.
206 ReservedNames() Names
207 // ReservedRanges is a list of reserved ranges of field numbers.
208 ReservedRanges() FieldRanges
209 // RequiredNumbers is a list of required field numbers.
210 // In Proto3, it is always an empty list.
211 RequiredNumbers() FieldNumbers
212 // ExtensionRanges is the field ranges used for extension fields.
213 // In Proto3, it is always an empty ranges.
214 ExtensionRanges() FieldRanges
215 // ExtensionRangeOptions returns the ith extension range options.
216 //
217 // To avoid a dependency cycle, this method returns a proto.Message value,
218 // which always contains a google.protobuf.ExtensionRangeOptions message.
219 // This method returns a typed nil-pointer if no options are present.
220 // The caller must import the descriptorpb package to use this.
221 ExtensionRangeOptions(i int) ProtoMessage
222
223 // Enums is a list of nested enum declarations.
224 Enums() EnumDescriptors
225 // Messages is a list of nested message declarations.
226 Messages() MessageDescriptors
227 // Extensions is a list of nested extension declarations.
228 Extensions() ExtensionDescriptors
229
230 isMessageDescriptor
231}
232type isMessageDescriptor interface{ ProtoType(MessageDescriptor) }
233
234// MessageType encapsulates a MessageDescriptor with a concrete Go implementation.
235type MessageType interface {
236 // New returns a newly allocated empty message.
237 New() Message
238
239 // Zero returns an empty, read-only message.
240 Zero() Message
241
242 // Descriptor returns the message descriptor.
243 //
244 // Invariant: t.Descriptor() == t.New().Descriptor()
245 Descriptor() MessageDescriptor
246}
247
248// MessageDescriptors is a list of message declarations.
249type MessageDescriptors interface {
250 // Len reports the number of messages.
251 Len() int
252 // Get returns the ith MessageDescriptor. It panics if out of bounds.
253 Get(i int) MessageDescriptor
254 // ByName returns the MessageDescriptor for a message named s.
255 // It returns nil if not found.
256 ByName(s Name) MessageDescriptor
257
258 doNotImplement
259}
260
261// FieldDescriptor describes a field within a message and
262// corresponds with the google.protobuf.FieldDescriptorProto message.
263//
264// It is used for both normal fields defined within the parent message
265// (e.g., MessageDescriptor.Fields) and fields that extend some remote message
266// (e.g., FileDescriptor.Extensions or MessageDescriptor.Extensions).
267type FieldDescriptor interface {
268 Descriptor
269
270 // Number reports the unique number for this field.
271 Number() FieldNumber
272 // Cardinality reports the cardinality for this field.
273 Cardinality() Cardinality
274 // Kind reports the basic kind for this field.
275 Kind() Kind
276
277 // HasJSONName reports whether this field has an explicitly set JSON name.
278 HasJSONName() bool
279
280 // JSONName reports the name used for JSON serialization.
281 // It is usually the camel-cased form of the field name.
282 JSONName() string
283
284 // IsExtension reports whether this is an extension field. If false,
285 // then Parent and ContainingMessage refer to the same message.
286 // Otherwise, ContainingMessage and Parent likely differ.
287 IsExtension() bool
288
289 // IsWeak reports whether this is a weak field, which does not impose a
290 // direct dependency on the target type.
291 // If true, then Message returns a placeholder type.
292 IsWeak() bool
293
294 // IsPacked reports whether repeated primitive numeric kinds should be
295 // serialized using a packed encoding.
296 // If true, then it implies Cardinality is Repeated.
297 IsPacked() bool
298
299 // IsList reports whether this field represents a list,
300 // where the value type for the associated field is a List.
301 // It is equivalent to checking whether Cardinality is Repeated and
302 // that IsMap reports false.
303 IsList() bool
304
305 // IsMap reports whether this field represents a map,
306 // where the value type for the associated field is a Map.
307 // It is equivalent to checking whether Cardinality is Repeated,
308 // that the Kind is MessageKind, and that Message.IsMapEntry reports true.
309 IsMap() bool
310
311 // MapKey returns the field descriptor for the key in the map entry.
312 // It returns nil if IsMap reports false.
313 MapKey() FieldDescriptor
314
315 // MapValue returns the field descriptor for the value in the map entry.
316 // It returns nil if IsMap reports false.
317 MapValue() FieldDescriptor
318
319 // HasDefault reports whether this field has a default value.
320 HasDefault() bool
321
322 // Default returns the default value for scalar fields.
323 // For proto2, it is the default value as specified in the proto file,
324 // or the zero value if unspecified.
325 // For proto3, it is always the zero value of the scalar.
326 // The Value type is determined by the Kind.
327 Default() Value
328
329 // DefaultEnumValue returns the enum value descriptor for the default value
330 // of an enum field, and is nil for any other kind of field.
331 DefaultEnumValue() EnumValueDescriptor
332
333 // ContainingOneof is the containing oneof that this field belongs to,
334 // and is nil if this field is not part of a oneof.
335 ContainingOneof() OneofDescriptor
336
337 // ContainingMessage is the containing message that this field belongs to.
338 // For extension fields, this may not necessarily be the parent message
339 // that the field is declared within.
340 ContainingMessage() MessageDescriptor
341
342 // Enum is the enum descriptor if Kind is EnumKind.
343 // It returns nil for any other Kind.
344 Enum() EnumDescriptor
345
346 // Message is the message descriptor if Kind is
347 // MessageKind or GroupKind. It returns nil for any other Kind.
348 Message() MessageDescriptor
349
350 isFieldDescriptor
351}
352type isFieldDescriptor interface{ ProtoType(FieldDescriptor) }
353
354// FieldDescriptors is a list of field declarations.
355type FieldDescriptors interface {
356 // Len reports the number of fields.
357 Len() int
358 // Get returns the ith FieldDescriptor. It panics if out of bounds.
359 Get(i int) FieldDescriptor
360 // ByName returns the FieldDescriptor for a field named s.
361 // It returns nil if not found.
362 ByName(s Name) FieldDescriptor
363 // ByJSONName returns the FieldDescriptor for a field with s as the JSON name.
364 // It returns nil if not found.
365 ByJSONName(s string) FieldDescriptor
366 // ByNumber returns the FieldDescriptor for a field numbered n.
367 // It returns nil if not found.
368 ByNumber(n FieldNumber) FieldDescriptor
369
370 doNotImplement
371}
372
373// OneofDescriptor describes a oneof field set within a given message and
374// corresponds with the google.protobuf.OneofDescriptorProto message.
375type OneofDescriptor interface {
376 Descriptor
377
378 // Fields is a list of fields belonging to this oneof.
379 Fields() FieldDescriptors
380
381 isOneofDescriptor
382}
383type isOneofDescriptor interface{ ProtoType(OneofDescriptor) }
384
385// OneofDescriptors is a list of oneof declarations.
386type OneofDescriptors interface {
387 // Len reports the number of oneof fields.
388 Len() int
389 // Get returns the ith OneofDescriptor. It panics if out of bounds.
390 Get(i int) OneofDescriptor
391 // ByName returns the OneofDescriptor for a oneof named s.
392 // It returns nil if not found.
393 ByName(s Name) OneofDescriptor
394
395 doNotImplement
396}
397
398// ExtensionDescriptor is an alias of FieldDescriptor for documentation.
399type ExtensionDescriptor = FieldDescriptor
400
401// ExtensionTypeDescriptor is an ExtensionDescriptor with an associated ExtensionType.
402type ExtensionTypeDescriptor interface {
403 ExtensionDescriptor
404
405 // Type returns the associated ExtensionType.
406 Type() ExtensionType
407
408 // Descriptor returns the plain ExtensionDescriptor without the
409 // associated ExtensionType.
410 Descriptor() ExtensionDescriptor
411}
412
413// ExtensionDescriptors is a list of field declarations.
414type ExtensionDescriptors interface {
415 // Len reports the number of fields.
416 Len() int
417 // Get returns the ith ExtensionDescriptor. It panics if out of bounds.
418 Get(i int) ExtensionDescriptor
419 // ByName returns the ExtensionDescriptor for a field named s.
420 // It returns nil if not found.
421 ByName(s Name) ExtensionDescriptor
422
423 doNotImplement
424}
425
426// ExtensionType encapsulates an ExtensionDescriptor with a concrete
427// Go implementation. The nested field descriptor must be for a extension field.
428//
429// While a normal field is a member of the parent message that it is declared
430// within (see Descriptor.Parent), an extension field is a member of some other
431// target message (see ExtensionDescriptor.Extendee) and may have no
432// relationship with the parent. However, the full name of an extension field is
433// relative to the parent that it is declared within.
434//
435// For example:
436// syntax = "proto2";
437// package example;
438// message FooMessage {
439// extensions 100 to max;
440// }
441// message BarMessage {
442// extends FooMessage { optional BarMessage bar_field = 100; }
443// }
444//
445// Field "bar_field" is an extension of FooMessage, but its full name is
446// "example.BarMessage.bar_field" instead of "example.FooMessage.bar_field".
447type ExtensionType interface {
448 // New returns a new value for the field.
449 // For scalars, this returns the default value in native Go form.
450 New() Value
451
452 // Zero returns a new value for the field.
453 // For scalars, this returns the default value in native Go form.
454 // For composite types, this returns an empty, read-only message, list, or map.
455 Zero() Value
456
457 // TypeDescriptor returns the extension type descriptor.
458 TypeDescriptor() ExtensionTypeDescriptor
459
460 // ValueOf wraps the input and returns it as a Value.
461 // ValueOf panics if the input value is invalid or not the appropriate type.
462 //
463 // ValueOf is more extensive than protoreflect.ValueOf for a given field's
464 // value as it has more type information available.
465 ValueOf(interface{}) Value
466
467 // InterfaceOf completely unwraps the Value to the underlying Go type.
468 // InterfaceOf panics if the input is nil or does not represent the
469 // appropriate underlying Go type. For composite types, it panics if the
470 // value is not mutable.
471 //
472 // InterfaceOf is able to unwrap the Value further than Value.Interface
473 // as it has more type information available.
474 InterfaceOf(Value) interface{}
475
476 // IsValidValue reports whether the Value is valid to assign to the field.
477 IsValidValue(Value) bool
478
479 // IsValidInterface reports whether the input is valid to assign to the field.
480 IsValidInterface(interface{}) bool
481}
482
483// EnumDescriptor describes an enum and
484// corresponds with the google.protobuf.EnumDescriptorProto message.
485//
486// Nested declarations:
487// EnumValueDescriptor.
488type EnumDescriptor interface {
489 Descriptor
490
491 // Values is a list of nested enum value declarations.
492 Values() EnumValueDescriptors
493
494 // ReservedNames is a list of reserved enum names.
495 ReservedNames() Names
496 // ReservedRanges is a list of reserved ranges of enum numbers.
497 ReservedRanges() EnumRanges
498
499 isEnumDescriptor
500}
501type isEnumDescriptor interface{ ProtoType(EnumDescriptor) }
502
503// EnumType encapsulates an EnumDescriptor with a concrete Go implementation.
504type EnumType interface {
505 // New returns an instance of this enum type with its value set to n.
506 New(n EnumNumber) Enum
507
508 // Descriptor returns the enum descriptor.
509 //
510 // Invariant: t.Descriptor() == t.New(0).Descriptor()
511 Descriptor() EnumDescriptor
512}
513
514// EnumDescriptors is a list of enum declarations.
515type EnumDescriptors interface {
516 // Len reports the number of enum types.
517 Len() int
518 // Get returns the ith EnumDescriptor. It panics if out of bounds.
519 Get(i int) EnumDescriptor
520 // ByName returns the EnumDescriptor for an enum named s.
521 // It returns nil if not found.
522 ByName(s Name) EnumDescriptor
523
524 doNotImplement
525}
526
527// EnumValueDescriptor describes an enum value and
528// corresponds with the google.protobuf.EnumValueDescriptorProto message.
529//
530// All other proto declarations are in the namespace of the parent.
531// However, enum values do not follow this rule and are within the namespace
532// of the parent's parent (i.e., they are a sibling of the containing enum).
533// Thus, a value named "FOO_VALUE" declared within an enum uniquely identified
534// as "proto.package.MyEnum" has a full name of "proto.package.FOO_VALUE".
535type EnumValueDescriptor interface {
536 Descriptor
537
538 // Number returns the enum value as an integer.
539 Number() EnumNumber
540
541 isEnumValueDescriptor
542}
543type isEnumValueDescriptor interface{ ProtoType(EnumValueDescriptor) }
544
545// EnumValueDescriptors is a list of enum value declarations.
546type EnumValueDescriptors interface {
547 // Len reports the number of enum values.
548 Len() int
549 // Get returns the ith EnumValueDescriptor. It panics if out of bounds.
550 Get(i int) EnumValueDescriptor
551 // ByName returns the EnumValueDescriptor for the enum value named s.
552 // It returns nil if not found.
553 ByName(s Name) EnumValueDescriptor
554 // ByNumber returns the EnumValueDescriptor for the enum value numbered n.
555 // If multiple have the same number, the first one defined is returned
556 // It returns nil if not found.
557 ByNumber(n EnumNumber) EnumValueDescriptor
558
559 doNotImplement
560}
561
562// ServiceDescriptor describes a service and
563// corresponds with the google.protobuf.ServiceDescriptorProto message.
564//
565// Nested declarations: MethodDescriptor.
566type ServiceDescriptor interface {
567 Descriptor
568
569 // Methods is a list of nested message declarations.
570 Methods() MethodDescriptors
571
572 isServiceDescriptor
573}
574type isServiceDescriptor interface{ ProtoType(ServiceDescriptor) }
575
576// ServiceDescriptors is a list of service declarations.
577type ServiceDescriptors interface {
578 // Len reports the number of services.
579 Len() int
580 // Get returns the ith ServiceDescriptor. It panics if out of bounds.
581 Get(i int) ServiceDescriptor
582 // ByName returns the ServiceDescriptor for a service named s.
583 // It returns nil if not found.
584 ByName(s Name) ServiceDescriptor
585
586 doNotImplement
587}
588
589// MethodDescriptor describes a method and
590// corresponds with the google.protobuf.MethodDescriptorProto message.
591type MethodDescriptor interface {
592 Descriptor
593
594 // Input is the input message descriptor.
595 Input() MessageDescriptor
596 // Output is the output message descriptor.
597 Output() MessageDescriptor
598 // IsStreamingClient reports whether the client streams multiple messages.
599 IsStreamingClient() bool
600 // IsStreamingServer reports whether the server streams multiple messages.
601 IsStreamingServer() bool
602
603 isMethodDescriptor
604}
605type isMethodDescriptor interface{ ProtoType(MethodDescriptor) }
606
607// MethodDescriptors is a list of method declarations.
608type MethodDescriptors interface {
609 // Len reports the number of methods.
610 Len() int
611 // Get returns the ith MethodDescriptor. It panics if out of bounds.
612 Get(i int) MethodDescriptor
613 // ByName returns the MethodDescriptor for a service method named s.
614 // It returns nil if not found.
615 ByName(s Name) MethodDescriptor
616
617 doNotImplement
618}