desc_lazy.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. var rawTypeName []byte
  391. var rawOptions []byte
  392. for len(b) > 0 {
  393. num, typ, n := protowire.ConsumeTag(b)
  394. b = b[n:]
  395. switch typ {
  396. case protowire.VarintType:
  397. v, m := protowire.ConsumeVarint(b)
  398. b = b[m:]
  399. switch num {
  400. case genid.FieldDescriptorProto_Number_field_number:
  401. fd.L1.Number = protoreflect.FieldNumber(v)
  402. case genid.FieldDescriptorProto_Label_field_number:
  403. fd.L1.Cardinality = protoreflect.Cardinality(v)
  404. case genid.FieldDescriptorProto_Type_field_number:
  405. fd.L1.Kind = protoreflect.Kind(v)
  406. case genid.FieldDescriptorProto_OneofIndex_field_number:
  407. // In Message.unmarshalFull, we allocate slices for both
  408. // the field and oneof descriptors before unmarshaling either
  409. // of them. This ensures pointers to slice elements are stable.
  410. od := &pd.(*Message).L2.Oneofs.List[v]
  411. od.L1.Fields.List = append(od.L1.Fields.List, fd)
  412. if fd.L1.ContainingOneof != nil {
  413. panic("oneof type already set")
  414. }
  415. fd.L1.ContainingOneof = od
  416. case genid.FieldDescriptorProto_Proto3Optional_field_number:
  417. fd.L1.IsProto3Optional = protowire.DecodeBool(v)
  418. }
  419. case protowire.BytesType:
  420. v, m := protowire.ConsumeBytes(b)
  421. b = b[m:]
  422. switch num {
  423. case genid.FieldDescriptorProto_Name_field_number:
  424. fd.L0.FullName = appendFullName(sb, pd.FullName(), v)
  425. case genid.FieldDescriptorProto_JsonName_field_number:
  426. fd.L1.StringName.InitJSON(sb.MakeString(v))
  427. case genid.FieldDescriptorProto_DefaultValue_field_number:
  428. fd.L1.Default.val = protoreflect.ValueOfBytes(v) // temporarily store as bytes; later resolved in resolveMessages
  429. case genid.FieldDescriptorProto_TypeName_field_number:
  430. rawTypeName = v
  431. case genid.FieldDescriptorProto_Options_field_number:
  432. fd.unmarshalOptions(v)
  433. rawOptions = appendOptions(rawOptions, v)
  434. }
  435. default:
  436. m := protowire.ConsumeFieldValue(num, typ, b)
  437. b = b[m:]
  438. }
  439. }
  440. if rawTypeName != nil {
  441. name := makeFullName(sb, rawTypeName)
  442. switch fd.L1.Kind {
  443. case protoreflect.EnumKind:
  444. fd.L1.Enum = PlaceholderEnum(name)
  445. case protoreflect.MessageKind, protoreflect.GroupKind:
  446. fd.L1.Message = PlaceholderMessage(name)
  447. }
  448. }
  449. fd.L1.Options = pf.builder.optionsUnmarshaler(&descopts.Field, rawOptions)
  450. }
  451. func (fd *Field) unmarshalOptions(b []byte) {
  452. const FieldOptions_EnforceUTF8 = 13
  453. for len(b) > 0 {
  454. num, typ, n := protowire.ConsumeTag(b)
  455. b = b[n:]
  456. switch typ {
  457. case protowire.VarintType:
  458. v, m := protowire.ConsumeVarint(b)
  459. b = b[m:]
  460. switch num {
  461. case genid.FieldOptions_Packed_field_number:
  462. fd.L1.HasPacked = true
  463. fd.L1.IsPacked = protowire.DecodeBool(v)
  464. case genid.FieldOptions_Weak_field_number:
  465. fd.L1.IsWeak = protowire.DecodeBool(v)
  466. case FieldOptions_EnforceUTF8:
  467. fd.L1.HasEnforceUTF8 = true
  468. fd.L1.EnforceUTF8 = protowire.DecodeBool(v)
  469. }
  470. default:
  471. m := protowire.ConsumeFieldValue(num, typ, b)
  472. b = b[m:]
  473. }
  474. }
  475. }
  476. func (od *Oneof) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  477. od.L0.ParentFile = pf
  478. od.L0.Parent = pd
  479. od.L0.Index = i
  480. var rawOptions []byte
  481. for len(b) > 0 {
  482. num, typ, n := protowire.ConsumeTag(b)
  483. b = b[n:]
  484. switch typ {
  485. case protowire.BytesType:
  486. v, m := protowire.ConsumeBytes(b)
  487. b = b[m:]
  488. switch num {
  489. case genid.OneofDescriptorProto_Name_field_number:
  490. od.L0.FullName = appendFullName(sb, pd.FullName(), v)
  491. case genid.OneofDescriptorProto_Options_field_number:
  492. rawOptions = appendOptions(rawOptions, v)
  493. }
  494. default:
  495. m := protowire.ConsumeFieldValue(num, typ, b)
  496. b = b[m:]
  497. }
  498. }
  499. od.L1.Options = pf.builder.optionsUnmarshaler(&descopts.Oneof, rawOptions)
  500. }
  501. func (xd *Extension) unmarshalFull(b []byte, sb *strs.Builder) {
  502. var rawTypeName []byte
  503. var rawOptions []byte
  504. xd.L2 = new(ExtensionL2)
  505. for len(b) > 0 {
  506. num, typ, n := protowire.ConsumeTag(b)
  507. b = b[n:]
  508. switch typ {
  509. case protowire.VarintType:
  510. v, m := protowire.ConsumeVarint(b)
  511. b = b[m:]
  512. switch num {
  513. case genid.FieldDescriptorProto_Proto3Optional_field_number:
  514. xd.L2.IsProto3Optional = protowire.DecodeBool(v)
  515. }
  516. case protowire.BytesType:
  517. v, m := protowire.ConsumeBytes(b)
  518. b = b[m:]
  519. switch num {
  520. case genid.FieldDescriptorProto_JsonName_field_number:
  521. xd.L2.StringName.InitJSON(sb.MakeString(v))
  522. case genid.FieldDescriptorProto_DefaultValue_field_number:
  523. xd.L2.Default.val = protoreflect.ValueOfBytes(v) // temporarily store as bytes; later resolved in resolveExtensions
  524. case genid.FieldDescriptorProto_TypeName_field_number:
  525. rawTypeName = v
  526. case genid.FieldDescriptorProto_Options_field_number:
  527. xd.unmarshalOptions(v)
  528. rawOptions = appendOptions(rawOptions, v)
  529. }
  530. default:
  531. m := protowire.ConsumeFieldValue(num, typ, b)
  532. b = b[m:]
  533. }
  534. }
  535. if rawTypeName != nil {
  536. name := makeFullName(sb, rawTypeName)
  537. switch xd.L1.Kind {
  538. case protoreflect.EnumKind:
  539. xd.L2.Enum = PlaceholderEnum(name)
  540. case protoreflect.MessageKind, protoreflect.GroupKind:
  541. xd.L2.Message = PlaceholderMessage(name)
  542. }
  543. }
  544. xd.L2.Options = xd.L0.ParentFile.builder.optionsUnmarshaler(&descopts.Field, rawOptions)
  545. }
  546. func (xd *Extension) unmarshalOptions(b []byte) {
  547. for len(b) > 0 {
  548. num, typ, n := protowire.ConsumeTag(b)
  549. b = b[n:]
  550. switch typ {
  551. case protowire.VarintType:
  552. v, m := protowire.ConsumeVarint(b)
  553. b = b[m:]
  554. switch num {
  555. case genid.FieldOptions_Packed_field_number:
  556. xd.L2.IsPacked = protowire.DecodeBool(v)
  557. }
  558. default:
  559. m := protowire.ConsumeFieldValue(num, typ, b)
  560. b = b[m:]
  561. }
  562. }
  563. }
  564. func (sd *Service) unmarshalFull(b []byte, sb *strs.Builder) {
  565. var rawMethods [][]byte
  566. var rawOptions []byte
  567. sd.L2 = new(ServiceL2)
  568. for len(b) > 0 {
  569. num, typ, n := protowire.ConsumeTag(b)
  570. b = b[n:]
  571. switch typ {
  572. case protowire.BytesType:
  573. v, m := protowire.ConsumeBytes(b)
  574. b = b[m:]
  575. switch num {
  576. case genid.ServiceDescriptorProto_Method_field_number:
  577. rawMethods = append(rawMethods, v)
  578. case genid.ServiceDescriptorProto_Options_field_number:
  579. rawOptions = appendOptions(rawOptions, v)
  580. }
  581. default:
  582. m := protowire.ConsumeFieldValue(num, typ, b)
  583. b = b[m:]
  584. }
  585. }
  586. if len(rawMethods) > 0 {
  587. sd.L2.Methods.List = make([]Method, len(rawMethods))
  588. for i, b := range rawMethods {
  589. sd.L2.Methods.List[i].unmarshalFull(b, sb, sd.L0.ParentFile, sd, i)
  590. }
  591. }
  592. sd.L2.Options = sd.L0.ParentFile.builder.optionsUnmarshaler(&descopts.Service, rawOptions)
  593. }
  594. func (md *Method) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  595. md.L0.ParentFile = pf
  596. md.L0.Parent = pd
  597. md.L0.Index = i
  598. var rawOptions []byte
  599. for len(b) > 0 {
  600. num, typ, n := protowire.ConsumeTag(b)
  601. b = b[n:]
  602. switch typ {
  603. case protowire.VarintType:
  604. v, m := protowire.ConsumeVarint(b)
  605. b = b[m:]
  606. switch num {
  607. case genid.MethodDescriptorProto_ClientStreaming_field_number:
  608. md.L1.IsStreamingClient = protowire.DecodeBool(v)
  609. case genid.MethodDescriptorProto_ServerStreaming_field_number:
  610. md.L1.IsStreamingServer = protowire.DecodeBool(v)
  611. }
  612. case protowire.BytesType:
  613. v, m := protowire.ConsumeBytes(b)
  614. b = b[m:]
  615. switch num {
  616. case genid.MethodDescriptorProto_Name_field_number:
  617. md.L0.FullName = appendFullName(sb, pd.FullName(), v)
  618. case genid.MethodDescriptorProto_InputType_field_number:
  619. md.L1.Input = PlaceholderMessage(makeFullName(sb, v))
  620. case genid.MethodDescriptorProto_OutputType_field_number:
  621. md.L1.Output = PlaceholderMessage(makeFullName(sb, v))
  622. case genid.MethodDescriptorProto_Options_field_number:
  623. rawOptions = appendOptions(rawOptions, v)
  624. }
  625. default:
  626. m := protowire.ConsumeFieldValue(num, typ, b)
  627. b = b[m:]
  628. }
  629. }
  630. md.L1.Options = pf.builder.optionsUnmarshaler(&descopts.Method, rawOptions)
  631. }
  632. // appendOptions appends src to dst, where the returned slice is never nil.
  633. // This is necessary to distinguish between empty and unpopulated options.
  634. func appendOptions(dst, src []byte) []byte {
  635. if dst == nil {
  636. dst = []byte{}
  637. }
  638. return append(dst, src...)
  639. }
  640. // optionsUnmarshaler constructs a lazy unmarshal function for an options message.
  641. //
  642. // The type of message to unmarshal to is passed as a pointer since the
  643. // vars in descopts may not yet be populated at the time this function is called.
  644. func (db *Builder) optionsUnmarshaler(p *protoreflect.ProtoMessage, b []byte) func() protoreflect.ProtoMessage {
  645. if b == nil {
  646. return nil
  647. }
  648. var opts protoreflect.ProtoMessage
  649. var once sync.Once
  650. return func() protoreflect.ProtoMessage {
  651. once.Do(func() {
  652. if *p == nil {
  653. panic("Descriptor.Options called without importing the descriptor package")
  654. }
  655. opts = reflect.New(reflect.TypeOf(*p).Elem()).Interface().(protoreflect.ProtoMessage)
  656. if err := (proto.UnmarshalOptions{
  657. AllowPartial: true,
  658. Resolver: db.TypeResolver,
  659. }).Unmarshal(b, opts); err != nil {
  660. panic(err)
  661. }
  662. })
  663. return opts
  664. }
  665. }