1
// Copyright 2019 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
13
"google.golang.org/protobuf/internal/descfmt"
14
"google.golang.org/protobuf/internal/descopts"
15
"google.golang.org/protobuf/internal/encoding/defval"
16
"google.golang.org/protobuf/internal/encoding/messageset"
17
"google.golang.org/protobuf/internal/genid"
18
"google.golang.org/protobuf/internal/pragma"
19
"google.golang.org/protobuf/internal/strs"
20
"google.golang.org/protobuf/reflect/protoreflect"
21
"google.golang.org/protobuf/reflect/protoregistry"
24
// Edition is an Enum for proto2.Edition
27
// These values align with the value of Enum in descriptor.proto which allows
28
// direct conversion between the proto enum and this enum.
30
EditionUnknown Edition = 0
31
EditionProto2 Edition = 998
32
EditionProto3 Edition = 999
33
Edition2023 Edition = 1000
34
EditionUnsupported Edition = 100000
37
// The types in this file may have a suffix:
38
// • L0: Contains fields common to all descriptors (except File) and
39
// must be initialized up front.
40
// • L1: Contains fields specific to a descriptor and
41
// must be initialized up front. If the associated proto uses Editions, the
42
// Editions features must always be resolved. If not explicitly set, the
43
// appropriate default must be resolved and set.
44
// • L2: Contains fields that are lazily initialized when constructing
45
// from the raw file descriptor. When constructing as a literal, the L2
46
// fields must be initialized up front.
48
// The types are exported so that packages like reflect/protodesc can
49
// directly construct descriptors.
56
once uint32 // atomically set if L2 is valid
57
mu sync.Mutex // protects L2
61
Syntax protoreflect.Syntax
62
Edition Edition // Only used if Syntax == Editions
64
Package protoreflect.FullName
71
EditionFeatures EditionFeatures
74
Options func() protoreflect.ProtoMessage
76
Locations SourceLocations
79
EditionFeatures struct {
80
// IsFieldPresence is true if field_presence is EXPLICIT
81
// https://protobuf.dev/editions/features/#field_presence
83
// IsFieldPresence is true if field_presence is LEGACY_REQUIRED
84
// https://protobuf.dev/editions/features/#field_presence
86
// IsOpenEnum is true if enum_type is OPEN
87
// https://protobuf.dev/editions/features/#enum_type
89
// IsPacked is true if repeated_field_encoding is PACKED
90
// https://protobuf.dev/editions/features/#repeated_field_encoding
92
// IsUTF8Validated is true if utf_validation is VERIFY
93
// https://protobuf.dev/editions/features/#utf8_validation
95
// IsDelimitedEncoded is true if message_encoding is DELIMITED
96
// https://protobuf.dev/editions/features/#message_encoding
97
IsDelimitedEncoded bool
98
// IsJSONCompliant is true if json_format is ALLOW
99
// https://protobuf.dev/editions/features/#json_format
101
// GenerateLegacyUnmarshalJSON determines if the plugin generates the
102
// UnmarshalJSON([]byte) error method for enums.
103
GenerateLegacyUnmarshalJSON bool
107
func (fd *File) ParentFile() protoreflect.FileDescriptor { return fd }
108
func (fd *File) Parent() protoreflect.Descriptor { return nil }
109
func (fd *File) Index() int { return 0 }
110
func (fd *File) Syntax() protoreflect.Syntax { return fd.L1.Syntax }
111
func (fd *File) Name() protoreflect.Name { return fd.L1.Package.Name() }
112
func (fd *File) FullName() protoreflect.FullName { return fd.L1.Package }
113
func (fd *File) IsPlaceholder() bool { return false }
114
func (fd *File) Options() protoreflect.ProtoMessage {
115
if f := fd.lazyInit().Options; f != nil {
120
func (fd *File) Path() string { return fd.L1.Path }
121
func (fd *File) Package() protoreflect.FullName { return fd.L1.Package }
122
func (fd *File) Imports() protoreflect.FileImports { return &fd.lazyInit().Imports }
123
func (fd *File) Enums() protoreflect.EnumDescriptors { return &fd.L1.Enums }
124
func (fd *File) Messages() protoreflect.MessageDescriptors { return &fd.L1.Messages }
125
func (fd *File) Extensions() protoreflect.ExtensionDescriptors { return &fd.L1.Extensions }
126
func (fd *File) Services() protoreflect.ServiceDescriptors { return &fd.L1.Services }
127
func (fd *File) SourceLocations() protoreflect.SourceLocations { return &fd.lazyInit().Locations }
128
func (fd *File) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, fd) }
129
func (fd *File) ProtoType(protoreflect.FileDescriptor) {}
130
func (fd *File) ProtoInternal(pragma.DoNotImplement) {}
132
func (fd *File) lazyInit() *FileL2 {
133
if atomic.LoadUint32(&fd.once) == 0 {
139
func (fd *File) lazyInitOnce() {
142
fd.lazyRawInit() // recursively initializes all L2 structures
144
atomic.StoreUint32(&fd.once, 1)
148
// GoPackagePath is a pseudo-internal API for determining the Go package path
149
// that this file descriptor is declared in.
151
// WARNING: This method is exempt from the compatibility promise and may be
152
// removed in the future without warning.
153
func (fd *File) GoPackagePath() string {
154
return fd.builder.GoPackagePath
161
L2 *EnumL2 // protected by fileDesc.once
164
eagerValues bool // controls whether EnumL2.Values is already populated
166
EditionFeatures EditionFeatures
169
Options func() protoreflect.ProtoMessage
172
ReservedRanges EnumRanges
180
Options func() protoreflect.ProtoMessage
181
Number protoreflect.EnumNumber
185
func (ed *Enum) Options() protoreflect.ProtoMessage {
186
if f := ed.lazyInit().Options; f != nil {
191
func (ed *Enum) Values() protoreflect.EnumValueDescriptors {
192
if ed.L1.eagerValues {
195
return &ed.lazyInit().Values
197
func (ed *Enum) ReservedNames() protoreflect.Names { return &ed.lazyInit().ReservedNames }
198
func (ed *Enum) ReservedRanges() protoreflect.EnumRanges { return &ed.lazyInit().ReservedRanges }
199
func (ed *Enum) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, ed) }
200
func (ed *Enum) ProtoType(protoreflect.EnumDescriptor) {}
201
func (ed *Enum) lazyInit() *EnumL2 {
202
ed.L0.ParentFile.lazyInit() // implicitly initializes L2
206
func (ed *EnumValue) Options() protoreflect.ProtoMessage {
207
if f := ed.L1.Options; f != nil {
210
return descopts.EnumValue
212
func (ed *EnumValue) Number() protoreflect.EnumNumber { return ed.L1.Number }
213
func (ed *EnumValue) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, ed) }
214
func (ed *EnumValue) ProtoType(protoreflect.EnumValueDescriptor) {}
220
L2 *MessageL2 // protected by fileDesc.once
225
Extensions Extensions
226
IsMapEntry bool // promoted from google.protobuf.MessageOptions
227
IsMessageSet bool // promoted from google.protobuf.MessageOptions
229
EditionFeatures EditionFeatures
232
Options func() protoreflect.ProtoMessage
236
ReservedRanges FieldRanges
237
RequiredNumbers FieldNumbers // must be consistent with Fields.Cardinality
238
ExtensionRanges FieldRanges
239
ExtensionRangeOptions []func() protoreflect.ProtoMessage // must be same length as ExtensionRanges
247
Options func() protoreflect.ProtoMessage
248
Number protoreflect.FieldNumber
249
Cardinality protoreflect.Cardinality // must be consistent with Message.RequiredNumbers
250
Kind protoreflect.Kind
251
StringName stringName
252
IsProto3Optional bool // promoted from google.protobuf.FieldDescriptorProto
253
IsWeak bool // promoted from google.protobuf.FieldOptions
254
HasPacked bool // promoted from google.protobuf.FieldOptions
255
IsPacked bool // promoted from google.protobuf.FieldOptions
256
HasEnforceUTF8 bool // promoted from google.protobuf.FieldOptions
257
EnforceUTF8 bool // promoted from google.protobuf.FieldOptions
259
ContainingOneof protoreflect.OneofDescriptor // must be consistent with Message.Oneofs.Fields
260
Enum protoreflect.EnumDescriptor
261
Message protoreflect.MessageDescriptor
263
EditionFeatures EditionFeatures
271
Options func() protoreflect.ProtoMessage
272
Fields OneofFields // must be consistent with Message.Fields.ContainingOneof
274
EditionFeatures EditionFeatures
278
func (md *Message) Options() protoreflect.ProtoMessage {
279
if f := md.lazyInit().Options; f != nil {
282
return descopts.Message
284
func (md *Message) IsMapEntry() bool { return md.L1.IsMapEntry }
285
func (md *Message) Fields() protoreflect.FieldDescriptors { return &md.lazyInit().Fields }
286
func (md *Message) Oneofs() protoreflect.OneofDescriptors { return &md.lazyInit().Oneofs }
287
func (md *Message) ReservedNames() protoreflect.Names { return &md.lazyInit().ReservedNames }
288
func (md *Message) ReservedRanges() protoreflect.FieldRanges { return &md.lazyInit().ReservedRanges }
289
func (md *Message) RequiredNumbers() protoreflect.FieldNumbers { return &md.lazyInit().RequiredNumbers }
290
func (md *Message) ExtensionRanges() protoreflect.FieldRanges { return &md.lazyInit().ExtensionRanges }
291
func (md *Message) ExtensionRangeOptions(i int) protoreflect.ProtoMessage {
292
if f := md.lazyInit().ExtensionRangeOptions[i]; f != nil {
295
return descopts.ExtensionRange
297
func (md *Message) Enums() protoreflect.EnumDescriptors { return &md.L1.Enums }
298
func (md *Message) Messages() protoreflect.MessageDescriptors { return &md.L1.Messages }
299
func (md *Message) Extensions() protoreflect.ExtensionDescriptors { return &md.L1.Extensions }
300
func (md *Message) ProtoType(protoreflect.MessageDescriptor) {}
301
func (md *Message) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, md) }
302
func (md *Message) lazyInit() *MessageL2 {
303
md.L0.ParentFile.lazyInit() // implicitly initializes L2
307
// IsMessageSet is a pseudo-internal API for checking whether a message
308
// should serialize in the proto1 message format.
310
// WARNING: This method is exempt from the compatibility promise and may be
311
// removed in the future without warning.
312
func (md *Message) IsMessageSet() bool {
313
return md.L1.IsMessageSet
316
func (fd *Field) Options() protoreflect.ProtoMessage {
317
if f := fd.L1.Options; f != nil {
320
return descopts.Field
322
func (fd *Field) Number() protoreflect.FieldNumber { return fd.L1.Number }
323
func (fd *Field) Cardinality() protoreflect.Cardinality { return fd.L1.Cardinality }
324
func (fd *Field) Kind() protoreflect.Kind {
327
func (fd *Field) HasJSONName() bool { return fd.L1.StringName.hasJSON }
328
func (fd *Field) JSONName() string { return fd.L1.StringName.getJSON(fd) }
329
func (fd *Field) TextName() string { return fd.L1.StringName.getText(fd) }
330
func (fd *Field) HasPresence() bool {
331
if fd.L1.Cardinality == protoreflect.Repeated {
334
explicitFieldPresence := fd.Syntax() == protoreflect.Editions && fd.L1.EditionFeatures.IsFieldPresence
335
return fd.Syntax() == protoreflect.Proto2 || explicitFieldPresence || fd.L1.Message != nil || fd.L1.ContainingOneof != nil
337
func (fd *Field) HasOptionalKeyword() bool {
338
return (fd.L0.ParentFile.L1.Syntax == protoreflect.Proto2 && fd.L1.Cardinality == protoreflect.Optional && fd.L1.ContainingOneof == nil) || fd.L1.IsProto3Optional
340
func (fd *Field) IsPacked() bool {
341
if fd.L1.Cardinality != protoreflect.Repeated {
345
case protoreflect.StringKind, protoreflect.BytesKind, protoreflect.MessageKind, protoreflect.GroupKind:
348
if fd.L0.ParentFile.L1.Syntax == protoreflect.Editions {
349
return fd.L1.EditionFeatures.IsPacked
351
if fd.L0.ParentFile.L1.Syntax == protoreflect.Proto3 {
352
// proto3 repeated fields are packed by default.
353
return !fd.L1.HasPacked || fd.L1.IsPacked
355
return fd.L1.IsPacked
357
func (fd *Field) IsExtension() bool { return false }
358
func (fd *Field) IsWeak() bool { return fd.L1.IsWeak }
359
func (fd *Field) IsList() bool { return fd.Cardinality() == protoreflect.Repeated && !fd.IsMap() }
360
func (fd *Field) IsMap() bool { return fd.Message() != nil && fd.Message().IsMapEntry() }
361
func (fd *Field) MapKey() protoreflect.FieldDescriptor {
365
return fd.Message().Fields().ByNumber(genid.MapEntry_Key_field_number)
367
func (fd *Field) MapValue() protoreflect.FieldDescriptor {
371
return fd.Message().Fields().ByNumber(genid.MapEntry_Value_field_number)
373
func (fd *Field) HasDefault() bool { return fd.L1.Default.has }
374
func (fd *Field) Default() protoreflect.Value { return fd.L1.Default.get(fd) }
375
func (fd *Field) DefaultEnumValue() protoreflect.EnumValueDescriptor { return fd.L1.Default.enum }
376
func (fd *Field) ContainingOneof() protoreflect.OneofDescriptor { return fd.L1.ContainingOneof }
377
func (fd *Field) ContainingMessage() protoreflect.MessageDescriptor {
378
return fd.L0.Parent.(protoreflect.MessageDescriptor)
380
func (fd *Field) Enum() protoreflect.EnumDescriptor {
383
func (fd *Field) Message() protoreflect.MessageDescriptor {
385
if d, _ := protoregistry.GlobalFiles.FindDescriptorByName(fd.L1.Message.FullName()); d != nil {
386
return d.(protoreflect.MessageDescriptor)
391
func (fd *Field) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, fd) }
392
func (fd *Field) ProtoType(protoreflect.FieldDescriptor) {}
394
// EnforceUTF8 is a pseudo-internal API to determine whether to enforce UTF-8
395
// validation for the string field. This exists for Google-internal use only
396
// since proto3 did not enforce UTF-8 validity prior to the open-source release.
397
// If this method does not exist, the default is to enforce valid UTF-8.
399
// WARNING: This method is exempt from the compatibility promise and may be
400
// removed in the future without warning.
401
func (fd *Field) EnforceUTF8() bool {
402
if fd.L0.ParentFile.L1.Syntax == protoreflect.Editions {
403
return fd.L1.EditionFeatures.IsUTF8Validated
405
if fd.L1.HasEnforceUTF8 {
406
return fd.L1.EnforceUTF8
408
return fd.L0.ParentFile.L1.Syntax == protoreflect.Proto3
411
func (od *Oneof) IsSynthetic() bool {
412
return od.L0.ParentFile.L1.Syntax == protoreflect.Proto3 && len(od.L1.Fields.List) == 1 && od.L1.Fields.List[0].HasOptionalKeyword()
414
func (od *Oneof) Options() protoreflect.ProtoMessage {
415
if f := od.L1.Options; f != nil {
418
return descopts.Oneof
420
func (od *Oneof) Fields() protoreflect.FieldDescriptors { return &od.L1.Fields }
421
func (od *Oneof) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, od) }
422
func (od *Oneof) ProtoType(protoreflect.OneofDescriptor) {}
428
L2 *ExtensionL2 // protected by fileDesc.once
431
Number protoreflect.FieldNumber
432
Extendee protoreflect.MessageDescriptor
433
Cardinality protoreflect.Cardinality
434
Kind protoreflect.Kind
435
EditionFeatures EditionFeatures
438
Options func() protoreflect.ProtoMessage
439
StringName stringName
440
IsProto3Optional bool // promoted from google.protobuf.FieldDescriptorProto
441
IsPacked bool // promoted from google.protobuf.FieldOptions
443
Enum protoreflect.EnumDescriptor
444
Message protoreflect.MessageDescriptor
448
func (xd *Extension) Options() protoreflect.ProtoMessage {
449
if f := xd.lazyInit().Options; f != nil {
452
return descopts.Field
454
func (xd *Extension) Number() protoreflect.FieldNumber { return xd.L1.Number }
455
func (xd *Extension) Cardinality() protoreflect.Cardinality { return xd.L1.Cardinality }
456
func (xd *Extension) Kind() protoreflect.Kind { return xd.L1.Kind }
457
func (xd *Extension) HasJSONName() bool { return xd.lazyInit().StringName.hasJSON }
458
func (xd *Extension) JSONName() string { return xd.lazyInit().StringName.getJSON(xd) }
459
func (xd *Extension) TextName() string { return xd.lazyInit().StringName.getText(xd) }
460
func (xd *Extension) HasPresence() bool { return xd.L1.Cardinality != protoreflect.Repeated }
461
func (xd *Extension) HasOptionalKeyword() bool {
462
return (xd.L0.ParentFile.L1.Syntax == protoreflect.Proto2 && xd.L1.Cardinality == protoreflect.Optional) || xd.lazyInit().IsProto3Optional
464
func (xd *Extension) IsPacked() bool { return xd.lazyInit().IsPacked }
465
func (xd *Extension) IsExtension() bool { return true }
466
func (xd *Extension) IsWeak() bool { return false }
467
func (xd *Extension) IsList() bool { return xd.Cardinality() == protoreflect.Repeated }
468
func (xd *Extension) IsMap() bool { return false }
469
func (xd *Extension) MapKey() protoreflect.FieldDescriptor { return nil }
470
func (xd *Extension) MapValue() protoreflect.FieldDescriptor { return nil }
471
func (xd *Extension) HasDefault() bool { return xd.lazyInit().Default.has }
472
func (xd *Extension) Default() protoreflect.Value { return xd.lazyInit().Default.get(xd) }
473
func (xd *Extension) DefaultEnumValue() protoreflect.EnumValueDescriptor {
474
return xd.lazyInit().Default.enum
476
func (xd *Extension) ContainingOneof() protoreflect.OneofDescriptor { return nil }
477
func (xd *Extension) ContainingMessage() protoreflect.MessageDescriptor { return xd.L1.Extendee }
478
func (xd *Extension) Enum() protoreflect.EnumDescriptor { return xd.lazyInit().Enum }
479
func (xd *Extension) Message() protoreflect.MessageDescriptor { return xd.lazyInit().Message }
480
func (xd *Extension) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, xd) }
481
func (xd *Extension) ProtoType(protoreflect.FieldDescriptor) {}
482
func (xd *Extension) ProtoInternal(pragma.DoNotImplement) {}
483
func (xd *Extension) lazyInit() *ExtensionL2 {
484
xd.L0.ParentFile.lazyInit() // implicitly initializes L2
492
L2 *ServiceL2 // protected by fileDesc.once
496
Options func() protoreflect.ProtoMessage
505
Options func() protoreflect.ProtoMessage
506
Input protoreflect.MessageDescriptor
507
Output protoreflect.MessageDescriptor
508
IsStreamingClient bool
509
IsStreamingServer bool
513
func (sd *Service) Options() protoreflect.ProtoMessage {
514
if f := sd.lazyInit().Options; f != nil {
517
return descopts.Service
519
func (sd *Service) Methods() protoreflect.MethodDescriptors { return &sd.lazyInit().Methods }
520
func (sd *Service) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, sd) }
521
func (sd *Service) ProtoType(protoreflect.ServiceDescriptor) {}
522
func (sd *Service) ProtoInternal(pragma.DoNotImplement) {}
523
func (sd *Service) lazyInit() *ServiceL2 {
524
sd.L0.ParentFile.lazyInit() // implicitly initializes L2
528
func (md *Method) Options() protoreflect.ProtoMessage {
529
if f := md.L1.Options; f != nil {
532
return descopts.Method
534
func (md *Method) Input() protoreflect.MessageDescriptor { return md.L1.Input }
535
func (md *Method) Output() protoreflect.MessageDescriptor { return md.L1.Output }
536
func (md *Method) IsStreamingClient() bool { return md.L1.IsStreamingClient }
537
func (md *Method) IsStreamingServer() bool { return md.L1.IsStreamingServer }
538
func (md *Method) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, md) }
539
func (md *Method) ProtoType(protoreflect.MethodDescriptor) {}
540
func (md *Method) ProtoInternal(pragma.DoNotImplement) {}
542
// Surrogate files are can be used to create standalone descriptors
543
// where the syntax is only information derived from the parent file.
545
SurrogateProto2 = &File{L1: FileL1{Syntax: protoreflect.Proto2}, L2: &FileL2{}}
546
SurrogateProto3 = &File{L1: FileL1{Syntax: protoreflect.Proto3}, L2: &FileL2{}}
554
FullName protoreflect.FullName // must be populated
555
ParentFile *File // must be populated
556
Parent protoreflect.Descriptor
561
func (d *Base) Name() protoreflect.Name { return d.L0.FullName.Name() }
562
func (d *Base) FullName() protoreflect.FullName { return d.L0.FullName }
563
func (d *Base) ParentFile() protoreflect.FileDescriptor {
564
if d.L0.ParentFile == SurrogateProto2 || d.L0.ParentFile == SurrogateProto3 {
565
return nil // surrogate files are not real parents
567
return d.L0.ParentFile
569
func (d *Base) Parent() protoreflect.Descriptor { return d.L0.Parent }
570
func (d *Base) Index() int { return d.L0.Index }
571
func (d *Base) Syntax() protoreflect.Syntax { return d.L0.ParentFile.Syntax() }
572
func (d *Base) IsPlaceholder() bool { return false }
573
func (d *Base) ProtoInternal(pragma.DoNotImplement) {}
575
type stringName struct {
582
// InitJSON initializes the name. It is exported for use by other internal packages.
583
func (s *stringName) InitJSON(name string) {
588
func (s *stringName) lazyInit(fd protoreflect.FieldDescriptor) *stringName {
590
if fd.IsExtension() {
591
// For extensions, JSON and text are formatted the same way.
593
if messageset.IsMessageSetExtension(fd) {
594
name = string("[" + fd.FullName().Parent() + "]")
596
name = string("[" + fd.FullName() + "]")
601
// Format the JSON name.
603
s.nameJSON = strs.JSONCamelCase(string(fd.Name()))
606
// Format the text name.
607
s.nameText = string(fd.Name())
608
if fd.Kind() == protoreflect.GroupKind {
609
s.nameText = string(fd.Message().Name())
616
func (s *stringName) getJSON(fd protoreflect.FieldDescriptor) string { return s.lazyInit(fd).nameJSON }
617
func (s *stringName) getText(fd protoreflect.FieldDescriptor) string { return s.lazyInit(fd).nameText }
619
func DefaultValue(v protoreflect.Value, ev protoreflect.EnumValueDescriptor) defaultValue {
620
dv := defaultValue{has: v.IsValid(), val: v, enum: ev}
621
if b, ok := v.Interface().([]byte); ok {
622
// Store a copy of the default bytes, so that we can detect
623
// accidental mutations of the original value.
624
dv.bytes = append([]byte(nil), b...)
629
func unmarshalDefault(b []byte, k protoreflect.Kind, pf *File, ed protoreflect.EnumDescriptor) defaultValue {
630
var evs protoreflect.EnumValueDescriptors
631
if k == protoreflect.EnumKind {
632
// If the enum is declared within the same file, be careful not to
633
// blindly call the Values method, lest we bind ourselves in a deadlock.
634
if e, ok := ed.(*Enum); ok && e.L0.ParentFile == pf {
640
// If we are unable to resolve the enum dependency, use a placeholder
641
// enum value since we will not be able to parse the default value.
642
if ed.IsPlaceholder() && protoreflect.Name(b).IsValid() {
643
v := protoreflect.ValueOfEnum(0)
644
ev := PlaceholderEnumValue(ed.FullName().Parent().Append(protoreflect.Name(b)))
645
return DefaultValue(v, ev)
649
v, ev, err := defval.Unmarshal(string(b), k, evs, defval.Descriptor)
653
return DefaultValue(v, ev)
656
type defaultValue struct {
658
val protoreflect.Value
659
enum protoreflect.EnumValueDescriptor
663
func (dv *defaultValue) get(fd protoreflect.FieldDescriptor) protoreflect.Value {
664
// Return the zero value as the default if unpopulated.
666
if fd.Cardinality() == protoreflect.Repeated {
667
return protoreflect.Value{}
670
case protoreflect.BoolKind:
671
return protoreflect.ValueOfBool(false)
672
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
673
return protoreflect.ValueOfInt32(0)
674
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
675
return protoreflect.ValueOfInt64(0)
676
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
677
return protoreflect.ValueOfUint32(0)
678
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
679
return protoreflect.ValueOfUint64(0)
680
case protoreflect.FloatKind:
681
return protoreflect.ValueOfFloat32(0)
682
case protoreflect.DoubleKind:
683
return protoreflect.ValueOfFloat64(0)
684
case protoreflect.StringKind:
685
return protoreflect.ValueOfString("")
686
case protoreflect.BytesKind:
687
return protoreflect.ValueOfBytes(nil)
688
case protoreflect.EnumKind:
689
if evs := fd.Enum().Values(); evs.Len() > 0 {
690
return protoreflect.ValueOfEnum(evs.Get(0).Number())
692
return protoreflect.ValueOfEnum(0)
696
if len(dv.bytes) > 0 && !bytes.Equal(dv.bytes, dv.val.Bytes()) {
697
// TODO: Avoid panic if we're running with the race detector
698
// and instead spawn a goroutine that periodically resets
699
// this value back to the original to induce a race.
700
panic(fmt.Sprintf("detected mutation on the default bytes for %v", fd.FullName()))