1
2
3
4
5 package protodesc
6
7 import (
8 "google.golang.org/protobuf/internal/errors"
9 "google.golang.org/protobuf/internal/filedesc"
10 "google.golang.org/protobuf/internal/strs"
11 "google.golang.org/protobuf/proto"
12 "google.golang.org/protobuf/reflect/protoreflect"
13
14 "google.golang.org/protobuf/types/descriptorpb"
15 )
16
17 type descsByName map[protoreflect.FullName]protoreflect.Descriptor
18
19 func (r descsByName) initEnumDeclarations(eds []*descriptorpb.EnumDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (es []filedesc.Enum, err error) {
20 es = make([]filedesc.Enum, len(eds))
21 for i, ed := range eds {
22 e := &es[i]
23 e.L2 = new(filedesc.EnumL2)
24 if e.L0, err = r.makeBase(e, parent, ed.GetName(), i, sb); err != nil {
25 return nil, err
26 }
27 if opts := ed.GetOptions(); opts != nil {
28 opts = proto.Clone(opts).(*descriptorpb.EnumOptions)
29 e.L2.Options = func() protoreflect.ProtoMessage { return opts }
30 }
31 for _, s := range ed.GetReservedName() {
32 e.L2.ReservedNames.List = append(e.L2.ReservedNames.List, protoreflect.Name(s))
33 }
34 for _, rr := range ed.GetReservedRange() {
35 e.L2.ReservedRanges.List = append(e.L2.ReservedRanges.List, [2]protoreflect.EnumNumber{
36 protoreflect.EnumNumber(rr.GetStart()),
37 protoreflect.EnumNumber(rr.GetEnd()),
38 })
39 }
40 if e.L2.Values.List, err = r.initEnumValuesFromDescriptorProto(ed.GetValue(), e, sb); err != nil {
41 return nil, err
42 }
43 }
44 return es, nil
45 }
46
47 func (r descsByName) initEnumValuesFromDescriptorProto(vds []*descriptorpb.EnumValueDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (vs []filedesc.EnumValue, err error) {
48 vs = make([]filedesc.EnumValue, len(vds))
49 for i, vd := range vds {
50 v := &vs[i]
51 if v.L0, err = r.makeBase(v, parent, vd.GetName(), i, sb); err != nil {
52 return nil, err
53 }
54 if opts := vd.GetOptions(); opts != nil {
55 opts = proto.Clone(opts).(*descriptorpb.EnumValueOptions)
56 v.L1.Options = func() protoreflect.ProtoMessage { return opts }
57 }
58 v.L1.Number = protoreflect.EnumNumber(vd.GetNumber())
59 }
60 return vs, nil
61 }
62
63 func (r descsByName) initMessagesDeclarations(mds []*descriptorpb.DescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ms []filedesc.Message, err error) {
64 ms = make([]filedesc.Message, len(mds))
65 for i, md := range mds {
66 m := &ms[i]
67 m.L2 = new(filedesc.MessageL2)
68 if m.L0, err = r.makeBase(m, parent, md.GetName(), i, sb); err != nil {
69 return nil, err
70 }
71 if opts := md.GetOptions(); opts != nil {
72 opts = proto.Clone(opts).(*descriptorpb.MessageOptions)
73 m.L2.Options = func() protoreflect.ProtoMessage { return opts }
74 m.L1.IsMapEntry = opts.GetMapEntry()
75 m.L1.IsMessageSet = opts.GetMessageSetWireFormat()
76 }
77 for _, s := range md.GetReservedName() {
78 m.L2.ReservedNames.List = append(m.L2.ReservedNames.List, protoreflect.Name(s))
79 }
80 for _, rr := range md.GetReservedRange() {
81 m.L2.ReservedRanges.List = append(m.L2.ReservedRanges.List, [2]protoreflect.FieldNumber{
82 protoreflect.FieldNumber(rr.GetStart()),
83 protoreflect.FieldNumber(rr.GetEnd()),
84 })
85 }
86 for _, xr := range md.GetExtensionRange() {
87 m.L2.ExtensionRanges.List = append(m.L2.ExtensionRanges.List, [2]protoreflect.FieldNumber{
88 protoreflect.FieldNumber(xr.GetStart()),
89 protoreflect.FieldNumber(xr.GetEnd()),
90 })
91 var optsFunc func() protoreflect.ProtoMessage
92 if opts := xr.GetOptions(); opts != nil {
93 opts = proto.Clone(opts).(*descriptorpb.ExtensionRangeOptions)
94 optsFunc = func() protoreflect.ProtoMessage { return opts }
95 }
96 m.L2.ExtensionRangeOptions = append(m.L2.ExtensionRangeOptions, optsFunc)
97 }
98 if m.L2.Fields.List, err = r.initFieldsFromDescriptorProto(md.GetField(), m, sb); err != nil {
99 return nil, err
100 }
101 if m.L2.Oneofs.List, err = r.initOneofsFromDescriptorProto(md.GetOneofDecl(), m, sb); err != nil {
102 return nil, err
103 }
104 if m.L1.Enums.List, err = r.initEnumDeclarations(md.GetEnumType(), m, sb); err != nil {
105 return nil, err
106 }
107 if m.L1.Messages.List, err = r.initMessagesDeclarations(md.GetNestedType(), m, sb); err != nil {
108 return nil, err
109 }
110 if m.L1.Extensions.List, err = r.initExtensionDeclarations(md.GetExtension(), m, sb); err != nil {
111 return nil, err
112 }
113 }
114 return ms, nil
115 }
116
117 func (r descsByName) initFieldsFromDescriptorProto(fds []*descriptorpb.FieldDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (fs []filedesc.Field, err error) {
118 fs = make([]filedesc.Field, len(fds))
119 for i, fd := range fds {
120 f := &fs[i]
121 if f.L0, err = r.makeBase(f, parent, fd.GetName(), i, sb); err != nil {
122 return nil, err
123 }
124 f.L1.IsProto3Optional = fd.GetProto3Optional()
125 if opts := fd.GetOptions(); opts != nil {
126 opts = proto.Clone(opts).(*descriptorpb.FieldOptions)
127 f.L1.Options = func() protoreflect.ProtoMessage { return opts }
128 f.L1.IsWeak = opts.GetWeak()
129 f.L1.HasPacked = opts.Packed != nil
130 f.L1.IsPacked = opts.GetPacked()
131 }
132 f.L1.Number = protoreflect.FieldNumber(fd.GetNumber())
133 f.L1.Cardinality = protoreflect.Cardinality(fd.GetLabel())
134 if fd.Type != nil {
135 f.L1.Kind = protoreflect.Kind(fd.GetType())
136 }
137 if fd.JsonName != nil {
138 f.L1.StringName.InitJSON(fd.GetJsonName())
139 }
140
141 if f.Base.L0.ParentFile.Syntax() == protoreflect.Editions {
142 f.L1.Presence = resolveFeatureHasFieldPresence(f.Base.L0.ParentFile, fd)
143
144
145 if fd.GetLabel() == descriptorpb.FieldDescriptorProto_LABEL_REPEATED {
146 f.L1.HasPacked = true
147 f.L1.IsPacked = resolveFeatureRepeatedFieldEncodingPacked(f.Base.L0.ParentFile, fd)
148 }
149
150
151
152
153
154
155
156
157 f.L1.HasEnforceUTF8 = true
158 f.L1.EnforceUTF8 = resolveFeatureEnforceUTF8(f.Base.L0.ParentFile, fd)
159
160 if f.L1.Kind == protoreflect.MessageKind && resolveFeatureDelimitedEncoding(f.Base.L0.ParentFile, fd) {
161 f.L1.Kind = protoreflect.GroupKind
162 }
163 }
164 }
165 return fs, nil
166 }
167
168 func (r descsByName) initOneofsFromDescriptorProto(ods []*descriptorpb.OneofDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (os []filedesc.Oneof, err error) {
169 os = make([]filedesc.Oneof, len(ods))
170 for i, od := range ods {
171 o := &os[i]
172 if o.L0, err = r.makeBase(o, parent, od.GetName(), i, sb); err != nil {
173 return nil, err
174 }
175 if opts := od.GetOptions(); opts != nil {
176 opts = proto.Clone(opts).(*descriptorpb.OneofOptions)
177 o.L1.Options = func() protoreflect.ProtoMessage { return opts }
178 }
179 }
180 return os, nil
181 }
182
183 func (r descsByName) initExtensionDeclarations(xds []*descriptorpb.FieldDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (xs []filedesc.Extension, err error) {
184 xs = make([]filedesc.Extension, len(xds))
185 for i, xd := range xds {
186 x := &xs[i]
187 x.L2 = new(filedesc.ExtensionL2)
188 if x.L0, err = r.makeBase(x, parent, xd.GetName(), i, sb); err != nil {
189 return nil, err
190 }
191 if opts := xd.GetOptions(); opts != nil {
192 opts = proto.Clone(opts).(*descriptorpb.FieldOptions)
193 x.L2.Options = func() protoreflect.ProtoMessage { return opts }
194 x.L2.IsPacked = opts.GetPacked()
195 }
196 x.L1.Number = protoreflect.FieldNumber(xd.GetNumber())
197 x.L1.Cardinality = protoreflect.Cardinality(xd.GetLabel())
198 if xd.Type != nil {
199 x.L1.Kind = protoreflect.Kind(xd.GetType())
200 }
201 if xd.JsonName != nil {
202 x.L2.StringName.InitJSON(xd.GetJsonName())
203 }
204 }
205 return xs, nil
206 }
207
208 func (r descsByName) initServiceDeclarations(sds []*descriptorpb.ServiceDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ss []filedesc.Service, err error) {
209 ss = make([]filedesc.Service, len(sds))
210 for i, sd := range sds {
211 s := &ss[i]
212 s.L2 = new(filedesc.ServiceL2)
213 if s.L0, err = r.makeBase(s, parent, sd.GetName(), i, sb); err != nil {
214 return nil, err
215 }
216 if opts := sd.GetOptions(); opts != nil {
217 opts = proto.Clone(opts).(*descriptorpb.ServiceOptions)
218 s.L2.Options = func() protoreflect.ProtoMessage { return opts }
219 }
220 if s.L2.Methods.List, err = r.initMethodsFromDescriptorProto(sd.GetMethod(), s, sb); err != nil {
221 return nil, err
222 }
223 }
224 return ss, nil
225 }
226
227 func (r descsByName) initMethodsFromDescriptorProto(mds []*descriptorpb.MethodDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ms []filedesc.Method, err error) {
228 ms = make([]filedesc.Method, len(mds))
229 for i, md := range mds {
230 m := &ms[i]
231 if m.L0, err = r.makeBase(m, parent, md.GetName(), i, sb); err != nil {
232 return nil, err
233 }
234 if opts := md.GetOptions(); opts != nil {
235 opts = proto.Clone(opts).(*descriptorpb.MethodOptions)
236 m.L1.Options = func() protoreflect.ProtoMessage { return opts }
237 }
238 m.L1.IsStreamingClient = md.GetClientStreaming()
239 m.L1.IsStreamingServer = md.GetServerStreaming()
240 }
241 return ms, nil
242 }
243
244 func (r descsByName) makeBase(child, parent protoreflect.Descriptor, name string, idx int, sb *strs.Builder) (filedesc.BaseL0, error) {
245 if !protoreflect.Name(name).IsValid() {
246 return filedesc.BaseL0{}, errors.New("descriptor %q has an invalid nested name: %q", parent.FullName(), name)
247 }
248
249
250
251 var fullName protoreflect.FullName
252 if _, ok := parent.(protoreflect.EnumDescriptor); ok {
253 fullName = sb.AppendFullName(parent.FullName().Parent(), protoreflect.Name(name))
254 } else {
255 fullName = sb.AppendFullName(parent.FullName(), protoreflect.Name(name))
256 }
257 if _, ok := r[fullName]; ok {
258 return filedesc.BaseL0{}, errors.New("descriptor %q already declared", fullName)
259 }
260 r[fullName] = child
261
262
263
264
265
266 return filedesc.BaseL0{
267 FullName: fullName,
268 ParentFile: parent.ParentFile().(*filedesc.File),
269 Parent: parent,
270 Index: idx,
271 }, nil
272 }
273
View as plain text