desc_lazy.go 22 KB

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