desc_lazy.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 filedesc
  5. import (
  6. "reflect"
  7. "sync"
  8. "google.golang.org/protobuf/encoding/protowire"
  9. "google.golang.org/protobuf/internal/descopts"
  10. "google.golang.org/protobuf/internal/genid"
  11. "google.golang.org/protobuf/internal/strs"
  12. "google.golang.org/protobuf/proto"
  13. "google.golang.org/protobuf/reflect/protoreflect"
  14. )
  15. func (fd *File) lazyRawInit() {
  16. fd.unmarshalFull(fd.builder.RawDescriptor)
  17. fd.resolveMessages()
  18. fd.resolveExtensions()
  19. fd.resolveServices()
  20. }
  21. func (file *File) resolveMessages() {
  22. var depIdx int32
  23. for i := range file.allMessages {
  24. md := &file.allMessages[i]
  25. // Resolve message field dependencies.
  26. for j := range md.L2.Fields.List {
  27. fd := &md.L2.Fields.List[j]
  28. // Weak fields are resolved upon actual use.
  29. if fd.L1.IsWeak {
  30. continue
  31. }
  32. // Resolve message field dependency.
  33. switch fd.L1.Kind {
  34. case protoreflect.EnumKind:
  35. fd.L1.Enum = file.resolveEnumDependency(fd.L1.Enum, listFieldDeps, depIdx)
  36. depIdx++
  37. case protoreflect.MessageKind, protoreflect.GroupKind:
  38. fd.L1.Message = file.resolveMessageDependency(fd.L1.Message, listFieldDeps, depIdx)
  39. depIdx++
  40. }
  41. // Default is resolved here since it depends on Enum being resolved.
  42. if v := fd.L1.Default.val; v.IsValid() {
  43. fd.L1.Default = unmarshalDefault(v.Bytes(), fd.L1.Kind, file, fd.L1.Enum)
  44. }
  45. }
  46. }
  47. }
  48. func (file *File) resolveExtensions() {
  49. var depIdx int32
  50. for i := range file.allExtensions {
  51. xd := &file.allExtensions[i]
  52. // Resolve extension field dependency.
  53. switch xd.L1.Kind {
  54. case protoreflect.EnumKind:
  55. xd.L2.Enum = file.resolveEnumDependency(xd.L2.Enum, listExtDeps, depIdx)
  56. depIdx++
  57. case protoreflect.MessageKind, protoreflect.GroupKind:
  58. xd.L2.Message = file.resolveMessageDependency(xd.L2.Message, listExtDeps, depIdx)
  59. depIdx++
  60. }
  61. // Default is resolved here since it depends on Enum being resolved.
  62. if v := xd.L2.Default.val; v.IsValid() {
  63. xd.L2.Default = unmarshalDefault(v.Bytes(), xd.L1.Kind, file, xd.L2.Enum)
  64. }
  65. }
  66. }
  67. func (file *File) resolveServices() {
  68. var depIdx int32
  69. for i := range file.allServices {
  70. sd := &file.allServices[i]
  71. // Resolve method dependencies.
  72. for j := range sd.L2.Methods.List {
  73. md := &sd.L2.Methods.List[j]
  74. md.L1.Input = file.resolveMessageDependency(md.L1.Input, listMethInDeps, depIdx)
  75. md.L1.Output = file.resolveMessageDependency(md.L1.Output, listMethOutDeps, depIdx)
  76. depIdx++
  77. }
  78. }
  79. }
  80. func (file *File) resolveEnumDependency(ed protoreflect.EnumDescriptor, i, j int32) protoreflect.EnumDescriptor {
  81. r := file.builder.FileRegistry
  82. if r, ok := r.(resolverByIndex); ok {
  83. if ed2 := r.FindEnumByIndex(i, j, file.allEnums, file.allMessages); ed2 != nil {
  84. return ed2
  85. }
  86. }
  87. for i := range file.allEnums {
  88. if ed2 := &file.allEnums[i]; ed2.L0.FullName == ed.FullName() {
  89. return ed2
  90. }
  91. }
  92. if d, _ := r.FindDescriptorByName(ed.FullName()); d != nil {
  93. return d.(protoreflect.EnumDescriptor)
  94. }
  95. return ed
  96. }
  97. func (file *File) resolveMessageDependency(md protoreflect.MessageDescriptor, i, j int32) protoreflect.MessageDescriptor {
  98. r := file.builder.FileRegistry
  99. if r, ok := r.(resolverByIndex); ok {
  100. if md2 := r.FindMessageByIndex(i, j, file.allEnums, file.allMessages); md2 != nil {
  101. return md2
  102. }
  103. }
  104. for i := range file.allMessages {
  105. if md2 := &file.allMessages[i]; md2.L0.FullName == md.FullName() {
  106. return md2
  107. }
  108. }
  109. if d, _ := r.FindDescriptorByName(md.FullName()); d != nil {
  110. return d.(protoreflect.MessageDescriptor)
  111. }
  112. return md
  113. }
  114. func (fd *File) unmarshalFull(b []byte) {
  115. sb := getBuilder()
  116. defer putBuilder(sb)
  117. var enumIdx, messageIdx, extensionIdx, serviceIdx int
  118. var rawOptions []byte
  119. fd.L2 = new(FileL2)
  120. for len(b) > 0 {
  121. num, typ, n := protowire.ConsumeTag(b)
  122. b = b[n:]
  123. switch typ {
  124. case protowire.VarintType:
  125. v, m := protowire.ConsumeVarint(b)
  126. b = b[m:]
  127. switch num {
  128. case genid.FileDescriptorProto_PublicDependency_field_number:
  129. fd.L2.Imports[v].IsPublic = true
  130. case genid.FileDescriptorProto_WeakDependency_field_number:
  131. fd.L2.Imports[v].IsWeak = true
  132. }
  133. case protowire.BytesType:
  134. v, m := protowire.ConsumeBytes(b)
  135. b = b[m:]
  136. switch num {
  137. case genid.FileDescriptorProto_Dependency_field_number:
  138. path := sb.MakeString(v)
  139. imp, _ := fd.builder.FileRegistry.FindFileByPath(path)
  140. if imp == nil {
  141. imp = PlaceholderFile(path)
  142. }
  143. fd.L2.Imports = append(fd.L2.Imports, protoreflect.FileImport{FileDescriptor: imp})
  144. case genid.FileDescriptorProto_EnumType_field_number:
  145. fd.L1.Enums.List[enumIdx].unmarshalFull(v, sb)
  146. enumIdx++
  147. case genid.FileDescriptorProto_MessageType_field_number:
  148. fd.L1.Messages.List[messageIdx].unmarshalFull(v, sb)
  149. messageIdx++
  150. case genid.FileDescriptorProto_Extension_field_number:
  151. fd.L1.Extensions.List[extensionIdx].unmarshalFull(v, sb)
  152. extensionIdx++
  153. case genid.FileDescriptorProto_Service_field_number:
  154. fd.L1.Services.List[serviceIdx].unmarshalFull(v, sb)
  155. serviceIdx++
  156. case genid.FileDescriptorProto_Options_field_number:
  157. rawOptions = appendOptions(rawOptions, v)
  158. }
  159. default:
  160. m := protowire.ConsumeFieldValue(num, typ, b)
  161. b = b[m:]
  162. }
  163. }
  164. fd.L2.Options = fd.builder.optionsUnmarshaler(&descopts.File, rawOptions)
  165. }
  166. func (ed *Enum) unmarshalFull(b []byte, sb *strs.Builder) {
  167. var rawValues [][]byte
  168. var rawOptions []byte
  169. if !ed.L1.eagerValues {
  170. ed.L2 = new(EnumL2)
  171. }
  172. for len(b) > 0 {
  173. num, typ, n := protowire.ConsumeTag(b)
  174. b = b[n:]
  175. switch typ {
  176. case protowire.BytesType:
  177. v, m := protowire.ConsumeBytes(b)
  178. b = b[m:]
  179. switch num {
  180. case genid.EnumDescriptorProto_Value_field_number:
  181. rawValues = append(rawValues, v)
  182. case genid.EnumDescriptorProto_ReservedName_field_number:
  183. ed.L2.ReservedNames.List = append(ed.L2.ReservedNames.List, protoreflect.Name(sb.MakeString(v)))
  184. case genid.EnumDescriptorProto_ReservedRange_field_number:
  185. ed.L2.ReservedRanges.List = append(ed.L2.ReservedRanges.List, unmarshalEnumReservedRange(v))
  186. case genid.EnumDescriptorProto_Options_field_number:
  187. rawOptions = appendOptions(rawOptions, v)
  188. }
  189. default:
  190. m := protowire.ConsumeFieldValue(num, typ, b)
  191. b = b[m:]
  192. }
  193. }
  194. if !ed.L1.eagerValues && len(rawValues) > 0 {
  195. ed.L2.Values.List = make([]EnumValue, len(rawValues))
  196. for i, b := range rawValues {
  197. ed.L2.Values.List[i].unmarshalFull(b, sb, ed.L0.ParentFile, ed, i)
  198. }
  199. }
  200. ed.L2.Options = ed.L0.ParentFile.builder.optionsUnmarshaler(&descopts.Enum, rawOptions)
  201. }
  202. func unmarshalEnumReservedRange(b []byte) (r [2]protoreflect.EnumNumber) {
  203. for len(b) > 0 {
  204. num, typ, n := protowire.ConsumeTag(b)
  205. b = b[n:]
  206. switch typ {
  207. case protowire.VarintType:
  208. v, m := protowire.ConsumeVarint(b)
  209. b = b[m:]
  210. switch num {
  211. case genid.EnumDescriptorProto_EnumReservedRange_Start_field_number:
  212. r[0] = protoreflect.EnumNumber(v)
  213. case genid.EnumDescriptorProto_EnumReservedRange_End_field_number:
  214. r[1] = protoreflect.EnumNumber(v)
  215. }
  216. default:
  217. m := protowire.ConsumeFieldValue(num, typ, b)
  218. b = b[m:]
  219. }
  220. }
  221. return r
  222. }
  223. func (vd *EnumValue) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  224. vd.L0.ParentFile = pf
  225. vd.L0.Parent = pd
  226. vd.L0.Index = i
  227. var rawOptions []byte
  228. for len(b) > 0 {
  229. num, typ, n := protowire.ConsumeTag(b)
  230. b = b[n:]
  231. switch typ {
  232. case protowire.VarintType:
  233. v, m := protowire.ConsumeVarint(b)
  234. b = b[m:]
  235. switch num {
  236. case genid.EnumValueDescriptorProto_Number_field_number:
  237. vd.L1.Number = protoreflect.EnumNumber(v)
  238. }
  239. case protowire.BytesType:
  240. v, m := protowire.ConsumeBytes(b)
  241. b = b[m:]
  242. switch num {
  243. case genid.EnumValueDescriptorProto_Name_field_number:
  244. // NOTE: Enum values are in the same scope as the enum parent.
  245. vd.L0.FullName = appendFullName(sb, pd.Parent().FullName(), v)
  246. case genid.EnumValueDescriptorProto_Options_field_number:
  247. rawOptions = appendOptions(rawOptions, v)
  248. }
  249. default:
  250. m := protowire.ConsumeFieldValue(num, typ, b)
  251. b = b[m:]
  252. }
  253. }
  254. vd.L1.Options = pf.builder.optionsUnmarshaler(&descopts.EnumValue, rawOptions)
  255. }
  256. func (md *Message) unmarshalFull(b []byte, sb *strs.Builder) {
  257. var rawFields, rawOneofs [][]byte
  258. var enumIdx, messageIdx, extensionIdx int
  259. var rawOptions []byte
  260. md.L2 = new(MessageL2)
  261. for len(b) > 0 {
  262. num, typ, n := protowire.ConsumeTag(b)
  263. b = b[n:]
  264. switch typ {
  265. case protowire.BytesType:
  266. v, m := protowire.ConsumeBytes(b)
  267. b = b[m:]
  268. switch num {
  269. case genid.DescriptorProto_Field_field_number:
  270. rawFields = append(rawFields, v)
  271. case genid.DescriptorProto_OneofDecl_field_number:
  272. rawOneofs = append(rawOneofs, v)
  273. case genid.DescriptorProto_ReservedName_field_number:
  274. md.L2.ReservedNames.List = append(md.L2.ReservedNames.List, protoreflect.Name(sb.MakeString(v)))
  275. case genid.DescriptorProto_ReservedRange_field_number:
  276. md.L2.ReservedRanges.List = append(md.L2.ReservedRanges.List, unmarshalMessageReservedRange(v))
  277. case genid.DescriptorProto_ExtensionRange_field_number:
  278. r, rawOptions := unmarshalMessageExtensionRange(v)
  279. opts := md.L0.ParentFile.builder.optionsUnmarshaler(&descopts.ExtensionRange, rawOptions)
  280. md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, r)
  281. md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, opts)
  282. case genid.DescriptorProto_EnumType_field_number:
  283. md.L1.Enums.List[enumIdx].unmarshalFull(v, sb)
  284. enumIdx++
  285. case genid.DescriptorProto_NestedType_field_number:
  286. md.L1.Messages.List[messageIdx].unmarshalFull(v, sb)
  287. messageIdx++
  288. case genid.DescriptorProto_Extension_field_number:
  289. md.L1.Extensions.List[extensionIdx].unmarshalFull(v, sb)
  290. extensionIdx++
  291. case genid.DescriptorProto_Options_field_number:
  292. md.unmarshalOptions(v)
  293. rawOptions = appendOptions(rawOptions, v)
  294. }
  295. default:
  296. m := protowire.ConsumeFieldValue(num, typ, b)
  297. b = b[m:]
  298. }
  299. }
  300. if len(rawFields) > 0 || len(rawOneofs) > 0 {
  301. md.L2.Fields.List = make([]Field, len(rawFields))
  302. md.L2.Oneofs.List = make([]Oneof, len(rawOneofs))
  303. for i, b := range rawFields {
  304. fd := &md.L2.Fields.List[i]
  305. fd.unmarshalFull(b, sb, md.L0.ParentFile, md, i)
  306. if fd.L1.Cardinality == protoreflect.Required {
  307. md.L2.RequiredNumbers.List = append(md.L2.RequiredNumbers.List, fd.L1.Number)
  308. }
  309. }
  310. for i, b := range rawOneofs {
  311. od := &md.L2.Oneofs.List[i]
  312. od.unmarshalFull(b, sb, md.L0.ParentFile, md, i)
  313. }
  314. }
  315. md.L2.Options = md.L0.ParentFile.builder.optionsUnmarshaler(&descopts.Message, rawOptions)
  316. }
  317. func (md *Message) unmarshalOptions(b []byte) {
  318. for len(b) > 0 {
  319. num, typ, n := protowire.ConsumeTag(b)
  320. b = b[n:]
  321. switch typ {
  322. case protowire.VarintType:
  323. v, m := protowire.ConsumeVarint(b)
  324. b = b[m:]
  325. switch num {
  326. case genid.MessageOptions_MapEntry_field_number:
  327. md.L1.IsMapEntry = protowire.DecodeBool(v)
  328. case genid.MessageOptions_MessageSetWireFormat_field_number:
  329. md.L1.IsMessageSet = protowire.DecodeBool(v)
  330. }
  331. default:
  332. m := protowire.ConsumeFieldValue(num, typ, b)
  333. b = b[m:]
  334. }
  335. }
  336. }
  337. func unmarshalMessageReservedRange(b []byte) (r [2]protoreflect.FieldNumber) {
  338. for len(b) > 0 {
  339. num, typ, n := protowire.ConsumeTag(b)
  340. b = b[n:]
  341. switch typ {
  342. case protowire.VarintType:
  343. v, m := protowire.ConsumeVarint(b)
  344. b = b[m:]
  345. switch num {
  346. case genid.DescriptorProto_ReservedRange_Start_field_number:
  347. r[0] = protoreflect.FieldNumber(v)
  348. case genid.DescriptorProto_ReservedRange_End_field_number:
  349. r[1] = protoreflect.FieldNumber(v)
  350. }
  351. default:
  352. m := protowire.ConsumeFieldValue(num, typ, b)
  353. b = b[m:]
  354. }
  355. }
  356. return r
  357. }
  358. func unmarshalMessageExtensionRange(b []byte) (r [2]protoreflect.FieldNumber, rawOptions []byte) {
  359. for len(b) > 0 {
  360. num, typ, n := protowire.ConsumeTag(b)
  361. b = b[n:]
  362. switch typ {
  363. case protowire.VarintType:
  364. v, m := protowire.ConsumeVarint(b)
  365. b = b[m:]
  366. switch num {
  367. case genid.DescriptorProto_ExtensionRange_Start_field_number:
  368. r[0] = protoreflect.FieldNumber(v)
  369. case genid.DescriptorProto_ExtensionRange_End_field_number:
  370. r[1] = protoreflect.FieldNumber(v)
  371. }
  372. case protowire.BytesType:
  373. v, m := protowire.ConsumeBytes(b)
  374. b = b[m:]
  375. switch num {
  376. case genid.DescriptorProto_ExtensionRange_Options_field_number:
  377. rawOptions = appendOptions(rawOptions, v)
  378. }
  379. default:
  380. m := protowire.ConsumeFieldValue(num, typ, b)
  381. b = b[m:]
  382. }
  383. }
  384. return r, rawOptions
  385. }
  386. func (fd *Field) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  387. fd.L0.ParentFile = pf
  388. fd.L0.Parent = pd
  389. fd.L0.Index = i
  390. fd.L1.EditionFeatures = featuresFromParentDesc(fd.Parent())
  391. var rawTypeName []byte
  392. var rawOptions []byte
  393. for len(b) > 0 {
  394. num, typ, n := protowire.ConsumeTag(b)
  395. b = b[n:]
  396. switch typ {
  397. case protowire.VarintType:
  398. v, m := protowire.ConsumeVarint(b)
  399. b = b[m:]
  400. switch num {
  401. case genid.FieldDescriptorProto_Number_field_number:
  402. fd.L1.Number = protoreflect.FieldNumber(v)
  403. case genid.FieldDescriptorProto_Label_field_number:
  404. fd.L1.Cardinality = protoreflect.Cardinality(v)
  405. case genid.FieldDescriptorProto_Type_field_number:
  406. fd.L1.Kind = protoreflect.Kind(v)
  407. case genid.FieldDescriptorProto_OneofIndex_field_number:
  408. // In Message.unmarshalFull, we allocate slices for both
  409. // the field and oneof descriptors before unmarshaling either
  410. // of them. This ensures pointers to slice elements are stable.
  411. od := &pd.(*Message).L2.Oneofs.List[v]
  412. od.L1.Fields.List = append(od.L1.Fields.List, fd)
  413. if fd.L1.ContainingOneof != nil {
  414. panic("oneof type already set")
  415. }
  416. fd.L1.ContainingOneof = od
  417. case genid.FieldDescriptorProto_Proto3Optional_field_number:
  418. fd.L1.IsProto3Optional = protowire.DecodeBool(v)
  419. }
  420. case protowire.BytesType:
  421. v, m := protowire.ConsumeBytes(b)
  422. b = b[m:]
  423. switch num {
  424. case genid.FieldDescriptorProto_Name_field_number:
  425. fd.L0.FullName = appendFullName(sb, pd.FullName(), v)
  426. case genid.FieldDescriptorProto_JsonName_field_number:
  427. fd.L1.StringName.InitJSON(sb.MakeString(v))
  428. case genid.FieldDescriptorProto_DefaultValue_field_number:
  429. fd.L1.Default.val = protoreflect.ValueOfBytes(v) // temporarily store as bytes; later resolved in resolveMessages
  430. case genid.FieldDescriptorProto_TypeName_field_number:
  431. rawTypeName = v
  432. case genid.FieldDescriptorProto_Options_field_number:
  433. fd.unmarshalOptions(v)
  434. rawOptions = appendOptions(rawOptions, v)
  435. }
  436. default:
  437. m := protowire.ConsumeFieldValue(num, typ, b)
  438. b = b[m:]
  439. }
  440. }
  441. if fd.Syntax() == protoreflect.Editions && fd.L1.Kind == protoreflect.MessageKind && fd.L1.EditionFeatures.IsDelimitedEncoded {
  442. fd.L1.Kind = protoreflect.GroupKind
  443. }
  444. if fd.Syntax() == protoreflect.Editions && fd.L1.EditionFeatures.IsLegacyRequired {
  445. fd.L1.Cardinality = protoreflect.Required
  446. }
  447. if rawTypeName != nil {
  448. name := makeFullName(sb, rawTypeName)
  449. switch fd.L1.Kind {
  450. case protoreflect.EnumKind:
  451. fd.L1.Enum = PlaceholderEnum(name)
  452. case protoreflect.MessageKind, protoreflect.GroupKind:
  453. fd.L1.Message = PlaceholderMessage(name)
  454. }
  455. }
  456. fd.L1.Options = pf.builder.optionsUnmarshaler(&descopts.Field, rawOptions)
  457. }
  458. func (fd *Field) unmarshalOptions(b []byte) {
  459. const FieldOptions_EnforceUTF8 = 13
  460. for len(b) > 0 {
  461. num, typ, n := protowire.ConsumeTag(b)
  462. b = b[n:]
  463. switch typ {
  464. case protowire.VarintType:
  465. v, m := protowire.ConsumeVarint(b)
  466. b = b[m:]
  467. switch num {
  468. case genid.FieldOptions_Packed_field_number:
  469. fd.L1.HasPacked = true
  470. fd.L1.IsPacked = protowire.DecodeBool(v)
  471. case genid.FieldOptions_Weak_field_number:
  472. fd.L1.IsWeak = protowire.DecodeBool(v)
  473. case FieldOptions_EnforceUTF8:
  474. fd.L1.HasEnforceUTF8 = true
  475. fd.L1.EnforceUTF8 = protowire.DecodeBool(v)
  476. }
  477. case protowire.BytesType:
  478. v, m := protowire.ConsumeBytes(b)
  479. b = b[m:]
  480. switch num {
  481. case genid.FieldOptions_Features_field_number:
  482. fd.L1.EditionFeatures = unmarshalFeatureSet(v, fd.L1.EditionFeatures)
  483. }
  484. default:
  485. m := protowire.ConsumeFieldValue(num, typ, b)
  486. b = b[m:]
  487. }
  488. }
  489. }
  490. func (od *Oneof) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  491. od.L0.ParentFile = pf
  492. od.L0.Parent = pd
  493. od.L0.Index = i
  494. var rawOptions []byte
  495. for len(b) > 0 {
  496. num, typ, n := protowire.ConsumeTag(b)
  497. b = b[n:]
  498. switch typ {
  499. case protowire.BytesType:
  500. v, m := protowire.ConsumeBytes(b)
  501. b = b[m:]
  502. switch num {
  503. case genid.OneofDescriptorProto_Name_field_number:
  504. od.L0.FullName = appendFullName(sb, pd.FullName(), v)
  505. case genid.OneofDescriptorProto_Options_field_number:
  506. rawOptions = appendOptions(rawOptions, v)
  507. }
  508. default:
  509. m := protowire.ConsumeFieldValue(num, typ, b)
  510. b = b[m:]
  511. }
  512. }
  513. od.L1.Options = pf.builder.optionsUnmarshaler(&descopts.Oneof, rawOptions)
  514. }
  515. func (xd *Extension) unmarshalFull(b []byte, sb *strs.Builder) {
  516. var rawTypeName []byte
  517. var rawOptions []byte
  518. xd.L1.EditionFeatures = featuresFromParentDesc(xd.L1.Extendee)
  519. xd.L2 = new(ExtensionL2)
  520. for len(b) > 0 {
  521. num, typ, n := protowire.ConsumeTag(b)
  522. b = b[n:]
  523. switch typ {
  524. case protowire.VarintType:
  525. v, m := protowire.ConsumeVarint(b)
  526. b = b[m:]
  527. switch num {
  528. case genid.FieldDescriptorProto_Proto3Optional_field_number:
  529. xd.L2.IsProto3Optional = protowire.DecodeBool(v)
  530. }
  531. case protowire.BytesType:
  532. v, m := protowire.ConsumeBytes(b)
  533. b = b[m:]
  534. switch num {
  535. case genid.FieldDescriptorProto_JsonName_field_number:
  536. xd.L2.StringName.InitJSON(sb.MakeString(v))
  537. case genid.FieldDescriptorProto_DefaultValue_field_number:
  538. xd.L2.Default.val = protoreflect.ValueOfBytes(v) // temporarily store as bytes; later resolved in resolveExtensions
  539. case genid.FieldDescriptorProto_TypeName_field_number:
  540. rawTypeName = v
  541. case genid.FieldDescriptorProto_Options_field_number:
  542. xd.unmarshalOptions(v)
  543. rawOptions = appendOptions(rawOptions, v)
  544. }
  545. default:
  546. m := protowire.ConsumeFieldValue(num, typ, b)
  547. b = b[m:]
  548. }
  549. }
  550. if xd.Syntax() == protoreflect.Editions && xd.L1.Kind == protoreflect.MessageKind && xd.L1.EditionFeatures.IsDelimitedEncoded {
  551. xd.L1.Kind = protoreflect.GroupKind
  552. }
  553. if xd.Syntax() == protoreflect.Editions && xd.L1.EditionFeatures.IsLegacyRequired {
  554. xd.L1.Cardinality = protoreflect.Required
  555. }
  556. if rawTypeName != nil {
  557. name := makeFullName(sb, rawTypeName)
  558. switch xd.L1.Kind {
  559. case protoreflect.EnumKind:
  560. xd.L2.Enum = PlaceholderEnum(name)
  561. case protoreflect.MessageKind, protoreflect.GroupKind:
  562. xd.L2.Message = PlaceholderMessage(name)
  563. }
  564. }
  565. xd.L2.Options = xd.L0.ParentFile.builder.optionsUnmarshaler(&descopts.Field, rawOptions)
  566. }
  567. func (xd *Extension) unmarshalOptions(b []byte) {
  568. for len(b) > 0 {
  569. num, typ, n := protowire.ConsumeTag(b)
  570. b = b[n:]
  571. switch typ {
  572. case protowire.VarintType:
  573. v, m := protowire.ConsumeVarint(b)
  574. b = b[m:]
  575. switch num {
  576. case genid.FieldOptions_Packed_field_number:
  577. xd.L2.IsPacked = protowire.DecodeBool(v)
  578. }
  579. case protowire.BytesType:
  580. v, m := protowire.ConsumeBytes(b)
  581. b = b[m:]
  582. switch num {
  583. case genid.FieldOptions_Features_field_number:
  584. xd.L1.EditionFeatures = unmarshalFeatureSet(v, xd.L1.EditionFeatures)
  585. }
  586. default:
  587. m := protowire.ConsumeFieldValue(num, typ, b)
  588. b = b[m:]
  589. }
  590. }
  591. }
  592. func (sd *Service) unmarshalFull(b []byte, sb *strs.Builder) {
  593. var rawMethods [][]byte
  594. var rawOptions []byte
  595. sd.L2 = new(ServiceL2)
  596. for len(b) > 0 {
  597. num, typ, n := protowire.ConsumeTag(b)
  598. b = b[n:]
  599. switch typ {
  600. case protowire.BytesType:
  601. v, m := protowire.ConsumeBytes(b)
  602. b = b[m:]
  603. switch num {
  604. case genid.ServiceDescriptorProto_Method_field_number:
  605. rawMethods = append(rawMethods, v)
  606. case genid.ServiceDescriptorProto_Options_field_number:
  607. rawOptions = appendOptions(rawOptions, v)
  608. }
  609. default:
  610. m := protowire.ConsumeFieldValue(num, typ, b)
  611. b = b[m:]
  612. }
  613. }
  614. if len(rawMethods) > 0 {
  615. sd.L2.Methods.List = make([]Method, len(rawMethods))
  616. for i, b := range rawMethods {
  617. sd.L2.Methods.List[i].unmarshalFull(b, sb, sd.L0.ParentFile, sd, i)
  618. }
  619. }
  620. sd.L2.Options = sd.L0.ParentFile.builder.optionsUnmarshaler(&descopts.Service, rawOptions)
  621. }
  622. func (md *Method) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  623. md.L0.ParentFile = pf
  624. md.L0.Parent = pd
  625. md.L0.Index = i
  626. var rawOptions []byte
  627. for len(b) > 0 {
  628. num, typ, n := protowire.ConsumeTag(b)
  629. b = b[n:]
  630. switch typ {
  631. case protowire.VarintType:
  632. v, m := protowire.ConsumeVarint(b)
  633. b = b[m:]
  634. switch num {
  635. case genid.MethodDescriptorProto_ClientStreaming_field_number:
  636. md.L1.IsStreamingClient = protowire.DecodeBool(v)
  637. case genid.MethodDescriptorProto_ServerStreaming_field_number:
  638. md.L1.IsStreamingServer = protowire.DecodeBool(v)
  639. }
  640. case protowire.BytesType:
  641. v, m := protowire.ConsumeBytes(b)
  642. b = b[m:]
  643. switch num {
  644. case genid.MethodDescriptorProto_Name_field_number:
  645. md.L0.FullName = appendFullName(sb, pd.FullName(), v)
  646. case genid.MethodDescriptorProto_InputType_field_number:
  647. md.L1.Input = PlaceholderMessage(makeFullName(sb, v))
  648. case genid.MethodDescriptorProto_OutputType_field_number:
  649. md.L1.Output = PlaceholderMessage(makeFullName(sb, v))
  650. case genid.MethodDescriptorProto_Options_field_number:
  651. rawOptions = appendOptions(rawOptions, v)
  652. }
  653. default:
  654. m := protowire.ConsumeFieldValue(num, typ, b)
  655. b = b[m:]
  656. }
  657. }
  658. md.L1.Options = pf.builder.optionsUnmarshaler(&descopts.Method, rawOptions)
  659. }
  660. // appendOptions appends src to dst, where the returned slice is never nil.
  661. // This is necessary to distinguish between empty and unpopulated options.
  662. func appendOptions(dst, src []byte) []byte {
  663. if dst == nil {
  664. dst = []byte{}
  665. }
  666. return append(dst, src...)
  667. }
  668. // optionsUnmarshaler constructs a lazy unmarshal function for an options message.
  669. //
  670. // The type of message to unmarshal to is passed as a pointer since the
  671. // vars in descopts may not yet be populated at the time this function is called.
  672. func (db *Builder) optionsUnmarshaler(p *protoreflect.ProtoMessage, b []byte) func() protoreflect.ProtoMessage {
  673. if b == nil {
  674. return nil
  675. }
  676. var opts protoreflect.ProtoMessage
  677. var once sync.Once
  678. return func() protoreflect.ProtoMessage {
  679. once.Do(func() {
  680. if *p == nil {
  681. panic("Descriptor.Options called without importing the descriptor package")
  682. }
  683. opts = reflect.New(reflect.TypeOf(*p).Elem()).Interface().(protoreflect.ProtoMessage)
  684. if err := (proto.UnmarshalOptions{
  685. AllowPartial: true,
  686. Resolver: db.TypeResolver,
  687. }).Unmarshal(b, opts); err != nil {
  688. panic(err)
  689. }
  690. })
  691. return opts
  692. }
  693. }