desc_init.go 11 KB

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