desc_lazy.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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/internal/descopts"
  9. "google.golang.org/protobuf/internal/encoding/wire"
  10. "google.golang.org/protobuf/internal/fieldnum"
  11. "google.golang.org/protobuf/internal/strs"
  12. "google.golang.org/protobuf/proto"
  13. pref "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 pref.EnumKind:
  35. fd.L1.Enum = file.resolveEnumDependency(fd.L1.Enum, listFieldDeps, depIdx)
  36. depIdx++
  37. case pref.MessageKind, pref.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 pref.EnumKind:
  55. xd.L2.Enum = file.resolveEnumDependency(xd.L2.Enum, listExtDeps, depIdx)
  56. depIdx++
  57. case pref.MessageKind, pref.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 pref.EnumDescriptor, i, j int32) pref.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.(pref.EnumDescriptor)
  94. }
  95. return ed
  96. }
  97. func (file *File) resolveMessageDependency(md pref.MessageDescriptor, i, j int32) pref.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.(pref.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 := wire.ConsumeTag(b)
  122. b = b[n:]
  123. switch typ {
  124. case wire.VarintType:
  125. v, m := wire.ConsumeVarint(b)
  126. b = b[m:]
  127. switch num {
  128. case fieldnum.FileDescriptorProto_PublicDependency:
  129. fd.L2.Imports[v].IsPublic = true
  130. case fieldnum.FileDescriptorProto_WeakDependency:
  131. fd.L2.Imports[v].IsWeak = true
  132. }
  133. case wire.BytesType:
  134. v, m := wire.ConsumeBytes(b)
  135. b = b[m:]
  136. switch num {
  137. case fieldnum.FileDescriptorProto_Dependency:
  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, pref.FileImport{FileDescriptor: imp})
  144. case fieldnum.FileDescriptorProto_EnumType:
  145. fd.L1.Enums.List[enumIdx].unmarshalFull(v, sb)
  146. enumIdx++
  147. case fieldnum.FileDescriptorProto_MessageType:
  148. fd.L1.Messages.List[messageIdx].unmarshalFull(v, sb)
  149. messageIdx++
  150. case fieldnum.FileDescriptorProto_Extension:
  151. fd.L1.Extensions.List[extensionIdx].unmarshalFull(v, sb)
  152. extensionIdx++
  153. case fieldnum.FileDescriptorProto_Service:
  154. fd.L1.Services.List[serviceIdx].unmarshalFull(v, sb)
  155. serviceIdx++
  156. case fieldnum.FileDescriptorProto_Options:
  157. rawOptions = appendOptions(rawOptions, v)
  158. }
  159. default:
  160. m := wire.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 := wire.ConsumeTag(b)
  174. b = b[n:]
  175. switch typ {
  176. case wire.BytesType:
  177. v, m := wire.ConsumeBytes(b)
  178. b = b[m:]
  179. switch num {
  180. case fieldnum.EnumDescriptorProto_Value:
  181. rawValues = append(rawValues, v)
  182. case fieldnum.EnumDescriptorProto_ReservedName:
  183. ed.L2.ReservedNames.List = append(ed.L2.ReservedNames.List, pref.Name(sb.MakeString(v)))
  184. case fieldnum.EnumDescriptorProto_ReservedRange:
  185. ed.L2.ReservedRanges.List = append(ed.L2.ReservedRanges.List, unmarshalEnumReservedRange(v))
  186. case fieldnum.EnumDescriptorProto_Options:
  187. rawOptions = appendOptions(rawOptions, v)
  188. }
  189. default:
  190. m := wire.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]pref.EnumNumber) {
  203. for len(b) > 0 {
  204. num, typ, n := wire.ConsumeTag(b)
  205. b = b[n:]
  206. switch typ {
  207. case wire.VarintType:
  208. v, m := wire.ConsumeVarint(b)
  209. b = b[m:]
  210. switch num {
  211. case fieldnum.EnumDescriptorProto_EnumReservedRange_Start:
  212. r[0] = pref.EnumNumber(v)
  213. case fieldnum.EnumDescriptorProto_EnumReservedRange_End:
  214. r[1] = pref.EnumNumber(v)
  215. }
  216. default:
  217. m := wire.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 pref.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 := wire.ConsumeTag(b)
  230. b = b[n:]
  231. switch typ {
  232. case wire.VarintType:
  233. v, m := wire.ConsumeVarint(b)
  234. b = b[m:]
  235. switch num {
  236. case fieldnum.EnumValueDescriptorProto_Number:
  237. vd.L1.Number = pref.EnumNumber(v)
  238. }
  239. case wire.BytesType:
  240. v, m := wire.ConsumeBytes(b)
  241. b = b[m:]
  242. switch num {
  243. case fieldnum.EnumValueDescriptorProto_Name:
  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 fieldnum.EnumValueDescriptorProto_Options:
  247. rawOptions = appendOptions(rawOptions, v)
  248. }
  249. default:
  250. m := wire.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 := wire.ConsumeTag(b)
  263. b = b[n:]
  264. switch typ {
  265. case wire.BytesType:
  266. v, m := wire.ConsumeBytes(b)
  267. b = b[m:]
  268. switch num {
  269. case fieldnum.DescriptorProto_Field:
  270. rawFields = append(rawFields, v)
  271. case fieldnum.DescriptorProto_OneofDecl:
  272. rawOneofs = append(rawOneofs, v)
  273. case fieldnum.DescriptorProto_ReservedName:
  274. md.L2.ReservedNames.List = append(md.L2.ReservedNames.List, pref.Name(sb.MakeString(v)))
  275. case fieldnum.DescriptorProto_ReservedRange:
  276. md.L2.ReservedRanges.List = append(md.L2.ReservedRanges.List, unmarshalMessageReservedRange(v))
  277. case fieldnum.DescriptorProto_ExtensionRange:
  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 fieldnum.DescriptorProto_EnumType:
  283. md.L1.Enums.List[enumIdx].unmarshalFull(v, sb)
  284. enumIdx++
  285. case fieldnum.DescriptorProto_NestedType:
  286. md.L1.Messages.List[messageIdx].unmarshalFull(v, sb)
  287. messageIdx++
  288. case fieldnum.DescriptorProto_Extension:
  289. md.L1.Extensions.List[extensionIdx].unmarshalFull(v, sb)
  290. extensionIdx++
  291. case fieldnum.DescriptorProto_Options:
  292. md.unmarshalOptions(v)
  293. rawOptions = appendOptions(rawOptions, v)
  294. }
  295. default:
  296. m := wire.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 == pref.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 := wire.ConsumeTag(b)
  320. b = b[n:]
  321. switch typ {
  322. case wire.VarintType:
  323. v, m := wire.ConsumeVarint(b)
  324. b = b[m:]
  325. switch num {
  326. case fieldnum.MessageOptions_MapEntry:
  327. md.L2.IsMapEntry = wire.DecodeBool(v)
  328. case fieldnum.MessageOptions_MessageSetWireFormat:
  329. md.L2.IsMessageSet = wire.DecodeBool(v)
  330. }
  331. default:
  332. m := wire.ConsumeFieldValue(num, typ, b)
  333. b = b[m:]
  334. }
  335. }
  336. }
  337. func unmarshalMessageReservedRange(b []byte) (r [2]pref.FieldNumber) {
  338. for len(b) > 0 {
  339. num, typ, n := wire.ConsumeTag(b)
  340. b = b[n:]
  341. switch typ {
  342. case wire.VarintType:
  343. v, m := wire.ConsumeVarint(b)
  344. b = b[m:]
  345. switch num {
  346. case fieldnum.DescriptorProto_ReservedRange_Start:
  347. r[0] = pref.FieldNumber(v)
  348. case fieldnum.DescriptorProto_ReservedRange_End:
  349. r[1] = pref.FieldNumber(v)
  350. }
  351. default:
  352. m := wire.ConsumeFieldValue(num, typ, b)
  353. b = b[m:]
  354. }
  355. }
  356. return r
  357. }
  358. func unmarshalMessageExtensionRange(b []byte) (r [2]pref.FieldNumber, rawOptions []byte) {
  359. for len(b) > 0 {
  360. num, typ, n := wire.ConsumeTag(b)
  361. b = b[n:]
  362. switch typ {
  363. case wire.VarintType:
  364. v, m := wire.ConsumeVarint(b)
  365. b = b[m:]
  366. switch num {
  367. case fieldnum.DescriptorProto_ExtensionRange_Start:
  368. r[0] = pref.FieldNumber(v)
  369. case fieldnum.DescriptorProto_ExtensionRange_End:
  370. r[1] = pref.FieldNumber(v)
  371. }
  372. case wire.BytesType:
  373. v, m := wire.ConsumeBytes(b)
  374. b = b[m:]
  375. switch num {
  376. case fieldnum.DescriptorProto_ExtensionRange_Options:
  377. rawOptions = appendOptions(rawOptions, v)
  378. }
  379. default:
  380. m := wire.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 pref.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 := wire.ConsumeTag(b)
  394. b = b[n:]
  395. switch typ {
  396. case wire.VarintType:
  397. v, m := wire.ConsumeVarint(b)
  398. b = b[m:]
  399. switch num {
  400. case fieldnum.FieldDescriptorProto_Number:
  401. fd.L1.Number = pref.FieldNumber(v)
  402. case fieldnum.FieldDescriptorProto_Label:
  403. fd.L1.Cardinality = pref.Cardinality(v)
  404. case fieldnum.FieldDescriptorProto_Type:
  405. fd.L1.Kind = pref.Kind(v)
  406. case fieldnum.FieldDescriptorProto_OneofIndex:
  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. }
  417. case wire.BytesType:
  418. v, m := wire.ConsumeBytes(b)
  419. b = b[m:]
  420. switch num {
  421. case fieldnum.FieldDescriptorProto_Name:
  422. fd.L0.FullName = appendFullName(sb, pd.FullName(), v)
  423. case fieldnum.FieldDescriptorProto_JsonName:
  424. fd.L1.JSONName = JSONName(sb.MakeString(v))
  425. case fieldnum.FieldDescriptorProto_DefaultValue:
  426. fd.L1.Default.val = pref.ValueOfBytes(v) // temporarily store as bytes; later resolved in resolveMessages
  427. case fieldnum.FieldDescriptorProto_TypeName:
  428. rawTypeName = v
  429. case fieldnum.FieldDescriptorProto_Options:
  430. fd.unmarshalOptions(v)
  431. rawOptions = appendOptions(rawOptions, v)
  432. }
  433. default:
  434. m := wire.ConsumeFieldValue(num, typ, b)
  435. b = b[m:]
  436. }
  437. }
  438. if rawTypeName != nil {
  439. name := makeFullName(sb, rawTypeName)
  440. switch fd.L1.Kind {
  441. case pref.EnumKind:
  442. fd.L1.Enum = PlaceholderEnum(name)
  443. case pref.MessageKind, pref.GroupKind:
  444. fd.L1.Message = PlaceholderMessage(name)
  445. }
  446. }
  447. fd.L1.Options = pf.builder.optionsUnmarshaler(&descopts.Field, rawOptions)
  448. }
  449. func (fd *Field) unmarshalOptions(b []byte) {
  450. const FieldOptions_EnforceUTF8 = 13
  451. for len(b) > 0 {
  452. num, typ, n := wire.ConsumeTag(b)
  453. b = b[n:]
  454. switch typ {
  455. case wire.VarintType:
  456. v, m := wire.ConsumeVarint(b)
  457. b = b[m:]
  458. switch num {
  459. case fieldnum.FieldOptions_Packed:
  460. fd.L1.HasPacked = true
  461. fd.L1.IsPacked = wire.DecodeBool(v)
  462. case fieldnum.FieldOptions_Weak:
  463. fd.L1.IsWeak = wire.DecodeBool(v)
  464. case FieldOptions_EnforceUTF8:
  465. fd.L1.HasEnforceUTF8 = true
  466. fd.L1.EnforceUTF8 = wire.DecodeBool(v)
  467. }
  468. default:
  469. m := wire.ConsumeFieldValue(num, typ, b)
  470. b = b[m:]
  471. }
  472. }
  473. }
  474. func (od *Oneof) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd pref.Descriptor, i int) {
  475. od.L0.ParentFile = pf
  476. od.L0.Parent = pd
  477. od.L0.Index = i
  478. var rawOptions []byte
  479. for len(b) > 0 {
  480. num, typ, n := wire.ConsumeTag(b)
  481. b = b[n:]
  482. switch typ {
  483. case wire.BytesType:
  484. v, m := wire.ConsumeBytes(b)
  485. b = b[m:]
  486. switch num {
  487. case fieldnum.OneofDescriptorProto_Name:
  488. od.L0.FullName = appendFullName(sb, pd.FullName(), v)
  489. case fieldnum.OneofDescriptorProto_Options:
  490. rawOptions = appendOptions(rawOptions, v)
  491. }
  492. default:
  493. m := wire.ConsumeFieldValue(num, typ, b)
  494. b = b[m:]
  495. }
  496. }
  497. od.L1.Options = pf.builder.optionsUnmarshaler(&descopts.Oneof, rawOptions)
  498. }
  499. func (xd *Extension) unmarshalFull(b []byte, sb *strs.Builder) {
  500. var rawTypeName []byte
  501. var rawOptions []byte
  502. xd.L2 = new(ExtensionL2)
  503. for len(b) > 0 {
  504. num, typ, n := wire.ConsumeTag(b)
  505. b = b[n:]
  506. switch typ {
  507. case wire.BytesType:
  508. v, m := wire.ConsumeBytes(b)
  509. b = b[m:]
  510. switch num {
  511. case fieldnum.FieldDescriptorProto_JsonName:
  512. xd.L2.JSONName = JSONName(sb.MakeString(v))
  513. case fieldnum.FieldDescriptorProto_DefaultValue:
  514. xd.L2.Default.val = pref.ValueOfBytes(v) // temporarily store as bytes; later resolved in resolveExtensions
  515. case fieldnum.FieldDescriptorProto_TypeName:
  516. rawTypeName = v
  517. case fieldnum.FieldDescriptorProto_Options:
  518. xd.unmarshalOptions(v)
  519. rawOptions = appendOptions(rawOptions, v)
  520. }
  521. default:
  522. m := wire.ConsumeFieldValue(num, typ, b)
  523. b = b[m:]
  524. }
  525. }
  526. if rawTypeName != nil {
  527. name := makeFullName(sb, rawTypeName)
  528. switch xd.L1.Kind {
  529. case pref.EnumKind:
  530. xd.L2.Enum = PlaceholderEnum(name)
  531. case pref.MessageKind, pref.GroupKind:
  532. xd.L2.Message = PlaceholderMessage(name)
  533. }
  534. }
  535. xd.L2.Options = xd.L0.ParentFile.builder.optionsUnmarshaler(&descopts.Field, rawOptions)
  536. }
  537. func (xd *Extension) unmarshalOptions(b []byte) {
  538. for len(b) > 0 {
  539. num, typ, n := wire.ConsumeTag(b)
  540. b = b[n:]
  541. switch typ {
  542. case wire.VarintType:
  543. v, m := wire.ConsumeVarint(b)
  544. b = b[m:]
  545. switch num {
  546. case fieldnum.FieldOptions_Packed:
  547. xd.L2.IsPacked = wire.DecodeBool(v)
  548. }
  549. default:
  550. m := wire.ConsumeFieldValue(num, typ, b)
  551. b = b[m:]
  552. }
  553. }
  554. }
  555. func (sd *Service) unmarshalFull(b []byte, sb *strs.Builder) {
  556. var rawMethods [][]byte
  557. var rawOptions []byte
  558. sd.L2 = new(ServiceL2)
  559. for len(b) > 0 {
  560. num, typ, n := wire.ConsumeTag(b)
  561. b = b[n:]
  562. switch typ {
  563. case wire.BytesType:
  564. v, m := wire.ConsumeBytes(b)
  565. b = b[m:]
  566. switch num {
  567. case fieldnum.ServiceDescriptorProto_Method:
  568. rawMethods = append(rawMethods, v)
  569. case fieldnum.ServiceDescriptorProto_Options:
  570. rawOptions = appendOptions(rawOptions, v)
  571. }
  572. default:
  573. m := wire.ConsumeFieldValue(num, typ, b)
  574. b = b[m:]
  575. }
  576. }
  577. if len(rawMethods) > 0 {
  578. sd.L2.Methods.List = make([]Method, len(rawMethods))
  579. for i, b := range rawMethods {
  580. sd.L2.Methods.List[i].unmarshalFull(b, sb, sd.L0.ParentFile, sd, i)
  581. }
  582. }
  583. sd.L2.Options = sd.L0.ParentFile.builder.optionsUnmarshaler(&descopts.Service, rawOptions)
  584. }
  585. func (md *Method) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd pref.Descriptor, i int) {
  586. md.L0.ParentFile = pf
  587. md.L0.Parent = pd
  588. md.L0.Index = i
  589. var rawOptions []byte
  590. for len(b) > 0 {
  591. num, typ, n := wire.ConsumeTag(b)
  592. b = b[n:]
  593. switch typ {
  594. case wire.VarintType:
  595. v, m := wire.ConsumeVarint(b)
  596. b = b[m:]
  597. switch num {
  598. case fieldnum.MethodDescriptorProto_ClientStreaming:
  599. md.L1.IsStreamingClient = wire.DecodeBool(v)
  600. case fieldnum.MethodDescriptorProto_ServerStreaming:
  601. md.L1.IsStreamingServer = wire.DecodeBool(v)
  602. }
  603. case wire.BytesType:
  604. v, m := wire.ConsumeBytes(b)
  605. b = b[m:]
  606. switch num {
  607. case fieldnum.MethodDescriptorProto_Name:
  608. md.L0.FullName = appendFullName(sb, pd.FullName(), v)
  609. case fieldnum.MethodDescriptorProto_InputType:
  610. md.L1.Input = PlaceholderMessage(makeFullName(sb, v))
  611. case fieldnum.MethodDescriptorProto_OutputType:
  612. md.L1.Output = PlaceholderMessage(makeFullName(sb, v))
  613. case fieldnum.MethodDescriptorProto_Options:
  614. rawOptions = appendOptions(rawOptions, v)
  615. }
  616. default:
  617. m := wire.ConsumeFieldValue(num, typ, b)
  618. b = b[m:]
  619. }
  620. }
  621. md.L1.Options = pf.builder.optionsUnmarshaler(&descopts.Method, rawOptions)
  622. }
  623. // appendOptions appends src to dst, where the returned slice is never nil.
  624. // This is necessary to distinguish between empty and unpopulated options.
  625. func appendOptions(dst, src []byte) []byte {
  626. if dst == nil {
  627. dst = []byte{}
  628. }
  629. return append(dst, src...)
  630. }
  631. // optionsUnmarshaler constructs a lazy unmarshal function for an options message.
  632. //
  633. // The type of message to unmarshal to is passed as a pointer since the
  634. // vars in descopts may not yet be populated at the time this function is called.
  635. func (db *Builder) optionsUnmarshaler(p *pref.ProtoMessage, b []byte) func() pref.ProtoMessage {
  636. if b == nil {
  637. return nil
  638. }
  639. var opts pref.ProtoMessage
  640. var once sync.Once
  641. return func() pref.ProtoMessage {
  642. once.Do(func() {
  643. if *p == nil {
  644. panic("Descriptor.Options called without importing the descriptor package")
  645. }
  646. opts = reflect.New(reflect.TypeOf(*p).Elem()).Interface().(pref.ProtoMessage)
  647. if err := (proto.UnmarshalOptions{
  648. AllowPartial: true,
  649. Resolver: db.TypeResolver,
  650. }).Unmarshal(b, opts); err != nil {
  651. panic(err)
  652. }
  653. })
  654. return opts
  655. }
  656. }