Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 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 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 | // ToFileDescriptorProto copies a protoreflect.FileDescriptor into a |
| 20 | // google.protobuf.FileDescriptorProto message. |
| 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 { |
| 74 | p.Syntax = proto.String(file.Syntax().String()) |
| 75 | } |
| 76 | return p |
| 77 | } |
| 78 | |
| 79 | // ToDescriptorProto copies a protoreflect.MessageDescriptor into a |
| 80 | // google.protobuf.DescriptorProto message. |
| 81 | func ToDescriptorProto(message protoreflect.MessageDescriptor) *descriptorpb.DescriptorProto { |
| 82 | p := &descriptorpb.DescriptorProto{ |
| 83 | Name: proto.String(string(message.Name())), |
| 84 | Options: proto.Clone(message.Options()).(*descriptorpb.MessageOptions), |
| 85 | } |
| 86 | for i, fields := 0, message.Fields(); i < fields.Len(); i++ { |
| 87 | p.Field = append(p.Field, ToFieldDescriptorProto(fields.Get(i))) |
| 88 | } |
| 89 | for i, exts := 0, message.Extensions(); i < exts.Len(); i++ { |
| 90 | p.Extension = append(p.Extension, ToFieldDescriptorProto(exts.Get(i))) |
| 91 | } |
| 92 | for i, messages := 0, message.Messages(); i < messages.Len(); i++ { |
| 93 | p.NestedType = append(p.NestedType, ToDescriptorProto(messages.Get(i))) |
| 94 | } |
| 95 | for i, enums := 0, message.Enums(); i < enums.Len(); i++ { |
| 96 | p.EnumType = append(p.EnumType, ToEnumDescriptorProto(enums.Get(i))) |
| 97 | } |
| 98 | for i, xranges := 0, message.ExtensionRanges(); i < xranges.Len(); i++ { |
| 99 | xrange := xranges.Get(i) |
| 100 | p.ExtensionRange = append(p.ExtensionRange, &descriptorpb.DescriptorProto_ExtensionRange{ |
| 101 | Start: proto.Int32(int32(xrange[0])), |
| 102 | End: proto.Int32(int32(xrange[1])), |
| 103 | Options: proto.Clone(message.ExtensionRangeOptions(i)).(*descriptorpb.ExtensionRangeOptions), |
| 104 | }) |
| 105 | } |
| 106 | for i, oneofs := 0, message.Oneofs(); i < oneofs.Len(); i++ { |
| 107 | p.OneofDecl = append(p.OneofDecl, ToOneofDescriptorProto(oneofs.Get(i))) |
| 108 | } |
| 109 | for i, ranges := 0, message.ReservedRanges(); i < ranges.Len(); i++ { |
| 110 | rrange := ranges.Get(i) |
| 111 | p.ReservedRange = append(p.ReservedRange, &descriptorpb.DescriptorProto_ReservedRange{ |
| 112 | Start: proto.Int32(int32(rrange[0])), |
| 113 | End: proto.Int32(int32(rrange[1])), |
| 114 | }) |
| 115 | } |
| 116 | for i, names := 0, message.ReservedNames(); i < names.Len(); i++ { |
| 117 | p.ReservedName = append(p.ReservedName, string(names.Get(i))) |
| 118 | } |
| 119 | return p |
| 120 | } |
| 121 | |
| 122 | // ToFieldDescriptorProto copies a protoreflect.FieldDescriptor into a |
| 123 | // google.protobuf.FieldDescriptorProto message. |
| 124 | func ToFieldDescriptorProto(field protoreflect.FieldDescriptor) *descriptorpb.FieldDescriptorProto { |
| 125 | p := &descriptorpb.FieldDescriptorProto{ |
| 126 | Name: proto.String(string(field.Name())), |
| 127 | Number: proto.Int32(int32(field.Number())), |
| 128 | Label: descriptorpb.FieldDescriptorProto_Label(field.Cardinality()).Enum(), |
| 129 | Options: proto.Clone(field.Options()).(*descriptorpb.FieldOptions), |
| 130 | } |
| 131 | if field.IsExtension() { |
| 132 | p.Extendee = fullNameOf(field.ContainingMessage()) |
| 133 | } |
| 134 | if field.Kind().IsValid() { |
| 135 | p.Type = descriptorpb.FieldDescriptorProto_Type(field.Kind()).Enum() |
| 136 | } |
| 137 | if field.Enum() != nil { |
| 138 | p.TypeName = fullNameOf(field.Enum()) |
| 139 | } |
| 140 | if field.Message() != nil { |
| 141 | p.TypeName = fullNameOf(field.Message()) |
| 142 | } |
| 143 | if field.HasJSONName() { |
| 144 | // A bug in older versions of protoc would always populate the |
| 145 | // "json_name" option for extensions when it is meaningless. |
| 146 | // When it did so, it would always use the camel-cased field name. |
| 147 | if field.IsExtension() { |
| 148 | p.JsonName = proto.String(strs.JSONCamelCase(string(field.Name()))) |
| 149 | } else { |
| 150 | p.JsonName = proto.String(field.JSONName()) |
| 151 | } |
| 152 | } |
| 153 | if field.Syntax() == protoreflect.Proto3 && field.HasOptionalKeyword() { |
| 154 | p.Proto3Optional = proto.Bool(true) |
| 155 | } |
| 156 | if field.HasDefault() { |
| 157 | def, err := defval.Marshal(field.Default(), field.DefaultEnumValue(), field.Kind(), defval.Descriptor) |
| 158 | if err != nil && field.DefaultEnumValue() != nil { |
| 159 | def = string(field.DefaultEnumValue().Name()) // occurs for unresolved enum values |
| 160 | } else if err != nil { |
| 161 | panic(fmt.Sprintf("%v: %v", field.FullName(), err)) |
| 162 | } |
| 163 | p.DefaultValue = proto.String(def) |
| 164 | } |
| 165 | if oneof := field.ContainingOneof(); oneof != nil { |
| 166 | p.OneofIndex = proto.Int32(int32(oneof.Index())) |
| 167 | } |
| 168 | return p |
| 169 | } |
| 170 | |
| 171 | // ToOneofDescriptorProto copies a protoreflect.OneofDescriptor into a |
| 172 | // google.protobuf.OneofDescriptorProto message. |
| 173 | func ToOneofDescriptorProto(oneof protoreflect.OneofDescriptor) *descriptorpb.OneofDescriptorProto { |
| 174 | return &descriptorpb.OneofDescriptorProto{ |
| 175 | Name: proto.String(string(oneof.Name())), |
| 176 | Options: proto.Clone(oneof.Options()).(*descriptorpb.OneofOptions), |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // ToEnumDescriptorProto copies a protoreflect.EnumDescriptor into a |
| 181 | // google.protobuf.EnumDescriptorProto message. |
| 182 | func ToEnumDescriptorProto(enum protoreflect.EnumDescriptor) *descriptorpb.EnumDescriptorProto { |
| 183 | p := &descriptorpb.EnumDescriptorProto{ |
| 184 | Name: proto.String(string(enum.Name())), |
| 185 | Options: proto.Clone(enum.Options()).(*descriptorpb.EnumOptions), |
| 186 | } |
| 187 | for i, values := 0, enum.Values(); i < values.Len(); i++ { |
| 188 | p.Value = append(p.Value, ToEnumValueDescriptorProto(values.Get(i))) |
| 189 | } |
| 190 | for i, ranges := 0, enum.ReservedRanges(); i < ranges.Len(); i++ { |
| 191 | rrange := ranges.Get(i) |
| 192 | p.ReservedRange = append(p.ReservedRange, &descriptorpb.EnumDescriptorProto_EnumReservedRange{ |
| 193 | Start: proto.Int32(int32(rrange[0])), |
| 194 | End: proto.Int32(int32(rrange[1])), |
| 195 | }) |
| 196 | } |
| 197 | for i, names := 0, enum.ReservedNames(); i < names.Len(); i++ { |
| 198 | p.ReservedName = append(p.ReservedName, string(names.Get(i))) |
| 199 | } |
| 200 | return p |
| 201 | } |
| 202 | |
| 203 | // ToEnumValueDescriptorProto copies a protoreflect.EnumValueDescriptor into a |
| 204 | // google.protobuf.EnumValueDescriptorProto message. |
| 205 | func ToEnumValueDescriptorProto(value protoreflect.EnumValueDescriptor) *descriptorpb.EnumValueDescriptorProto { |
| 206 | return &descriptorpb.EnumValueDescriptorProto{ |
| 207 | Name: proto.String(string(value.Name())), |
| 208 | Number: proto.Int32(int32(value.Number())), |
| 209 | Options: proto.Clone(value.Options()).(*descriptorpb.EnumValueOptions), |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // ToServiceDescriptorProto copies a protoreflect.ServiceDescriptor into a |
| 214 | // google.protobuf.ServiceDescriptorProto message. |
| 215 | func ToServiceDescriptorProto(service protoreflect.ServiceDescriptor) *descriptorpb.ServiceDescriptorProto { |
| 216 | p := &descriptorpb.ServiceDescriptorProto{ |
| 217 | Name: proto.String(string(service.Name())), |
| 218 | Options: proto.Clone(service.Options()).(*descriptorpb.ServiceOptions), |
| 219 | } |
| 220 | for i, methods := 0, service.Methods(); i < methods.Len(); i++ { |
| 221 | p.Method = append(p.Method, ToMethodDescriptorProto(methods.Get(i))) |
| 222 | } |
| 223 | return p |
| 224 | } |
| 225 | |
| 226 | // ToMethodDescriptorProto copies a protoreflect.MethodDescriptor into a |
| 227 | // google.protobuf.MethodDescriptorProto message. |
| 228 | func ToMethodDescriptorProto(method protoreflect.MethodDescriptor) *descriptorpb.MethodDescriptorProto { |
| 229 | p := &descriptorpb.MethodDescriptorProto{ |
| 230 | Name: proto.String(string(method.Name())), |
| 231 | InputType: fullNameOf(method.Input()), |
| 232 | OutputType: fullNameOf(method.Output()), |
| 233 | Options: proto.Clone(method.Options()).(*descriptorpb.MethodOptions), |
| 234 | } |
| 235 | if method.IsStreamingClient() { |
| 236 | p.ClientStreaming = proto.Bool(true) |
| 237 | } |
| 238 | if method.IsStreamingServer() { |
| 239 | p.ServerStreaming = proto.Bool(true) |
| 240 | } |
| 241 | return p |
| 242 | } |
| 243 | |
| 244 | func fullNameOf(d protoreflect.Descriptor) *string { |
| 245 | if d == nil { |
| 246 | return nil |
| 247 | } |
| 248 | if strings.HasPrefix(string(d.FullName()), unknownPrefix) { |
| 249 | return proto.String(string(d.FullName()[len(unknownPrefix):])) |
| 250 | } |
| 251 | return proto.String("." + string(d.FullName())) |
| 252 | } |