desc.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2018 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. // Package protodesc provides functionality for converting
  5. // FileDescriptorProto messages to/from protoreflect.FileDescriptor values.
  6. //
  7. // The google.protobuf.FileDescriptorProto is a protobuf message that describes
  8. // the type information for a .proto file in a form that is easily serializable.
  9. // The protoreflect.FileDescriptor is a more structured representation of
  10. // the FileDescriptorProto message where references and remote dependencies
  11. // can be directly followed.
  12. package protodesc
  13. import (
  14. "google.golang.org/protobuf/internal/errors"
  15. "google.golang.org/protobuf/internal/filedesc"
  16. "google.golang.org/protobuf/internal/pragma"
  17. "google.golang.org/protobuf/internal/strs"
  18. "google.golang.org/protobuf/proto"
  19. "google.golang.org/protobuf/reflect/protoreflect"
  20. "google.golang.org/protobuf/reflect/protoregistry"
  21. "google.golang.org/protobuf/types/descriptorpb"
  22. )
  23. // Resolver is the resolver used by NewFile to resolve dependencies.
  24. // The enums and messages provided must belong to some parent file,
  25. // which is also registered.
  26. //
  27. // It is implemented by protoregistry.Files.
  28. type Resolver interface {
  29. FindFileByPath(string) (protoreflect.FileDescriptor, error)
  30. FindDescriptorByName(protoreflect.FullName) (protoreflect.Descriptor, error)
  31. }
  32. // FileOptions configures the construction of file descriptors.
  33. type FileOptions struct {
  34. pragma.NoUnkeyedLiterals
  35. // AllowUnresolvable configures New to permissively allow unresolvable
  36. // file, enum, or message dependencies. Unresolved dependencies are replaced
  37. // by placeholder equivalents.
  38. //
  39. // The following dependencies may be left unresolved:
  40. // • Resolving an imported file.
  41. // • Resolving the type for a message field or extension field.
  42. // If the kind of the field is unknown, then a placeholder is used for both
  43. // the Enum and Message accessors on the protoreflect.FieldDescriptor.
  44. // • Resolving an enum value set as the default for an optional enum field.
  45. // If unresolvable, the protoreflect.FieldDescriptor.Default is set to the
  46. // first value in the associated enum (or zero if the also enum dependency
  47. // is also unresolvable). The protoreflect.FieldDescriptor.DefaultEnumValue
  48. // is populated with a placeholder.
  49. // • Resolving the extended message type for an extension field.
  50. // • Resolving the input or output message type for a service method.
  51. //
  52. // If the unresolved dependency uses a relative name,
  53. // then the placeholder will contain an invalid FullName with a "*." prefix,
  54. // indicating that the starting prefix of the full name is unknown.
  55. AllowUnresolvable bool
  56. }
  57. // NewFile creates a new protoreflect.FileDescriptor from the provided
  58. // file descriptor message. See FileOptions.New for more information.
  59. func NewFile(fd *descriptorpb.FileDescriptorProto, r Resolver) (protoreflect.FileDescriptor, error) {
  60. return FileOptions{}.New(fd, r)
  61. }
  62. // NewFiles creates a new protoregistry.Files from the provided
  63. // FileDescriptorSet message. See FileOptions.NewFiles for more information.
  64. func NewFiles(fd *descriptorpb.FileDescriptorSet) (*protoregistry.Files, error) {
  65. return FileOptions{}.NewFiles(fd)
  66. }
  67. // New creates a new protoreflect.FileDescriptor from the provided
  68. // file descriptor message. The file must represent a valid proto file according
  69. // to protobuf semantics. The returned descriptor is a deep copy of the input.
  70. //
  71. // Any imported files, enum types, or message types referenced in the file are
  72. // resolved using the provided registry. When looking up an import file path,
  73. // the path must be unique. The newly created file descriptor is not registered
  74. // back into the provided file registry.
  75. func (o FileOptions) New(fd *descriptorpb.FileDescriptorProto, r Resolver) (protoreflect.FileDescriptor, error) {
  76. if r == nil {
  77. r = (*protoregistry.Files)(nil) // empty resolver
  78. }
  79. // Handle the file descriptor content.
  80. f := &filedesc.File{L2: &filedesc.FileL2{}}
  81. switch fd.GetSyntax() {
  82. case "proto2", "":
  83. f.L1.Syntax = protoreflect.Proto2
  84. case "proto3":
  85. f.L1.Syntax = protoreflect.Proto3
  86. default:
  87. return nil, errors.New("invalid syntax: %q", fd.GetSyntax())
  88. }
  89. f.L1.Path = fd.GetName()
  90. if f.L1.Path == "" {
  91. return nil, errors.New("file path must be populated")
  92. }
  93. f.L1.Package = protoreflect.FullName(fd.GetPackage())
  94. if !f.L1.Package.IsValid() && f.L1.Package != "" {
  95. return nil, errors.New("invalid package: %q", f.L1.Package)
  96. }
  97. if opts := fd.GetOptions(); opts != nil {
  98. opts = proto.Clone(opts).(*descriptorpb.FileOptions)
  99. f.L2.Options = func() protoreflect.ProtoMessage { return opts }
  100. }
  101. f.L2.Imports = make(filedesc.FileImports, len(fd.GetDependency()))
  102. for _, i := range fd.GetPublicDependency() {
  103. if !(0 <= i && int(i) < len(f.L2.Imports)) || f.L2.Imports[i].IsPublic {
  104. return nil, errors.New("invalid or duplicate public import index: %d", i)
  105. }
  106. f.L2.Imports[i].IsPublic = true
  107. }
  108. for _, i := range fd.GetWeakDependency() {
  109. if !(0 <= i && int(i) < len(f.L2.Imports)) || f.L2.Imports[i].IsWeak {
  110. return nil, errors.New("invalid or duplicate weak import index: %d", i)
  111. }
  112. f.L2.Imports[i].IsWeak = true
  113. }
  114. imps := importSet{f.Path(): true}
  115. for i, path := range fd.GetDependency() {
  116. imp := &f.L2.Imports[i]
  117. f, err := r.FindFileByPath(path)
  118. if err == protoregistry.NotFound && (o.AllowUnresolvable || imp.IsWeak) {
  119. f = filedesc.PlaceholderFile(path)
  120. } else if err != nil {
  121. return nil, errors.New("could not resolve import %q: %v", path, err)
  122. }
  123. imp.FileDescriptor = f
  124. if imps[imp.Path()] {
  125. return nil, errors.New("already imported %q", path)
  126. }
  127. imps[imp.Path()] = true
  128. }
  129. for i := range fd.GetDependency() {
  130. imp := &f.L2.Imports[i]
  131. imps.importPublic(imp.Imports())
  132. }
  133. // Handle source locations.
  134. f.L2.Locations.File = f
  135. for _, loc := range fd.GetSourceCodeInfo().GetLocation() {
  136. var l protoreflect.SourceLocation
  137. // TODO: Validate that the path points to an actual declaration?
  138. l.Path = protoreflect.SourcePath(loc.GetPath())
  139. s := loc.GetSpan()
  140. switch len(s) {
  141. case 3:
  142. l.StartLine, l.StartColumn, l.EndLine, l.EndColumn = int(s[0]), int(s[1]), int(s[0]), int(s[2])
  143. case 4:
  144. l.StartLine, l.StartColumn, l.EndLine, l.EndColumn = int(s[0]), int(s[1]), int(s[2]), int(s[3])
  145. default:
  146. return nil, errors.New("invalid span: %v", s)
  147. }
  148. // TODO: Validate that the span information is sensible?
  149. // See https://github.com/protocolbuffers/protobuf/issues/6378.
  150. if false && (l.EndLine < l.StartLine || l.StartLine < 0 || l.StartColumn < 0 || l.EndColumn < 0 ||
  151. (l.StartLine == l.EndLine && l.EndColumn <= l.StartColumn)) {
  152. return nil, errors.New("invalid span: %v", s)
  153. }
  154. l.LeadingDetachedComments = loc.GetLeadingDetachedComments()
  155. l.LeadingComments = loc.GetLeadingComments()
  156. l.TrailingComments = loc.GetTrailingComments()
  157. f.L2.Locations.List = append(f.L2.Locations.List, l)
  158. }
  159. // Step 1: Allocate and derive the names for all declarations.
  160. // This copies all fields from the descriptor proto except:
  161. // google.protobuf.FieldDescriptorProto.type_name
  162. // google.protobuf.FieldDescriptorProto.default_value
  163. // google.protobuf.FieldDescriptorProto.oneof_index
  164. // google.protobuf.FieldDescriptorProto.extendee
  165. // google.protobuf.MethodDescriptorProto.input
  166. // google.protobuf.MethodDescriptorProto.output
  167. var err error
  168. sb := new(strs.Builder)
  169. r1 := make(descsByName)
  170. if f.L1.Enums.List, err = r1.initEnumDeclarations(fd.GetEnumType(), f, sb); err != nil {
  171. return nil, err
  172. }
  173. if f.L1.Messages.List, err = r1.initMessagesDeclarations(fd.GetMessageType(), f, sb); err != nil {
  174. return nil, err
  175. }
  176. if f.L1.Extensions.List, err = r1.initExtensionDeclarations(fd.GetExtension(), f, sb); err != nil {
  177. return nil, err
  178. }
  179. if f.L1.Services.List, err = r1.initServiceDeclarations(fd.GetService(), f, sb); err != nil {
  180. return nil, err
  181. }
  182. // Step 2: Resolve every dependency reference not handled by step 1.
  183. r2 := &resolver{local: r1, remote: r, imports: imps, allowUnresolvable: o.AllowUnresolvable}
  184. if err := r2.resolveMessageDependencies(f.L1.Messages.List, fd.GetMessageType()); err != nil {
  185. return nil, err
  186. }
  187. if err := r2.resolveExtensionDependencies(f.L1.Extensions.List, fd.GetExtension()); err != nil {
  188. return nil, err
  189. }
  190. if err := r2.resolveServiceDependencies(f.L1.Services.List, fd.GetService()); err != nil {
  191. return nil, err
  192. }
  193. // Step 3: Validate every enum, message, and extension declaration.
  194. if err := validateEnumDeclarations(f.L1.Enums.List, fd.GetEnumType()); err != nil {
  195. return nil, err
  196. }
  197. if err := validateMessageDeclarations(f.L1.Messages.List, fd.GetMessageType()); err != nil {
  198. return nil, err
  199. }
  200. if err := validateExtensionDeclarations(f.L1.Extensions.List, fd.GetExtension()); err != nil {
  201. return nil, err
  202. }
  203. return f, nil
  204. }
  205. type importSet map[string]bool
  206. func (is importSet) importPublic(imps protoreflect.FileImports) {
  207. for i := 0; i < imps.Len(); i++ {
  208. if imp := imps.Get(i); imp.IsPublic {
  209. is[imp.Path()] = true
  210. is.importPublic(imp.Imports())
  211. }
  212. }
  213. }
  214. // NewFiles creates a new protoregistry.Files from the provided
  215. // FileDescriptorSet message. The descriptor set must include only
  216. // valid files according to protobuf semantics. The returned descriptors
  217. // are a deep copy of the input.
  218. func (o FileOptions) NewFiles(fds *descriptorpb.FileDescriptorSet) (*protoregistry.Files, error) {
  219. files := make(map[string]*descriptorpb.FileDescriptorProto)
  220. for _, fd := range fds.File {
  221. if _, ok := files[fd.GetName()]; ok {
  222. return nil, errors.New("file appears multiple times: %q", fd.GetName())
  223. }
  224. files[fd.GetName()] = fd
  225. }
  226. r := &protoregistry.Files{}
  227. for _, fd := range files {
  228. if err := o.addFileDeps(r, fd, files); err != nil {
  229. return nil, err
  230. }
  231. }
  232. return r, nil
  233. }
  234. func (o FileOptions) addFileDeps(r *protoregistry.Files, fd *descriptorpb.FileDescriptorProto, files map[string]*descriptorpb.FileDescriptorProto) error {
  235. // Set the entry to nil while descending into a file's dependencies to detect cycles.
  236. files[fd.GetName()] = nil
  237. for _, dep := range fd.Dependency {
  238. depfd, ok := files[dep]
  239. if depfd == nil {
  240. if ok {
  241. return errors.New("import cycle in file: %q", dep)
  242. }
  243. continue
  244. }
  245. if err := o.addFileDeps(r, depfd, files); err != nil {
  246. return err
  247. }
  248. }
  249. // Delete the entry once dependencies are processed.
  250. delete(files, fd.GetName())
  251. f, err := o.New(fd, r)
  252. if err != nil {
  253. return err
  254. }
  255. return r.RegisterFile(f)
  256. }