podman

Форк
0
703 строки · 25.9 Кб
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.
4

5
package filedesc
6

7
import (
8
	"bytes"
9
	"fmt"
10
	"sync"
11
	"sync/atomic"
12

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"
22
)
23

24
// Edition is an Enum for proto2.Edition
25
type Edition int32
26

27
// These values align with the value of Enum in descriptor.proto which allows
28
// direct conversion between the proto enum and this enum.
29
const (
30
	EditionUnknown     Edition = 0
31
	EditionProto2      Edition = 998
32
	EditionProto3      Edition = 999
33
	Edition2023        Edition = 1000
34
	EditionUnsupported Edition = 100000
35
)
36

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.
47
//
48
// The types are exported so that packages like reflect/protodesc can
49
// directly construct descriptors.
50

51
type (
52
	File struct {
53
		fileRaw
54
		L1 FileL1
55

56
		once uint32     // atomically set if L2 is valid
57
		mu   sync.Mutex // protects L2
58
		L2   *FileL2
59
	}
60
	FileL1 struct {
61
		Syntax  protoreflect.Syntax
62
		Edition Edition // Only used if Syntax == Editions
63
		Path    string
64
		Package protoreflect.FullName
65

66
		Enums      Enums
67
		Messages   Messages
68
		Extensions Extensions
69
		Services   Services
70

71
		EditionFeatures EditionFeatures
72
	}
73
	FileL2 struct {
74
		Options   func() protoreflect.ProtoMessage
75
		Imports   FileImports
76
		Locations SourceLocations
77
	}
78

79
	EditionFeatures struct {
80
		// IsFieldPresence is true if field_presence is EXPLICIT
81
		// https://protobuf.dev/editions/features/#field_presence
82
		IsFieldPresence bool
83
		// IsFieldPresence is true if field_presence is LEGACY_REQUIRED
84
		// https://protobuf.dev/editions/features/#field_presence
85
		IsLegacyRequired bool
86
		// IsOpenEnum is true if enum_type is OPEN
87
		// https://protobuf.dev/editions/features/#enum_type
88
		IsOpenEnum bool
89
		// IsPacked is true if repeated_field_encoding is PACKED
90
		// https://protobuf.dev/editions/features/#repeated_field_encoding
91
		IsPacked bool
92
		// IsUTF8Validated is true if utf_validation is VERIFY
93
		// https://protobuf.dev/editions/features/#utf8_validation
94
		IsUTF8Validated bool
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
100
		IsJSONCompliant bool
101
		// GenerateLegacyUnmarshalJSON determines if the plugin generates the
102
		// UnmarshalJSON([]byte) error method for enums.
103
		GenerateLegacyUnmarshalJSON bool
104
	}
105
)
106

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 {
116
		return f()
117
	}
118
	return descopts.File
119
}
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)           {}
131

132
func (fd *File) lazyInit() *FileL2 {
133
	if atomic.LoadUint32(&fd.once) == 0 {
134
		fd.lazyInitOnce()
135
	}
136
	return fd.L2
137
}
138

139
func (fd *File) lazyInitOnce() {
140
	fd.mu.Lock()
141
	if fd.L2 == nil {
142
		fd.lazyRawInit() // recursively initializes all L2 structures
143
	}
144
	atomic.StoreUint32(&fd.once, 1)
145
	fd.mu.Unlock()
146
}
147

148
// GoPackagePath is a pseudo-internal API for determining the Go package path
149
// that this file descriptor is declared in.
150
//
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
155
}
156

157
type (
158
	Enum struct {
159
		Base
160
		L1 EnumL1
161
		L2 *EnumL2 // protected by fileDesc.once
162
	}
163
	EnumL1 struct {
164
		eagerValues bool // controls whether EnumL2.Values is already populated
165

166
		EditionFeatures EditionFeatures
167
	}
168
	EnumL2 struct {
169
		Options        func() protoreflect.ProtoMessage
170
		Values         EnumValues
171
		ReservedNames  Names
172
		ReservedRanges EnumRanges
173
	}
174

175
	EnumValue struct {
176
		Base
177
		L1 EnumValueL1
178
	}
179
	EnumValueL1 struct {
180
		Options func() protoreflect.ProtoMessage
181
		Number  protoreflect.EnumNumber
182
	}
183
)
184

185
func (ed *Enum) Options() protoreflect.ProtoMessage {
186
	if f := ed.lazyInit().Options; f != nil {
187
		return f()
188
	}
189
	return descopts.Enum
190
}
191
func (ed *Enum) Values() protoreflect.EnumValueDescriptors {
192
	if ed.L1.eagerValues {
193
		return &ed.L2.Values
194
	}
195
	return &ed.lazyInit().Values
196
}
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
203
	return ed.L2
204
}
205

206
func (ed *EnumValue) Options() protoreflect.ProtoMessage {
207
	if f := ed.L1.Options; f != nil {
208
		return f()
209
	}
210
	return descopts.EnumValue
211
}
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) {}
215

216
type (
217
	Message struct {
218
		Base
219
		L1 MessageL1
220
		L2 *MessageL2 // protected by fileDesc.once
221
	}
222
	MessageL1 struct {
223
		Enums        Enums
224
		Messages     Messages
225
		Extensions   Extensions
226
		IsMapEntry   bool // promoted from google.protobuf.MessageOptions
227
		IsMessageSet bool // promoted from google.protobuf.MessageOptions
228

229
		EditionFeatures EditionFeatures
230
	}
231
	MessageL2 struct {
232
		Options               func() protoreflect.ProtoMessage
233
		Fields                Fields
234
		Oneofs                Oneofs
235
		ReservedNames         Names
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
240
	}
241

242
	Field struct {
243
		Base
244
		L1 FieldL1
245
	}
246
	FieldL1 struct {
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
258
		Default          defaultValue
259
		ContainingOneof  protoreflect.OneofDescriptor // must be consistent with Message.Oneofs.Fields
260
		Enum             protoreflect.EnumDescriptor
261
		Message          protoreflect.MessageDescriptor
262

263
		EditionFeatures EditionFeatures
264
	}
265

266
	Oneof struct {
267
		Base
268
		L1 OneofL1
269
	}
270
	OneofL1 struct {
271
		Options func() protoreflect.ProtoMessage
272
		Fields  OneofFields // must be consistent with Message.Fields.ContainingOneof
273

274
		EditionFeatures EditionFeatures
275
	}
276
)
277

278
func (md *Message) Options() protoreflect.ProtoMessage {
279
	if f := md.lazyInit().Options; f != nil {
280
		return f()
281
	}
282
	return descopts.Message
283
}
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 {
293
		return f()
294
	}
295
	return descopts.ExtensionRange
296
}
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
304
	return md.L2
305
}
306

307
// IsMessageSet is a pseudo-internal API for checking whether a message
308
// should serialize in the proto1 message format.
309
//
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
314
}
315

316
func (fd *Field) Options() protoreflect.ProtoMessage {
317
	if f := fd.L1.Options; f != nil {
318
		return f()
319
	}
320
	return descopts.Field
321
}
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 {
325
	return fd.L1.Kind
326
}
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 {
332
		return false
333
	}
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
336
}
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
339
}
340
func (fd *Field) IsPacked() bool {
341
	if fd.L1.Cardinality != protoreflect.Repeated {
342
		return false
343
	}
344
	switch fd.L1.Kind {
345
	case protoreflect.StringKind, protoreflect.BytesKind, protoreflect.MessageKind, protoreflect.GroupKind:
346
		return false
347
	}
348
	if fd.L0.ParentFile.L1.Syntax == protoreflect.Editions {
349
		return fd.L1.EditionFeatures.IsPacked
350
	}
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
354
	}
355
	return fd.L1.IsPacked
356
}
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 {
362
	if !fd.IsMap() {
363
		return nil
364
	}
365
	return fd.Message().Fields().ByNumber(genid.MapEntry_Key_field_number)
366
}
367
func (fd *Field) MapValue() protoreflect.FieldDescriptor {
368
	if !fd.IsMap() {
369
		return nil
370
	}
371
	return fd.Message().Fields().ByNumber(genid.MapEntry_Value_field_number)
372
}
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)
379
}
380
func (fd *Field) Enum() protoreflect.EnumDescriptor {
381
	return fd.L1.Enum
382
}
383
func (fd *Field) Message() protoreflect.MessageDescriptor {
384
	if fd.L1.IsWeak {
385
		if d, _ := protoregistry.GlobalFiles.FindDescriptorByName(fd.L1.Message.FullName()); d != nil {
386
			return d.(protoreflect.MessageDescriptor)
387
		}
388
	}
389
	return fd.L1.Message
390
}
391
func (fd *Field) Format(s fmt.State, r rune)             { descfmt.FormatDesc(s, r, fd) }
392
func (fd *Field) ProtoType(protoreflect.FieldDescriptor) {}
393

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.
398
//
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
404
	}
405
	if fd.L1.HasEnforceUTF8 {
406
		return fd.L1.EnforceUTF8
407
	}
408
	return fd.L0.ParentFile.L1.Syntax == protoreflect.Proto3
409
}
410

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()
413
}
414
func (od *Oneof) Options() protoreflect.ProtoMessage {
415
	if f := od.L1.Options; f != nil {
416
		return f()
417
	}
418
	return descopts.Oneof
419
}
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) {}
423

424
type (
425
	Extension struct {
426
		Base
427
		L1 ExtensionL1
428
		L2 *ExtensionL2 // protected by fileDesc.once
429
	}
430
	ExtensionL1 struct {
431
		Number          protoreflect.FieldNumber
432
		Extendee        protoreflect.MessageDescriptor
433
		Cardinality     protoreflect.Cardinality
434
		Kind            protoreflect.Kind
435
		EditionFeatures EditionFeatures
436
	}
437
	ExtensionL2 struct {
438
		Options          func() protoreflect.ProtoMessage
439
		StringName       stringName
440
		IsProto3Optional bool // promoted from google.protobuf.FieldDescriptorProto
441
		IsPacked         bool // promoted from google.protobuf.FieldOptions
442
		Default          defaultValue
443
		Enum             protoreflect.EnumDescriptor
444
		Message          protoreflect.MessageDescriptor
445
	}
446
)
447

448
func (xd *Extension) Options() protoreflect.ProtoMessage {
449
	if f := xd.lazyInit().Options; f != nil {
450
		return f()
451
	}
452
	return descopts.Field
453
}
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
463
}
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
475
}
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
485
	return xd.L2
486
}
487

488
type (
489
	Service struct {
490
		Base
491
		L1 ServiceL1
492
		L2 *ServiceL2 // protected by fileDesc.once
493
	}
494
	ServiceL1 struct{}
495
	ServiceL2 struct {
496
		Options func() protoreflect.ProtoMessage
497
		Methods Methods
498
	}
499

500
	Method struct {
501
		Base
502
		L1 MethodL1
503
	}
504
	MethodL1 struct {
505
		Options           func() protoreflect.ProtoMessage
506
		Input             protoreflect.MessageDescriptor
507
		Output            protoreflect.MessageDescriptor
508
		IsStreamingClient bool
509
		IsStreamingServer bool
510
	}
511
)
512

513
func (sd *Service) Options() protoreflect.ProtoMessage {
514
	if f := sd.lazyInit().Options; f != nil {
515
		return f()
516
	}
517
	return descopts.Service
518
}
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
525
	return sd.L2
526
}
527

528
func (md *Method) Options() protoreflect.ProtoMessage {
529
	if f := md.L1.Options; f != nil {
530
		return f()
531
	}
532
	return descopts.Method
533
}
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)     {}
541

542
// Surrogate files are can be used to create standalone descriptors
543
// where the syntax is only information derived from the parent file.
544
var (
545
	SurrogateProto2 = &File{L1: FileL1{Syntax: protoreflect.Proto2}, L2: &FileL2{}}
546
	SurrogateProto3 = &File{L1: FileL1{Syntax: protoreflect.Proto3}, L2: &FileL2{}}
547
)
548

549
type (
550
	Base struct {
551
		L0 BaseL0
552
	}
553
	BaseL0 struct {
554
		FullName   protoreflect.FullName // must be populated
555
		ParentFile *File                 // must be populated
556
		Parent     protoreflect.Descriptor
557
		Index      int
558
	}
559
)
560

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
566
	}
567
	return d.L0.ParentFile
568
}
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) {}
574

575
type stringName struct {
576
	hasJSON  bool
577
	once     sync.Once
578
	nameJSON string
579
	nameText string
580
}
581

582
// InitJSON initializes the name. It is exported for use by other internal packages.
583
func (s *stringName) InitJSON(name string) {
584
	s.hasJSON = true
585
	s.nameJSON = name
586
}
587

588
func (s *stringName) lazyInit(fd protoreflect.FieldDescriptor) *stringName {
589
	s.once.Do(func() {
590
		if fd.IsExtension() {
591
			// For extensions, JSON and text are formatted the same way.
592
			var name string
593
			if messageset.IsMessageSetExtension(fd) {
594
				name = string("[" + fd.FullName().Parent() + "]")
595
			} else {
596
				name = string("[" + fd.FullName() + "]")
597
			}
598
			s.nameJSON = name
599
			s.nameText = name
600
		} else {
601
			// Format the JSON name.
602
			if !s.hasJSON {
603
				s.nameJSON = strs.JSONCamelCase(string(fd.Name()))
604
			}
605

606
			// Format the text name.
607
			s.nameText = string(fd.Name())
608
			if fd.Kind() == protoreflect.GroupKind {
609
				s.nameText = string(fd.Message().Name())
610
			}
611
		}
612
	})
613
	return s
614
}
615

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 }
618

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...)
625
	}
626
	return dv
627
}
628

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 {
635
			evs = &e.L2.Values
636
		} else {
637
			evs = ed.Values()
638
		}
639

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)
646
		}
647
	}
648

649
	v, ev, err := defval.Unmarshal(string(b), k, evs, defval.Descriptor)
650
	if err != nil {
651
		panic(err)
652
	}
653
	return DefaultValue(v, ev)
654
}
655

656
type defaultValue struct {
657
	has   bool
658
	val   protoreflect.Value
659
	enum  protoreflect.EnumValueDescriptor
660
	bytes []byte
661
}
662

663
func (dv *defaultValue) get(fd protoreflect.FieldDescriptor) protoreflect.Value {
664
	// Return the zero value as the default if unpopulated.
665
	if !dv.has {
666
		if fd.Cardinality() == protoreflect.Repeated {
667
			return protoreflect.Value{}
668
		}
669
		switch fd.Kind() {
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())
691
			}
692
			return protoreflect.ValueOfEnum(0)
693
		}
694
	}
695

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()))
701
	}
702
	return dv.val
703
}
704

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.