1
2
3
4
5 package protodesc
6
7 import (
8 "fmt"
9 "strings"
10
11 "google.golang.org/protobuf/internal/encoding/defval"
12 "google.golang.org/protobuf/internal/strs"
13 "google.golang.org/protobuf/proto"
14 "google.golang.org/protobuf/reflect/protoreflect"
15
16 "google.golang.org/protobuf/types/descriptorpb"
17 )
18
19
20
21 func ToFileDescriptorProto(file protoreflect.FileDescriptor) *descriptorpb.FileDescriptorProto {
22 p := &descriptorpb.FileDescriptorProto{
23 Name: proto.String(file.Path()),
24 Options: proto.Clone(file.Options()).(*descriptorpb.FileOptions),
25 }
26 if file.Package() != "" {
27 p.Package = proto.String(string(file.Package()))
28 }
29 for i, imports := 0, file.Imports(); i < imports.Len(); i++ {
30 imp := imports.Get(i)
31 p.Dependency = append(p.Dependency, imp.Path())
32 if imp.IsPublic {
33 p.PublicDependency = append(p.PublicDependency, int32(i))
34 }
35 if imp.IsWeak {
36 p.WeakDependency = append(p.WeakDependency, int32(i))
37 }
38 }
39 for i, locs := 0, file.SourceLocations(); i < locs.Len(); i++ {
40 loc := locs.Get(i)
41 l := &descriptorpb.SourceCodeInfo_Location{}
42 l.Path = append(l.Path, loc.Path...)
43 if loc.StartLine == loc.EndLine {
44 l.Span = []int32{int32(loc.StartLine), int32(loc.StartColumn), int32(loc.EndColumn)}
45 } else {
46 l.Span = []int32{int32(loc.StartLine), int32(loc.StartColumn), int32(loc.EndLine), int32(loc.EndColumn)}
47 }
48 l.LeadingDetachedComments = append([]string(nil), loc.LeadingDetachedComments...)
49 if loc.LeadingComments != "" {
50 l.LeadingComments = proto.String(loc.LeadingComments)
51 }
52 if loc.TrailingComments != "" {
53 l.TrailingComments = proto.String(loc.TrailingComments)
54 }
55 if p.SourceCodeInfo == nil {
56 p.SourceCodeInfo = &descriptorpb.SourceCodeInfo{}
57 }
58 p.SourceCodeInfo.Location = append(p.SourceCodeInfo.Location, l)
59
60 }
61 for i, messages := 0, file.Messages(); i < messages.Len(); i++ {
62 p.MessageType = append(p.MessageType, ToDescriptorProto(messages.Get(i)))
63 }
64 for i, enums := 0, file.Enums(); i < enums.Len(); i++ {
65 p.EnumType = append(p.EnumType, ToEnumDescriptorProto(enums.Get(i)))
66 }
67 for i, services := 0, file.Services(); i < services.Len(); i++ {
68 p.Service = append(p.Service, ToServiceDescriptorProto(services.Get(i)))
69 }
70 for i, exts := 0, file.Extensions(); i < exts.Len(); i++ {
71 p.Extension = append(p.Extension, ToFieldDescriptorProto(exts.Get(i)))
72 }
73 if syntax := file.Syntax(); syntax != protoreflect.Proto2 && syntax.IsValid() {
74 p.Syntax = proto.String(file.Syntax().String())
75 }
76 if file.Syntax() == protoreflect.Editions {
77 desc := file
78 if fileImportDesc, ok := file.(protoreflect.FileImport); ok {
79 desc = fileImportDesc.FileDescriptor
80 }
81
82 if editionsInterface, ok := desc.(interface{ Edition() int32 }); ok {
83 p.Edition = descriptorpb.Edition(editionsInterface.Edition()).Enum()
84 }
85 }
86 return p
87 }
88
89
90
91 func ToDescriptorProto(message protoreflect.MessageDescriptor) *descriptorpb.DescriptorProto {
92 p := &descriptorpb.DescriptorProto{
93 Name: proto.String(string(message.Name())),
94 Options: proto.Clone(message.Options()).(*descriptorpb.MessageOptions),
95 }
96 for i, fields := 0, message.Fields(); i < fields.Len(); i++ {
97 p.Field = append(p.Field, ToFieldDescriptorProto(fields.Get(i)))
98 }
99 for i, exts := 0, message.Extensions(); i < exts.Len(); i++ {
100 p.Extension = append(p.Extension, ToFieldDescriptorProto(exts.Get(i)))
101 }
102 for i, messages := 0, message.Messages(); i < messages.Len(); i++ {
103 p.NestedType = append(p.NestedType, ToDescriptorProto(messages.Get(i)))
104 }
105 for i, enums := 0, message.Enums(); i < enums.Len(); i++ {
106 p.EnumType = append(p.EnumType, ToEnumDescriptorProto(enums.Get(i)))
107 }
108 for i, xranges := 0, message.ExtensionRanges(); i < xranges.Len(); i++ {
109 xrange := xranges.Get(i)
110 p.ExtensionRange = append(p.ExtensionRange, &descriptorpb.DescriptorProto_ExtensionRange{
111 Start: proto.Int32(int32(xrange[0])),
112 End: proto.Int32(int32(xrange[1])),
113 Options: proto.Clone(message.ExtensionRangeOptions(i)).(*descriptorpb.ExtensionRangeOptions),
114 })
115 }
116 for i, oneofs := 0, message.Oneofs(); i < oneofs.Len(); i++ {
117 p.OneofDecl = append(p.OneofDecl, ToOneofDescriptorProto(oneofs.Get(i)))
118 }
119 for i, ranges := 0, message.ReservedRanges(); i < ranges.Len(); i++ {
120 rrange := ranges.Get(i)
121 p.ReservedRange = append(p.ReservedRange, &descriptorpb.DescriptorProto_ReservedRange{
122 Start: proto.Int32(int32(rrange[0])),
123 End: proto.Int32(int32(rrange[1])),
124 })
125 }
126 for i, names := 0, message.ReservedNames(); i < names.Len(); i++ {
127 p.ReservedName = append(p.ReservedName, string(names.Get(i)))
128 }
129 return p
130 }
131
132
133
134 func ToFieldDescriptorProto(field protoreflect.FieldDescriptor) *descriptorpb.FieldDescriptorProto {
135 p := &descriptorpb.FieldDescriptorProto{
136 Name: proto.String(string(field.Name())),
137 Number: proto.Int32(int32(field.Number())),
138 Label: descriptorpb.FieldDescriptorProto_Label(field.Cardinality()).Enum(),
139 Options: proto.Clone(field.Options()).(*descriptorpb.FieldOptions),
140 }
141 if field.IsExtension() {
142 p.Extendee = fullNameOf(field.ContainingMessage())
143 }
144 if field.Kind().IsValid() {
145 p.Type = descriptorpb.FieldDescriptorProto_Type(field.Kind()).Enum()
146 }
147 if field.Enum() != nil {
148 p.TypeName = fullNameOf(field.Enum())
149 }
150 if field.Message() != nil {
151 p.TypeName = fullNameOf(field.Message())
152 }
153 if field.HasJSONName() {
154
155
156
157 if field.IsExtension() {
158 p.JsonName = proto.String(strs.JSONCamelCase(string(field.Name())))
159 } else {
160 p.JsonName = proto.String(field.JSONName())
161 }
162 }
163 if field.Syntax() == protoreflect.Proto3 && field.HasOptionalKeyword() {
164 p.Proto3Optional = proto.Bool(true)
165 }
166 if field.Syntax() == protoreflect.Editions {
167
168
169 if p.GetType() == descriptorpb.FieldDescriptorProto_TYPE_GROUP {
170 p.Type = descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum()
171 }
172
173
174 if p.GetLabel() == descriptorpb.FieldDescriptorProto_LABEL_REQUIRED {
175 p.Label = descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum()
176 }
177 }
178 if field.HasDefault() {
179 def, err := defval.Marshal(field.Default(), field.DefaultEnumValue(), field.Kind(), defval.Descriptor)
180 if err != nil && field.DefaultEnumValue() != nil {
181 def = string(field.DefaultEnumValue().Name())
182 } else if err != nil {
183 panic(fmt.Sprintf("%v: %v", field.FullName(), err))
184 }
185 p.DefaultValue = proto.String(def)
186 }
187 if oneof := field.ContainingOneof(); oneof != nil {
188 p.OneofIndex = proto.Int32(int32(oneof.Index()))
189 }
190 return p
191 }
192
193
194
195 func ToOneofDescriptorProto(oneof protoreflect.OneofDescriptor) *descriptorpb.OneofDescriptorProto {
196 return &descriptorpb.OneofDescriptorProto{
197 Name: proto.String(string(oneof.Name())),
198 Options: proto.Clone(oneof.Options()).(*descriptorpb.OneofOptions),
199 }
200 }
201
202
203
204 func ToEnumDescriptorProto(enum protoreflect.EnumDescriptor) *descriptorpb.EnumDescriptorProto {
205 p := &descriptorpb.EnumDescriptorProto{
206 Name: proto.String(string(enum.Name())),
207 Options: proto.Clone(enum.Options()).(*descriptorpb.EnumOptions),
208 }
209 for i, values := 0, enum.Values(); i < values.Len(); i++ {
210 p.Value = append(p.Value, ToEnumValueDescriptorProto(values.Get(i)))
211 }
212 for i, ranges := 0, enum.ReservedRanges(); i < ranges.Len(); i++ {
213 rrange := ranges.Get(i)
214 p.ReservedRange = append(p.ReservedRange, &descriptorpb.EnumDescriptorProto_EnumReservedRange{
215 Start: proto.Int32(int32(rrange[0])),
216 End: proto.Int32(int32(rrange[1])),
217 })
218 }
219 for i, names := 0, enum.ReservedNames(); i < names.Len(); i++ {
220 p.ReservedName = append(p.ReservedName, string(names.Get(i)))
221 }
222 return p
223 }
224
225
226
227 func ToEnumValueDescriptorProto(value protoreflect.EnumValueDescriptor) *descriptorpb.EnumValueDescriptorProto {
228 return &descriptorpb.EnumValueDescriptorProto{
229 Name: proto.String(string(value.Name())),
230 Number: proto.Int32(int32(value.Number())),
231 Options: proto.Clone(value.Options()).(*descriptorpb.EnumValueOptions),
232 }
233 }
234
235
236
237 func ToServiceDescriptorProto(service protoreflect.ServiceDescriptor) *descriptorpb.ServiceDescriptorProto {
238 p := &descriptorpb.ServiceDescriptorProto{
239 Name: proto.String(string(service.Name())),
240 Options: proto.Clone(service.Options()).(*descriptorpb.ServiceOptions),
241 }
242 for i, methods := 0, service.Methods(); i < methods.Len(); i++ {
243 p.Method = append(p.Method, ToMethodDescriptorProto(methods.Get(i)))
244 }
245 return p
246 }
247
248
249
250 func ToMethodDescriptorProto(method protoreflect.MethodDescriptor) *descriptorpb.MethodDescriptorProto {
251 p := &descriptorpb.MethodDescriptorProto{
252 Name: proto.String(string(method.Name())),
253 InputType: fullNameOf(method.Input()),
254 OutputType: fullNameOf(method.Output()),
255 Options: proto.Clone(method.Options()).(*descriptorpb.MethodOptions),
256 }
257 if method.IsStreamingClient() {
258 p.ClientStreaming = proto.Bool(true)
259 }
260 if method.IsStreamingServer() {
261 p.ServerStreaming = proto.Bool(true)
262 }
263 return p
264 }
265
266 func fullNameOf(d protoreflect.Descriptor) *string {
267 if d == nil {
268 return nil
269 }
270 if strings.HasPrefix(string(d.FullName()), unknownPrefix) {
271 return proto.String(string(d.FullName()[len(unknownPrefix):]))
272 }
273 return proto.String("." + string(d.FullName()))
274 }
275
View as plain text