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