forked from bufbuild/protocompile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
descriptors.go
2000 lines (1701 loc) · 53.8 KB
/
descriptors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package linker
import (
"context"
"fmt"
"strconv"
"strings"
"unicode/utf8"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/dynamicpb"
"github.com/kralicky/protocompile/ast"
"github.com/kralicky/protocompile/editions"
"github.com/kralicky/protocompile/parser"
"github.com/kralicky/protocompile/protointernal"
"github.com/kralicky/protocompile/protoutil"
"github.com/kralicky/protocompile/sourceinfo"
art "github.com/plar/go-adaptive-radix-tree"
)
var (
// These "noOp*" values are all descriptors. The protoreflect.Descriptor
// interface and its sub-interfaces are all marked with an unexported
// method so that they cannot be implemented outside of the google.golang.org/protobuf
// module. So, to provide implementations from this package, we must embed
// them. If we simply left the embedded interface field nil, then if/when
// new methods are added to the interfaces, it could induce panics in this
// package or users of this module (since trying to invoke one of these new
// methods would end up trying to call a method on a nil interface value).
//
// So instead of leaving the embedded interface fields nil, we embed an actual
// value. While new methods are unlikely to return the correct value (since
// the calls will be delegated to these no-op instances), it is a less
// dangerous latent bug than inducing a nil-dereference panic.
noOpFile protoreflect.FileDescriptor
noOpMessage protoreflect.MessageDescriptor
noOpOneof protoreflect.OneofDescriptor
noOpField protoreflect.FieldDescriptor
noOpEnum protoreflect.EnumDescriptor
noOpEnumValue protoreflect.EnumValueDescriptor
noOpExtension protoreflect.ExtensionDescriptor
noOpService protoreflect.ServiceDescriptor
noOpMethod protoreflect.MethodDescriptor
)
var (
fieldPresenceField = editions.FeatureSetDescriptor.Fields().ByName("field_presence")
repeatedFieldEncodingField = editions.FeatureSetDescriptor.Fields().ByName("repeated_field_encoding")
messageEncodingField = editions.FeatureSetDescriptor.Fields().ByName("message_encoding")
enumTypeField = editions.FeatureSetDescriptor.Fields().ByName("enum_type")
jsonFormatField = editions.FeatureSetDescriptor.Fields().ByName("json_format")
)
func init() {
noOpFile, _ = protodesc.NewFile(
&descriptorpb.FileDescriptorProto{
Name: proto.String("no-op.proto"),
Syntax: proto.String("proto2"),
Dependency: []string{"google/protobuf/descriptor.proto"},
MessageType: []*descriptorpb.DescriptorProto{
{
Name: proto.String("NoOpMsg"),
Field: []*descriptorpb.FieldDescriptorProto{
{
Name: proto.String("no_op"),
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
Number: proto.Int32(1),
JsonName: proto.String("noOp"),
OneofIndex: proto.Int32(0),
},
},
OneofDecl: []*descriptorpb.OneofDescriptorProto{
{
Name: proto.String("no_op_oneof"),
},
},
},
},
EnumType: []*descriptorpb.EnumDescriptorProto{
{
Name: proto.String("NoOpEnum"),
Value: []*descriptorpb.EnumValueDescriptorProto{
{
Name: proto.String("NO_OP"),
Number: proto.Int32(0),
},
},
},
},
Extension: []*descriptorpb.FieldDescriptorProto{
{
Extendee: proto.String(".google.protobuf.FileOptions"),
Name: proto.String("no_op"),
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
Number: proto.Int32(50000),
},
},
Service: []*descriptorpb.ServiceDescriptorProto{
{
Name: proto.String("NoOpService"),
Method: []*descriptorpb.MethodDescriptorProto{
{
Name: proto.String("NoOp"),
InputType: proto.String(".NoOpMsg"),
OutputType: proto.String(".NoOpMsg"),
},
},
},
},
},
protoregistry.GlobalFiles,
)
noOpMessage = noOpFile.Messages().Get(0)
noOpOneof = noOpMessage.Oneofs().Get(0)
noOpField = noOpMessage.Fields().Get(0)
noOpEnum = noOpFile.Enums().Get(0)
noOpEnumValue = noOpEnum.Values().Get(0)
noOpExtension = noOpFile.Extensions().Get(0)
noOpService = noOpFile.Services().Get(0)
noOpMethod = noOpService.Methods().Get(0)
}
// This file contains implementations of protoreflect.Descriptor. Note that
// this is a hack since those interfaces have a "doNotImplement" tag
// interface therein. We do just enough to make dynamicpb happy; constructing
// a regular descriptor would fail because we haven't yet interpreted options
// at the point we need these, and some validations will fail if the options
// aren't present.
type result struct {
protoreflect.FileDescriptor
parser.Result
prefix string
deps Files
// A map of all descriptors keyed by their fully-qualified name (without
// any leading dot).
descriptors art.Tree
// A set of imports that have been used in the course of linking and
// interpreting options.
usedImports map[string]struct{}
// A map of AST nodes that represent identifiers in ast.FieldReferenceNodes
// to their fully-qualified name. The identifiers are for field names in
// message literals (in option values) that are extension fields. These names
// are resolved during linking and stored here, to be used to interpret options.
optionQualifiedNames map[*ast.IdentValueNode]string
// A map of descriptors to all resolved references to them. The reference
// locations contain the start and end position of the relevant identifier
// referencing the descriptor.
resolvedReferences map[protoreflect.Descriptor][]ast.NodeReference
extensionsByMessage map[protoreflect.FullName][]protoreflect.ExtensionDescriptor
imports fileImports
messages msgDescriptors
enums enumDescriptors
extensions extDescriptors
services svcDescriptors
srcLocations srcLocs
optsIndex sourceinfo.OptionIndex
optsDescIndex sourceinfo.OptionDescriptorIndex
}
var (
_ protoreflect.FileDescriptor = (*result)(nil)
_ Result = (*result)(nil)
_ protoutil.DescriptorProtoWrapper = (*result)(nil)
)
func (r *result) Dependencies() Files {
return r.deps
}
var _ editions.HasEdition = (*result)(nil)
func (r *result) RemoveAST() {
r.Result = parser.ResultWithoutAST(r.FileDescriptorProto())
r.optionQualifiedNames = nil
}
func (r *result) AsProto() proto.Message {
return r.FileDescriptorProto()
}
func (r *result) ParentFile() protoreflect.FileDescriptor {
return r
}
func (r *result) Parent() protoreflect.Descriptor {
return nil
}
func (r *result) Index() int {
return 0
}
func (r *result) Syntax() protoreflect.Syntax {
switch r.FileDescriptorProto().GetSyntax() {
case "proto2", "":
return protoreflect.Proto2
case "proto3":
return protoreflect.Proto3
case "editions":
return protoreflect.Editions
default:
return 0 // ???
}
}
func (r *result) Edition() int32 {
switch r.Syntax() {
case protoreflect.Proto2:
return int32(descriptorpb.Edition_EDITION_PROTO2)
case protoreflect.Proto3:
return int32(descriptorpb.Edition_EDITION_PROTO3)
case protoreflect.Editions:
return int32(r.FileDescriptorProto().GetEdition())
default:
return int32(descriptorpb.Edition_EDITION_UNKNOWN) // ???
}
}
func (r *result) Name() protoreflect.Name {
return ""
}
func (r *result) String() string {
return r.FileNode().Name()
}
func (r *result) FullName() protoreflect.FullName {
return r.Package()
}
func (r *result) IsPlaceholder() bool {
return false
}
func (r *result) Options() protoreflect.ProtoMessage {
return r.FileDescriptorProto().Options
}
func (r *result) Path() string {
return r.FileDescriptorProto().GetName()
}
func (r *result) Package() protoreflect.FullName {
return protoreflect.FullName(r.FileDescriptorProto().GetPackage())
}
func (r *result) Imports() protoreflect.FileImports {
return &r.imports
}
func (r *result) Enums() protoreflect.EnumDescriptors {
return &r.enums
}
func (r *result) Messages() protoreflect.MessageDescriptors {
return &r.messages
}
func (r *result) Extensions() protoreflect.ExtensionDescriptors {
return &r.extensions
}
func (r *result) Services() protoreflect.ServiceDescriptors {
return &r.services
}
func (r *result) PopulateSourceCodeInfo(optsIndex sourceinfo.OptionIndex, optsDescIndex sourceinfo.OptionDescriptorIndex) {
srcLocProtos := asSourceLocations(r.FileDescriptorProto().GetSourceCodeInfo().GetLocation())
srcLocIndex := computeSourceLocIndex(srcLocProtos)
r.srcLocations = srcLocs{file: r, locs: srcLocProtos, index: srcLocIndex}
r.optsIndex = optsIndex
r.optsDescIndex = optsDescIndex
a := r.AST()
for node, desc := range optsDescIndex.FieldReferenceNodesToFieldDescriptors {
r.resolvedReferences[desc] = append(r.resolvedReferences[desc], ast.NewNodeReference(a, node))
if ref, ok := node.(*ast.FieldReferenceNode); ok {
if name := ref.GetName().GetCompoundIdent(); name != nil {
r.indexCompoundIdentRefs(name, desc)
}
}
}
for node, desc := range optsDescIndex.EnumValueIdentNodesToEnumValueDescriptors {
r.resolvedReferences[desc] = append(r.resolvedReferences[desc], ast.NewNodeReference(a, node))
}
}
func (r *result) SourceLocations() protoreflect.SourceLocations {
return &r.srcLocations
}
func (r *result) FindOptionSourceInfo(node *ast.OptionNode) *sourceinfo.OptionSourceInfo {
if r.optsIndex == nil {
return nil
}
return r.optsIndex[node]
}
func (r *result) FindDescriptorsByPrefix(ctx context.Context, prefix string, filter ...func(protoreflect.Descriptor) bool) (results []protoreflect.Descriptor, err error) {
r.descriptors.ForEachPrefix(art.Key(prefix), func(node art.Node) (cont bool) {
if ctx.Err() != nil {
err = ctx.Err()
return false
}
if node.Kind() == art.Leaf {
value := node.Value()
if value == nil {
return true
}
if desc, ok := value.(protoreflect.Descriptor); ok {
if len(filter) > 0 {
if !filter[0](desc) {
return true
}
}
results = append(results, desc)
}
}
return true
})
return
}
func (r *result) RangeDescriptors(ctx context.Context, fn func(protoreflect.Descriptor) bool) (err error) {
r.descriptors.ForEach(func(node art.Node) (cont bool) {
if ctx.Err() != nil {
err = ctx.Err()
return false
}
if desc, ok := node.Value().(protoreflect.Descriptor); ok {
return fn(desc)
}
return true
}, art.TraverseLeaf)
return
}
func (r *result) FindReferences(to protoreflect.Descriptor) (results []ast.NodeReference) {
if r.resolvedReferences == nil {
return nil
}
return r.resolvedReferences[to]
}
func (o *result) FindOptionNameFieldDescriptor(name *descriptorpb.UninterpretedOption_NamePart) protoreflect.FieldDescriptor {
return o.optsDescIndex.UninterpretedNameDescriptorsToFieldDescriptors[name]
}
func (o *result) FindOptionFieldDescriptor(option *descriptorpb.UninterpretedOption) protoreflect.FieldDescriptor {
return o.optsDescIndex.OptionsToFieldDescriptors[option]
}
func (o *result) FindFieldDescriptorByFieldReferenceNode(node *ast.FieldReferenceNode) protoreflect.FieldDescriptor {
return o.optsDescIndex.FieldReferenceNodesToFieldDescriptors[node]
}
func (o *result) FindFieldDescriptorByMessageFieldNode(node *ast.MessageFieldNode) protoreflect.FieldDescriptor {
return o.optsDescIndex.FieldReferenceNodesToFieldDescriptors[node]
}
func (o *result) RangeFieldReferenceNodesWithDescriptors(f func(node ast.Node, desc protoreflect.FieldDescriptor) bool) {
for node, desc := range o.optsDescIndex.FieldReferenceNodesToFieldDescriptors {
if !f(node, desc) {
return
}
}
}
func (o *result) FindMessageDescriptorByTypeReferenceURLNode(node *ast.FieldReferenceNode) protoreflect.MessageDescriptor {
return o.optsDescIndex.TypeReferenceURLsToMessageDescriptors[node]
}
func computeSourceLocIndex(locs []protoreflect.SourceLocation) map[interface{}]int {
index := map[interface{}]int{}
for i, loc := range locs {
if loc.Next == 0 {
index[pathKey(loc.Path)] = i
}
}
return index
}
func asSourceLocations(srcInfoProtos []*descriptorpb.SourceCodeInfo_Location) []protoreflect.SourceLocation {
locs := make([]protoreflect.SourceLocation, len(srcInfoProtos))
prev := map[any]*protoreflect.SourceLocation{}
for i, loc := range srcInfoProtos {
var stLin, stCol, enLin, enCol int
if len(loc.Span) == 3 {
stLin, stCol, enCol = int(loc.Span[0]), int(loc.Span[1]), int(loc.Span[2])
enLin = stLin
} else {
stLin, stCol, enLin, enCol = int(loc.Span[0]), int(loc.Span[1]), int(loc.Span[2]), int(loc.Span[3])
}
locs[i] = protoreflect.SourceLocation{
Path: loc.Path,
LeadingComments: loc.GetLeadingComments(),
LeadingDetachedComments: loc.GetLeadingDetachedComments(),
TrailingComments: loc.GetTrailingComments(),
StartLine: stLin,
StartColumn: stCol,
EndLine: enLin,
EndColumn: enCol,
}
str := pathKey(loc.Path)
pr := prev[str]
if pr != nil {
pr.Next = i
}
prev[str] = &locs[i]
}
return locs
}
type fileImports struct {
protoreflect.FileImports
files []protoreflect.FileImport
}
func (r *result) createImports() fileImports {
fd := r.FileDescriptorProto()
imps := make([]protoreflect.FileImport, len(fd.Dependency))
for i, dep := range fd.Dependency {
desc := r.deps.FindFileByPath(dep)
if desc == nil {
imps[i] = protoreflect.FileImport{FileDescriptor: NewPlaceholderFile(dep)}
} else {
imps[i] = protoreflect.FileImport{FileDescriptor: unwrap(desc)}
}
}
for _, publicIndex := range fd.PublicDependency {
imps[int(publicIndex)].IsPublic = true
}
for _, weakIndex := range fd.WeakDependency {
imps[int(weakIndex)].IsWeak = true
}
return fileImports{files: imps}
}
func unwrap(descriptor protoreflect.FileDescriptor) protoreflect.FileDescriptor {
wrapped, ok := descriptor.(interface {
Unwrap() protoreflect.FileDescriptor
})
if !ok {
return descriptor
}
unwrapped := wrapped.Unwrap()
if unwrapped == nil {
return descriptor // shouldn't ever happen
}
return unwrapped
}
func (f *fileImports) Len() int {
return len(f.files)
}
func (f *fileImports) Get(i int) protoreflect.FileImport {
return f.files[i]
}
type srcLocs struct {
protoreflect.SourceLocations
file *result
locs []protoreflect.SourceLocation
index map[interface{}]int
}
func (s *srcLocs) Len() int {
return len(s.locs)
}
func (s *srcLocs) Get(i int) protoreflect.SourceLocation {
return s.locs[i]
}
func (s *srcLocs) ByPath(p protoreflect.SourcePath) protoreflect.SourceLocation {
index, ok := s.index[pathKey(p)]
if !ok {
return protoreflect.SourceLocation{}
}
return s.locs[index]
}
func (s *srcLocs) ByDescriptor(d protoreflect.Descriptor) protoreflect.SourceLocation {
if d.ParentFile() != s.file {
return protoreflect.SourceLocation{}
}
path, ok := protointernal.ComputeSourcePath(d)
if !ok {
return protoreflect.SourceLocation{}
}
return s.ByPath(path)
}
type msgDescriptors struct {
protoreflect.MessageDescriptors
msgs []*msgDescriptor
}
func (r *result) createMessages(prefix string, parent protoreflect.Descriptor, msgProtos []*descriptorpb.DescriptorProto) msgDescriptors {
msgs := make([]*msgDescriptor, len(msgProtos))
for i, msgProto := range msgProtos {
msgs[i] = r.createMessageDescriptor(msgProto, parent, i, prefix+msgProto.GetName())
}
return msgDescriptors{msgs: msgs}
}
func (r *result) populateExtensionRefs() {
addExtsByName := func(exts *extDescriptors) {
for _, x := range exts.exts {
if x.field.extendee == nil {
// extension type was not resolved
continue
}
extendee := x.ContainingMessage().FullName()
r.extensionsByMessage[extendee] = append(r.extensionsByMessage[extendee], x)
}
}
var addNestedExtsByName func(msgs *msgDescriptors)
addNestedExtsByName = func(msgs *msgDescriptors) {
for _, m := range msgs.msgs {
addExtsByName(&m.nestedExtensions)
addNestedExtsByName(&m.nestedMessages)
}
}
addExtsByName(&r.extensions)
addNestedExtsByName(&r.messages)
}
func (m *msgDescriptors) Len() int {
return len(m.msgs)
}
func (m *msgDescriptors) Get(i int) protoreflect.MessageDescriptor {
return m.msgs[i]
}
func (m *msgDescriptors) ByName(s protoreflect.Name) protoreflect.MessageDescriptor {
for _, msg := range m.msgs {
if msg.Name() == s {
return msg
}
}
return nil
}
type msgDescriptor struct {
protoreflect.MessageDescriptor
file *result
parent protoreflect.Descriptor
index int
proto *descriptorpb.DescriptorProto
fqn string
fields fldDescriptors
oneofs oneofDescriptors
nestedMessages msgDescriptors
nestedEnums enumDescriptors
nestedExtensions extDescriptors
extRanges fieldRanges
rsvdRanges fieldRanges
rsvdNames names
}
var (
_ protoreflect.MessageDescriptor = (*msgDescriptor)(nil)
_ protoutil.DescriptorProtoWrapper = (*msgDescriptor)(nil)
)
func (r *result) createMessageDescriptor(md *descriptorpb.DescriptorProto, parent protoreflect.Descriptor, index int, fqn string) *msgDescriptor {
ret := &msgDescriptor{MessageDescriptor: noOpMessage, file: r, parent: parent, index: index, proto: md, fqn: fqn}
r.descriptors.Insert(art.Key(fqn), ret)
prefix := fqn + "."
// NB: We MUST create fields before oneofs so that we can populate the
// set of fields that belong to the oneof
ret.fields = r.createFields(prefix, ret, md.Field)
ret.oneofs = r.createOneofs(prefix, ret, md.OneofDecl)
ret.nestedMessages = r.createMessages(prefix, ret, md.NestedType)
ret.nestedEnums = r.createEnums(prefix, ret, md.EnumType)
ret.nestedExtensions = r.createExtensions(prefix, ret, md.Extension)
ret.extRanges = createFieldRanges(md.ExtensionRange)
ret.rsvdRanges = createFieldRanges(md.ReservedRange)
ret.rsvdNames = names{s: md.ReservedName}
return ret
}
func (m *msgDescriptor) MessageDescriptorProto() *descriptorpb.DescriptorProto {
return m.proto
}
func (m *msgDescriptor) AsProto() proto.Message {
return m.proto
}
func (m *msgDescriptor) ParentFile() protoreflect.FileDescriptor {
return m.file
}
func (m *msgDescriptor) Parent() protoreflect.Descriptor {
return m.parent
}
func (m *msgDescriptor) Index() int {
return m.index
}
func (m *msgDescriptor) Syntax() protoreflect.Syntax {
return m.file.Syntax()
}
func (m *msgDescriptor) Name() protoreflect.Name {
return protoreflect.Name(m.proto.GetName())
}
func (m *msgDescriptor) FullName() protoreflect.FullName {
return protoreflect.FullName(m.fqn)
}
func (m *msgDescriptor) IsPlaceholder() bool {
return false
}
func (m *msgDescriptor) Options() protoreflect.ProtoMessage {
return m.proto.Options
}
func (m *msgDescriptor) IsMapEntry() bool {
return m.proto.Options.GetMapEntry()
}
func (m *msgDescriptor) Fields() protoreflect.FieldDescriptors {
return &m.fields
}
func (m *msgDescriptor) Oneofs() protoreflect.OneofDescriptors {
return &m.oneofs
}
func (m *msgDescriptor) ReservedNames() protoreflect.Names {
return m.rsvdNames
}
func (m *msgDescriptor) ReservedRanges() protoreflect.FieldRanges {
return m.rsvdRanges
}
func (m *msgDescriptor) RequiredNumbers() protoreflect.FieldNumbers {
var indexes fieldNums
for _, fld := range m.proto.Field {
if fld.GetLabel() == descriptorpb.FieldDescriptorProto_LABEL_REQUIRED {
indexes.s = append(indexes.s, fld.GetNumber())
}
}
return indexes
}
func (m *msgDescriptor) ExtensionRanges() protoreflect.FieldRanges {
return m.extRanges
}
func (m *msgDescriptor) ExtensionRangeOptions(i int) protoreflect.ProtoMessage {
return m.proto.ExtensionRange[i].Options
}
func (m *msgDescriptor) Enums() protoreflect.EnumDescriptors {
return &m.nestedEnums
}
func (m *msgDescriptor) Messages() protoreflect.MessageDescriptors {
return &m.nestedMessages
}
func (m *msgDescriptor) Extensions() protoreflect.ExtensionDescriptors {
return &m.nestedExtensions
}
type names struct {
protoreflect.Names
s []string
}
func (n names) Len() int {
return len(n.s)
}
func (n names) Get(i int) protoreflect.Name {
return protoreflect.Name(n.s[i])
}
func (n names) Has(s protoreflect.Name) bool {
for _, name := range n.s {
if name == string(s) {
return true
}
}
return false
}
type fieldNums struct {
protoreflect.FieldNumbers
s []int32
}
func (n fieldNums) Len() int {
return len(n.s)
}
func (n fieldNums) Get(i int) protoreflect.FieldNumber {
return protoreflect.FieldNumber(n.s[i])
}
func (n fieldNums) Has(s protoreflect.FieldNumber) bool {
for _, num := range n.s {
if num == int32(s) {
return true
}
}
return false
}
type fieldRanges struct {
protoreflect.FieldRanges
ranges [][2]protoreflect.FieldNumber
}
type fieldRange interface {
GetStart() int32
GetEnd() int32
}
func createFieldRanges[T fieldRange](rangeProtos []T) fieldRanges {
ranges := make([][2]protoreflect.FieldNumber, len(rangeProtos))
for i, r := range rangeProtos {
ranges[i] = [2]protoreflect.FieldNumber{
protoreflect.FieldNumber(r.GetStart()),
protoreflect.FieldNumber(r.GetEnd()),
}
}
return fieldRanges{ranges: ranges}
}
func (f fieldRanges) Len() int {
return len(f.ranges)
}
func (f fieldRanges) Get(i int) [2]protoreflect.FieldNumber {
return f.ranges[i]
}
func (f fieldRanges) Has(n protoreflect.FieldNumber) bool {
for _, r := range f.ranges {
if r[0] <= n && r[1] > n {
return true
}
}
return false
}
type enumDescriptors struct {
protoreflect.EnumDescriptors
enums []*enumDescriptor
}
func (r *result) createEnums(prefix string, parent protoreflect.Descriptor, enumProtos []*descriptorpb.EnumDescriptorProto) enumDescriptors {
enums := make([]*enumDescriptor, len(enumProtos))
for i, enumProto := range enumProtos {
enums[i] = r.createEnumDescriptor(enumProto, parent, i, prefix+enumProto.GetName())
}
return enumDescriptors{enums: enums}
}
func (e *enumDescriptors) Len() int {
return len(e.enums)
}
func (e *enumDescriptors) Get(i int) protoreflect.EnumDescriptor {
return e.enums[i]
}
func (e *enumDescriptors) ByName(s protoreflect.Name) protoreflect.EnumDescriptor {
for _, en := range e.enums {
if en.Name() == s {
return en
}
}
return nil
}
type enumDescriptor struct {
protoreflect.EnumDescriptor
file *result
parent protoreflect.Descriptor
index int
proto *descriptorpb.EnumDescriptorProto
fqn string
values enValDescriptors
rsvdRanges enumRanges
rsvdNames names
}
var (
_ protoreflect.EnumDescriptor = (*enumDescriptor)(nil)
_ protoutil.DescriptorProtoWrapper = (*enumDescriptor)(nil)
)
func (r *result) createEnumDescriptor(ed *descriptorpb.EnumDescriptorProto, parent protoreflect.Descriptor, index int, fqn string) *enumDescriptor {
ret := &enumDescriptor{EnumDescriptor: noOpEnum, file: r, parent: parent, index: index, proto: ed, fqn: fqn}
r.descriptors.Insert(art.Key(fqn), ret)
// Unlike all other elements, the fully-qualified name of enum values
// is NOT scoped to their parent element (the enum), but rather to
// the enum's parent element. This follows C++ scoping rules for
// enum values.
prefix := strings.TrimSuffix(fqn, ed.GetName())
ret.values = r.createEnumValues(prefix, ret, ed.Value)
ret.rsvdRanges = createEnumRanges(ed.ReservedRange)
ret.rsvdNames = names{s: ed.ReservedName}
return ret
}
func (e *enumDescriptor) EnumDescriptorProto() *descriptorpb.EnumDescriptorProto {
return e.proto
}
func (e *enumDescriptor) AsProto() proto.Message {
return e.proto
}
func (e *enumDescriptor) ParentFile() protoreflect.FileDescriptor {
return e.file
}
func (e *enumDescriptor) Parent() protoreflect.Descriptor {
return e.parent
}
func (e *enumDescriptor) Index() int {
return e.index
}
func (e *enumDescriptor) Syntax() protoreflect.Syntax {
return e.file.Syntax()
}
func (e *enumDescriptor) Name() protoreflect.Name {
return protoreflect.Name(e.proto.GetName())
}
func (e *enumDescriptor) FullName() protoreflect.FullName {
return protoreflect.FullName(e.fqn)
}
func (e *enumDescriptor) IsPlaceholder() bool {
return false
}
func (e *enumDescriptor) Options() protoreflect.ProtoMessage {
return e.proto.Options
}
func (e *enumDescriptor) Values() protoreflect.EnumValueDescriptors {
return &e.values
}
func (e *enumDescriptor) ReservedNames() protoreflect.Names {
return e.rsvdNames
}
func (e *enumDescriptor) ReservedRanges() protoreflect.EnumRanges {
return e.rsvdRanges
}
func (e *enumDescriptor) IsClosed() bool {
enumType := resolveFeature(e, enumTypeField)
return descriptorpb.FeatureSet_EnumType(enumType.Enum()) == descriptorpb.FeatureSet_CLOSED
}
type enumRanges struct {
protoreflect.EnumRanges
ranges [][2]protoreflect.EnumNumber
}
func createEnumRanges(rangeProtos []*descriptorpb.EnumDescriptorProto_EnumReservedRange) enumRanges {
ranges := make([][2]protoreflect.EnumNumber, len(rangeProtos))
for i, r := range rangeProtos {
ranges[i] = [2]protoreflect.EnumNumber{
protoreflect.EnumNumber(r.GetStart()),
protoreflect.EnumNumber(r.GetEnd()),
}
}
return enumRanges{ranges: ranges}
}
func (e enumRanges) Len() int {
return len(e.ranges)
}
func (e enumRanges) Get(i int) [2]protoreflect.EnumNumber {
return e.ranges[i]
}
func (e enumRanges) Has(n protoreflect.EnumNumber) bool {
for _, r := range e.ranges {
if r[0] <= n && r[1] >= n {
return true
}
}
return false
}
type enValDescriptors struct {
protoreflect.EnumValueDescriptors
vals []*enValDescriptor
}
func (r *result) createEnumValues(prefix string, parent *enumDescriptor, enValProtos []*descriptorpb.EnumValueDescriptorProto) enValDescriptors {
vals := make([]*enValDescriptor, len(enValProtos))
for i, enValProto := range enValProtos {
vals[i] = r.createEnumValueDescriptor(enValProto, parent, i, prefix+enValProto.GetName())
}
return enValDescriptors{vals: vals}
}
func (e *enValDescriptors) Len() int {
return len(e.vals)
}
func (e *enValDescriptors) Get(i int) protoreflect.EnumValueDescriptor {
return e.vals[i]
}
func (e *enValDescriptors) ByName(s protoreflect.Name) protoreflect.EnumValueDescriptor {
for _, val := range e.vals {
if val.Name() == s {
return val
}
}
return nil
}
func (e *enValDescriptors) ByNumber(n protoreflect.EnumNumber) protoreflect.EnumValueDescriptor {
for _, val := range e.vals {
if val.Number() == n {
return val
}
}
return nil
}
type enValDescriptor struct {
protoreflect.EnumValueDescriptor
file *result
parent *enumDescriptor
index int
proto *descriptorpb.EnumValueDescriptorProto
fqn string
}
var (
_ protoreflect.EnumValueDescriptor = (*enValDescriptor)(nil)
_ protoutil.DescriptorProtoWrapper = (*enValDescriptor)(nil)
)
func (r *result) createEnumValueDescriptor(ed *descriptorpb.EnumValueDescriptorProto, parent *enumDescriptor, index int, fqn string) *enValDescriptor {
ret := &enValDescriptor{EnumValueDescriptor: noOpEnumValue, file: r, parent: parent, index: index, proto: ed, fqn: fqn}