desc_init.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. if m.Base.L0.ParentFile.Syntax() == protoreflect.Editions {
  66. m.L1.EditionFeatures = mergeEditionFeatures(parent, md.GetOptions().GetFeatures())
  67. }
  68. if opts := md.GetOptions(); opts != nil {
  69. opts = proto.Clone(opts).(*descriptorpb.MessageOptions)
  70. m.L2.Options = func() protoreflect.ProtoMessage { return opts }
  71. m.L1.IsMapEntry = opts.GetMapEntry()
  72. m.L1.IsMessageSet = opts.GetMessageSetWireFormat()
  73. }
  74. for _, s := range md.GetReservedName() {
  75. m.L2.ReservedNames.List = append(m.L2.ReservedNames.List, protoreflect.Name(s))
  76. }
  77. for _, rr := range md.GetReservedRange() {
  78. m.L2.ReservedRanges.List = append(m.L2.ReservedRanges.List, [2]protoreflect.FieldNumber{
  79. protoreflect.FieldNumber(rr.GetStart()),
  80. protoreflect.FieldNumber(rr.GetEnd()),
  81. })
  82. }
  83. for _, xr := range md.GetExtensionRange() {
  84. m.L2.ExtensionRanges.List = append(m.L2.ExtensionRanges.List, [2]protoreflect.FieldNumber{
  85. protoreflect.FieldNumber(xr.GetStart()),
  86. protoreflect.FieldNumber(xr.GetEnd()),
  87. })
  88. var optsFunc func() protoreflect.ProtoMessage
  89. if opts := xr.GetOptions(); opts != nil {
  90. opts = proto.Clone(opts).(*descriptorpb.ExtensionRangeOptions)
  91. optsFunc = func() protoreflect.ProtoMessage { return opts }
  92. }
  93. m.L2.ExtensionRangeOptions = append(m.L2.ExtensionRangeOptions, optsFunc)
  94. }
  95. if m.L2.Fields.List, err = r.initFieldsFromDescriptorProto(md.GetField(), m, sb); err != nil {
  96. return nil, err
  97. }
  98. if m.L2.Oneofs.List, err = r.initOneofsFromDescriptorProto(md.GetOneofDecl(), m, sb); err != nil {
  99. return nil, err
  100. }
  101. if m.L1.Enums.List, err = r.initEnumDeclarations(md.GetEnumType(), m, sb); err != nil {
  102. return nil, err
  103. }
  104. if m.L1.Messages.List, err = r.initMessagesDeclarations(md.GetNestedType(), m, sb); err != nil {
  105. return nil, err
  106. }
  107. if m.L1.Extensions.List, err = r.initExtensionDeclarations(md.GetExtension(), m, sb); err != nil {
  108. return nil, err
  109. }
  110. }
  111. return ms, nil
  112. }
  113. // canBePacked returns whether the field can use packed encoding:
  114. // https://protobuf.dev/programming-guides/encoding/#packed
  115. func canBePacked(fd *descriptorpb.FieldDescriptorProto) bool {
  116. if fd.GetLabel() != descriptorpb.FieldDescriptorProto_LABEL_REPEATED {
  117. return false // not a repeated field
  118. }
  119. switch protoreflect.Kind(fd.GetType()) {
  120. case protoreflect.MessageKind, protoreflect.GroupKind:
  121. return false // not a scalar type field
  122. case protoreflect.StringKind, protoreflect.BytesKind:
  123. // string and bytes can explicitly not be declared as packed,
  124. // see https://protobuf.dev/programming-guides/encoding/#packed
  125. return false
  126. default:
  127. return true
  128. }
  129. }
  130. func (r descsByName) initFieldsFromDescriptorProto(fds []*descriptorpb.FieldDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (fs []filedesc.Field, err error) {
  131. fs = make([]filedesc.Field, len(fds)) // allocate up-front to ensure stable pointers
  132. for i, fd := range fds {
  133. f := &fs[i]
  134. if f.L0, err = r.makeBase(f, parent, fd.GetName(), i, sb); err != nil {
  135. return nil, err
  136. }
  137. f.L1.IsProto3Optional = fd.GetProto3Optional()
  138. if opts := fd.GetOptions(); opts != nil {
  139. opts = proto.Clone(opts).(*descriptorpb.FieldOptions)
  140. f.L1.Options = func() protoreflect.ProtoMessage { return opts }
  141. f.L1.IsWeak = opts.GetWeak()
  142. f.L1.HasPacked = opts.Packed != nil
  143. f.L1.IsPacked = opts.GetPacked()
  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.Base.L0.ParentFile.Syntax() == protoreflect.Editions {
  154. f.L1.EditionFeatures = mergeEditionFeatures(parent, fd.GetOptions().GetFeatures())
  155. if f.L1.EditionFeatures.IsLegacyRequired {
  156. f.L1.Cardinality = protoreflect.Required
  157. }
  158. // We reuse the existing field because the old option `[packed =
  159. // true]` is mutually exclusive with the editions feature.
  160. if canBePacked(fd) {
  161. f.L1.HasPacked = true
  162. f.L1.IsPacked = f.L1.EditionFeatures.IsPacked
  163. }
  164. // We pretend this option is always explicitly set because the only
  165. // use of HasEnforceUTF8 is to determine whether to use EnforceUTF8
  166. // or to return the appropriate default.
  167. // When using editions we either parse the option or resolve the
  168. // appropriate default here (instead of later when this option is
  169. // requested from the descriptor).
  170. // In proto2/proto3 syntax HasEnforceUTF8 might be false.
  171. f.L1.HasEnforceUTF8 = true
  172. f.L1.EnforceUTF8 = f.L1.EditionFeatures.IsUTF8Validated
  173. if f.L1.Kind == protoreflect.MessageKind && f.L1.EditionFeatures.IsDelimitedEncoded {
  174. f.L1.Kind = protoreflect.GroupKind
  175. }
  176. }
  177. }
  178. return fs, nil
  179. }
  180. func (r descsByName) initOneofsFromDescriptorProto(ods []*descriptorpb.OneofDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (os []filedesc.Oneof, err error) {
  181. os = make([]filedesc.Oneof, len(ods)) // allocate up-front to ensure stable pointers
  182. for i, od := range ods {
  183. o := &os[i]
  184. if o.L0, err = r.makeBase(o, parent, od.GetName(), i, sb); err != nil {
  185. return nil, err
  186. }
  187. if opts := od.GetOptions(); opts != nil {
  188. opts = proto.Clone(opts).(*descriptorpb.OneofOptions)
  189. o.L1.Options = func() protoreflect.ProtoMessage { return opts }
  190. if parent.Syntax() == protoreflect.Editions {
  191. o.L1.EditionFeatures = mergeEditionFeatures(parent, opts.GetFeatures())
  192. }
  193. }
  194. }
  195. return os, nil
  196. }
  197. func (r descsByName) initExtensionDeclarations(xds []*descriptorpb.FieldDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (xs []filedesc.Extension, err error) {
  198. xs = make([]filedesc.Extension, len(xds)) // allocate up-front to ensure stable pointers
  199. for i, xd := range xds {
  200. x := &xs[i]
  201. x.L2 = new(filedesc.ExtensionL2)
  202. if x.L0, err = r.makeBase(x, parent, xd.GetName(), i, sb); err != nil {
  203. return nil, err
  204. }
  205. if opts := xd.GetOptions(); opts != nil {
  206. opts = proto.Clone(opts).(*descriptorpb.FieldOptions)
  207. x.L2.Options = func() protoreflect.ProtoMessage { return opts }
  208. x.L2.IsPacked = opts.GetPacked()
  209. }
  210. x.L1.Number = protoreflect.FieldNumber(xd.GetNumber())
  211. x.L1.Cardinality = protoreflect.Cardinality(xd.GetLabel())
  212. if xd.Type != nil {
  213. x.L1.Kind = protoreflect.Kind(xd.GetType())
  214. }
  215. if xd.JsonName != nil {
  216. x.L2.StringName.InitJSON(xd.GetJsonName())
  217. }
  218. }
  219. return xs, nil
  220. }
  221. func (r descsByName) initServiceDeclarations(sds []*descriptorpb.ServiceDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ss []filedesc.Service, err error) {
  222. ss = make([]filedesc.Service, len(sds)) // allocate up-front to ensure stable pointers
  223. for i, sd := range sds {
  224. s := &ss[i]
  225. s.L2 = new(filedesc.ServiceL2)
  226. if s.L0, err = r.makeBase(s, parent, sd.GetName(), i, sb); err != nil {
  227. return nil, err
  228. }
  229. if opts := sd.GetOptions(); opts != nil {
  230. opts = proto.Clone(opts).(*descriptorpb.ServiceOptions)
  231. s.L2.Options = func() protoreflect.ProtoMessage { return opts }
  232. }
  233. if s.L2.Methods.List, err = r.initMethodsFromDescriptorProto(sd.GetMethod(), s, sb); err != nil {
  234. return nil, err
  235. }
  236. }
  237. return ss, nil
  238. }
  239. func (r descsByName) initMethodsFromDescriptorProto(mds []*descriptorpb.MethodDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ms []filedesc.Method, err error) {
  240. ms = make([]filedesc.Method, len(mds)) // allocate up-front to ensure stable pointers
  241. for i, md := range mds {
  242. m := &ms[i]
  243. if m.L0, err = r.makeBase(m, parent, md.GetName(), i, sb); err != nil {
  244. return nil, err
  245. }
  246. if opts := md.GetOptions(); opts != nil {
  247. opts = proto.Clone(opts).(*descriptorpb.MethodOptions)
  248. m.L1.Options = func() protoreflect.ProtoMessage { return opts }
  249. }
  250. m.L1.IsStreamingClient = md.GetClientStreaming()
  251. m.L1.IsStreamingServer = md.GetServerStreaming()
  252. }
  253. return ms, nil
  254. }
  255. func (r descsByName) makeBase(child, parent protoreflect.Descriptor, name string, idx int, sb *strs.Builder) (filedesc.BaseL0, error) {
  256. if !protoreflect.Name(name).IsValid() {
  257. return filedesc.BaseL0{}, errors.New("descriptor %q has an invalid nested name: %q", parent.FullName(), name)
  258. }
  259. // Derive the full name of the child.
  260. // Note that enum values are a sibling to the enum parent in the namespace.
  261. var fullName protoreflect.FullName
  262. if _, ok := parent.(protoreflect.EnumDescriptor); ok {
  263. fullName = sb.AppendFullName(parent.FullName().Parent(), protoreflect.Name(name))
  264. } else {
  265. fullName = sb.AppendFullName(parent.FullName(), protoreflect.Name(name))
  266. }
  267. if _, ok := r[fullName]; ok {
  268. return filedesc.BaseL0{}, errors.New("descriptor %q already declared", fullName)
  269. }
  270. r[fullName] = child
  271. // TODO: Verify that the full name does not already exist in the resolver?
  272. // This is not as critical since most usages of NewFile will register
  273. // the created file back into the registry, which will perform this check.
  274. return filedesc.BaseL0{
  275. FullName: fullName,
  276. ParentFile: parent.ParentFile().(*filedesc.File),
  277. Parent: parent,
  278. Index: idx,
  279. }, nil
  280. }