desc_init.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. package protodesc
  5. import (
  6. "google.golang.org/protobuf/internal/errors"
  7. "google.golang.org/protobuf/internal/filedesc"
  8. "google.golang.org/protobuf/internal/strs"
  9. "google.golang.org/protobuf/reflect/protoreflect"
  10. "google.golang.org/protobuf/types/descriptorpb"
  11. )
  12. type descsByName map[protoreflect.FullName]protoreflect.Descriptor
  13. func (r descsByName) initEnumDeclarations(eds []*descriptorpb.EnumDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (es []filedesc.Enum, err error) {
  14. es = make([]filedesc.Enum, len(eds)) // allocate up-front to ensure stable pointers
  15. for i, ed := range eds {
  16. e := &es[i]
  17. e.L2 = new(filedesc.EnumL2)
  18. if e.L0, err = r.makeBase(e, parent, ed.GetName(), i, sb); err != nil {
  19. return nil, err
  20. }
  21. if opts := ed.GetOptions(); opts != nil {
  22. opts = clone(opts).(*descriptorpb.EnumOptions)
  23. e.L2.Options = func() protoreflect.ProtoMessage { return opts }
  24. }
  25. for _, s := range ed.GetReservedName() {
  26. e.L2.ReservedNames.List = append(e.L2.ReservedNames.List, protoreflect.Name(s))
  27. }
  28. for _, rr := range ed.GetReservedRange() {
  29. e.L2.ReservedRanges.List = append(e.L2.ReservedRanges.List, [2]protoreflect.EnumNumber{
  30. protoreflect.EnumNumber(rr.GetStart()),
  31. protoreflect.EnumNumber(rr.GetEnd()),
  32. })
  33. }
  34. if e.L2.Values.List, err = r.initEnumValuesFromDescriptorProto(ed.GetValue(), e, sb); err != nil {
  35. return nil, err
  36. }
  37. }
  38. return es, nil
  39. }
  40. func (r descsByName) initEnumValuesFromDescriptorProto(vds []*descriptorpb.EnumValueDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (vs []filedesc.EnumValue, err error) {
  41. vs = make([]filedesc.EnumValue, len(vds)) // allocate up-front to ensure stable pointers
  42. for i, vd := range vds {
  43. v := &vs[i]
  44. if v.L0, err = r.makeBase(v, parent, vd.GetName(), i, sb); err != nil {
  45. return nil, err
  46. }
  47. if opts := vd.GetOptions(); opts != nil {
  48. opts = clone(opts).(*descriptorpb.EnumValueOptions)
  49. v.L1.Options = func() protoreflect.ProtoMessage { return opts }
  50. }
  51. v.L1.Number = protoreflect.EnumNumber(vd.GetNumber())
  52. }
  53. return vs, nil
  54. }
  55. func (r descsByName) initMessagesDeclarations(mds []*descriptorpb.DescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ms []filedesc.Message, err error) {
  56. ms = make([]filedesc.Message, len(mds)) // allocate up-front to ensure stable pointers
  57. for i, md := range mds {
  58. m := &ms[i]
  59. m.L2 = new(filedesc.MessageL2)
  60. if m.L0, err = r.makeBase(m, parent, md.GetName(), i, sb); err != nil {
  61. return nil, err
  62. }
  63. if opts := md.GetOptions(); opts != nil {
  64. opts = clone(opts).(*descriptorpb.MessageOptions)
  65. m.L2.Options = func() protoreflect.ProtoMessage { return opts }
  66. m.L2.IsMapEntry = opts.GetMapEntry()
  67. m.L2.IsMessageSet = opts.GetMessageSetWireFormat()
  68. }
  69. for _, s := range md.GetReservedName() {
  70. m.L2.ReservedNames.List = append(m.L2.ReservedNames.List, protoreflect.Name(s))
  71. }
  72. for _, rr := range md.GetReservedRange() {
  73. m.L2.ReservedRanges.List = append(m.L2.ReservedRanges.List, [2]protoreflect.FieldNumber{
  74. protoreflect.FieldNumber(rr.GetStart()),
  75. protoreflect.FieldNumber(rr.GetEnd()),
  76. })
  77. }
  78. for _, xr := range md.GetExtensionRange() {
  79. m.L2.ExtensionRanges.List = append(m.L2.ExtensionRanges.List, [2]protoreflect.FieldNumber{
  80. protoreflect.FieldNumber(xr.GetStart()),
  81. protoreflect.FieldNumber(xr.GetEnd()),
  82. })
  83. var optsFunc func() protoreflect.ProtoMessage
  84. if opts := xr.GetOptions(); opts != nil {
  85. opts = clone(opts).(*descriptorpb.ExtensionRangeOptions)
  86. optsFunc = func() protoreflect.ProtoMessage { return opts }
  87. }
  88. m.L2.ExtensionRangeOptions = append(m.L2.ExtensionRangeOptions, optsFunc)
  89. }
  90. if m.L2.Fields.List, err = r.initFieldsFromDescriptorProto(md.GetField(), m, sb); err != nil {
  91. return nil, err
  92. }
  93. if m.L2.Oneofs.List, err = r.initOneofsFromDescriptorProto(md.GetOneofDecl(), m, sb); err != nil {
  94. return nil, err
  95. }
  96. if m.L1.Enums.List, err = r.initEnumDeclarations(md.GetEnumType(), m, sb); err != nil {
  97. return nil, err
  98. }
  99. if m.L1.Messages.List, err = r.initMessagesDeclarations(md.GetNestedType(), m, sb); err != nil {
  100. return nil, err
  101. }
  102. if m.L1.Extensions.List, err = r.initExtensionDeclarations(md.GetExtension(), m, sb); err != nil {
  103. return nil, err
  104. }
  105. }
  106. return ms, nil
  107. }
  108. func (r descsByName) initFieldsFromDescriptorProto(fds []*descriptorpb.FieldDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (fs []filedesc.Field, err error) {
  109. fs = make([]filedesc.Field, len(fds)) // allocate up-front to ensure stable pointers
  110. for i, fd := range fds {
  111. f := &fs[i]
  112. if f.L0, err = r.makeBase(f, parent, fd.GetName(), i, sb); err != nil {
  113. return nil, err
  114. }
  115. if opts := fd.GetOptions(); opts != nil {
  116. opts = clone(opts).(*descriptorpb.FieldOptions)
  117. f.L1.Options = func() protoreflect.ProtoMessage { return opts }
  118. f.L1.IsWeak = opts.GetWeak()
  119. f.L1.HasPacked = opts.Packed != nil
  120. f.L1.IsPacked = opts.GetPacked()
  121. }
  122. f.L1.Number = protoreflect.FieldNumber(fd.GetNumber())
  123. f.L1.Cardinality = protoreflect.Cardinality(fd.GetLabel())
  124. if fd.Type != nil {
  125. f.L1.Kind = protoreflect.Kind(fd.GetType())
  126. }
  127. if fd.JsonName != nil {
  128. f.L1.JSONName = filedesc.JSONName(fd.GetJsonName())
  129. }
  130. }
  131. return fs, nil
  132. }
  133. func (r descsByName) initOneofsFromDescriptorProto(ods []*descriptorpb.OneofDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (os []filedesc.Oneof, err error) {
  134. os = make([]filedesc.Oneof, len(ods)) // allocate up-front to ensure stable pointers
  135. for i, od := range ods {
  136. o := &os[i]
  137. if o.L0, err = r.makeBase(o, parent, od.GetName(), i, sb); err != nil {
  138. return nil, err
  139. }
  140. if opts := od.GetOptions(); opts != nil {
  141. opts = clone(opts).(*descriptorpb.OneofOptions)
  142. o.L1.Options = func() protoreflect.ProtoMessage { return opts }
  143. }
  144. }
  145. return os, nil
  146. }
  147. func (r descsByName) initExtensionDeclarations(xds []*descriptorpb.FieldDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (xs []filedesc.Extension, err error) {
  148. xs = make([]filedesc.Extension, len(xds)) // allocate up-front to ensure stable pointers
  149. for i, xd := range xds {
  150. x := &xs[i]
  151. x.L2 = new(filedesc.ExtensionL2)
  152. if x.L0, err = r.makeBase(x, parent, xd.GetName(), i, sb); err != nil {
  153. return nil, err
  154. }
  155. if opts := xd.GetOptions(); opts != nil {
  156. opts = clone(opts).(*descriptorpb.FieldOptions)
  157. x.L2.Options = func() protoreflect.ProtoMessage { return opts }
  158. x.L2.IsPacked = opts.GetPacked()
  159. }
  160. x.L1.Number = protoreflect.FieldNumber(xd.GetNumber())
  161. x.L1.Cardinality = protoreflect.Cardinality(xd.GetLabel())
  162. if xd.Type != nil {
  163. x.L1.Kind = protoreflect.Kind(xd.GetType())
  164. }
  165. if xd.JsonName != nil {
  166. x.L2.JSONName = filedesc.JSONName(xd.GetJsonName())
  167. }
  168. }
  169. return xs, nil
  170. }
  171. func (r descsByName) initServiceDeclarations(sds []*descriptorpb.ServiceDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ss []filedesc.Service, err error) {
  172. ss = make([]filedesc.Service, len(sds)) // allocate up-front to ensure stable pointers
  173. for i, sd := range sds {
  174. s := &ss[i]
  175. s.L2 = new(filedesc.ServiceL2)
  176. if s.L0, err = r.makeBase(s, parent, sd.GetName(), i, sb); err != nil {
  177. return nil, err
  178. }
  179. if opts := sd.GetOptions(); opts != nil {
  180. opts = clone(opts).(*descriptorpb.ServiceOptions)
  181. s.L2.Options = func() protoreflect.ProtoMessage { return opts }
  182. }
  183. if s.L2.Methods.List, err = r.initMethodsFromDescriptorProto(sd.GetMethod(), s, sb); err != nil {
  184. return nil, err
  185. }
  186. }
  187. return ss, nil
  188. }
  189. func (r descsByName) initMethodsFromDescriptorProto(mds []*descriptorpb.MethodDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ms []filedesc.Method, err error) {
  190. ms = make([]filedesc.Method, len(mds)) // allocate up-front to ensure stable pointers
  191. for i, md := range mds {
  192. m := &ms[i]
  193. if m.L0, err = r.makeBase(m, parent, md.GetName(), i, sb); err != nil {
  194. return nil, err
  195. }
  196. if opts := md.GetOptions(); opts != nil {
  197. opts = clone(opts).(*descriptorpb.MethodOptions)
  198. m.L1.Options = func() protoreflect.ProtoMessage { return opts }
  199. }
  200. m.L1.IsStreamingClient = md.GetClientStreaming()
  201. m.L1.IsStreamingServer = md.GetServerStreaming()
  202. }
  203. return ms, nil
  204. }
  205. func (r descsByName) makeBase(child, parent protoreflect.Descriptor, name string, idx int, sb *strs.Builder) (filedesc.BaseL0, error) {
  206. if !protoreflect.Name(name).IsValid() {
  207. return filedesc.BaseL0{}, errors.New("descriptor %q has an invalid nested name: %q", parent.FullName(), name)
  208. }
  209. // Derive the full name of the child.
  210. // Note that enum values are a sibling to the enum parent in the namespace.
  211. var fullName protoreflect.FullName
  212. if _, ok := parent.(protoreflect.EnumDescriptor); ok {
  213. fullName = sb.AppendFullName(parent.FullName().Parent(), protoreflect.Name(name))
  214. } else {
  215. fullName = sb.AppendFullName(parent.FullName(), protoreflect.Name(name))
  216. }
  217. if _, ok := r[fullName]; ok {
  218. return filedesc.BaseL0{}, errors.New("descriptor %q already declared", fullName)
  219. }
  220. r[fullName] = child
  221. // TODO: Verify that the full name does not already exist in the resolver?
  222. // This is not as critical since most usages of NewFile will register
  223. // the created file back into the registry, which will perform this check.
  224. return filedesc.BaseL0{
  225. FullName: fullName,
  226. ParentFile: parent.ParentFile().(*filedesc.File),
  227. Parent: parent,
  228. Index: idx,
  229. }, nil
  230. }