legacy_parse.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 impl
  5. import (
  6. "google.golang.org/protobuf/internal/encoding/wire"
  7. "google.golang.org/protobuf/internal/fieldnum"
  8. )
  9. // To avoid a dependency from legacy to descriptor.proto, use a hand-written parser
  10. // for the bits of the descriptor we need.
  11. //
  12. // TODO: Consider unifying this with the parser in fileinit.
  13. type legacyFileDescriptorProto struct {
  14. Syntax string
  15. Package string
  16. EnumType []*legacyEnumDescriptorProto
  17. MessageType []*legacyDescriptorProto
  18. }
  19. func (fd legacyFileDescriptorProto) GetSyntax() string { return fd.Syntax }
  20. func (fd legacyFileDescriptorProto) GetPackage() string { return fd.Package }
  21. func legacyParseFileDescProto(b []byte) *legacyFileDescriptorProto {
  22. fd := &legacyFileDescriptorProto{}
  23. for len(b) > 0 {
  24. num, typ, n := wire.ConsumeTag(b)
  25. legacyParseCheck(n)
  26. b = b[n:]
  27. switch typ {
  28. case wire.BytesType:
  29. v, n := wire.ConsumeBytes(b)
  30. b = b[n:]
  31. switch num {
  32. case fieldnum.FileDescriptorProto_Syntax:
  33. fd.Syntax = string(v)
  34. case fieldnum.FileDescriptorProto_Package:
  35. fd.Package = string(v)
  36. case fieldnum.FileDescriptorProto_EnumType:
  37. fd.EnumType = append(fd.EnumType, legacyParseEnumDescProto(v))
  38. case fieldnum.FileDescriptorProto_MessageType:
  39. fd.MessageType = append(fd.MessageType, parseDescProto(v))
  40. }
  41. default:
  42. n := wire.ConsumeFieldValue(num, typ, b)
  43. legacyParseCheck(n)
  44. b = b[n:]
  45. }
  46. }
  47. return fd
  48. }
  49. type legacyDescriptorProto struct {
  50. Name string
  51. NestedType []*legacyDescriptorProto
  52. EnumType []*legacyEnumDescriptorProto
  53. }
  54. func (md legacyDescriptorProto) GetName() string { return md.Name }
  55. func parseDescProto(b []byte) *legacyDescriptorProto {
  56. md := &legacyDescriptorProto{}
  57. for len(b) > 0 {
  58. num, typ, n := wire.ConsumeTag(b)
  59. legacyParseCheck(n)
  60. b = b[n:]
  61. switch typ {
  62. case wire.BytesType:
  63. v, n := wire.ConsumeBytes(b)
  64. legacyParseCheck(n)
  65. b = b[n:]
  66. switch num {
  67. case fieldnum.DescriptorProto_Name:
  68. md.Name = string(v)
  69. case fieldnum.DescriptorProto_NestedType:
  70. md.NestedType = append(md.NestedType, parseDescProto(v))
  71. case fieldnum.DescriptorProto_EnumType:
  72. md.EnumType = append(md.EnumType, legacyParseEnumDescProto(v))
  73. }
  74. default:
  75. n := wire.ConsumeFieldValue(num, typ, b)
  76. legacyParseCheck(n)
  77. b = b[n:]
  78. }
  79. }
  80. return md
  81. }
  82. type legacyEnumDescriptorProto struct {
  83. Name string
  84. Value []*legacyEnumValueDescriptorProto
  85. }
  86. func (ed legacyEnumDescriptorProto) GetName() string { return ed.Name }
  87. func legacyParseEnumDescProto(b []byte) *legacyEnumDescriptorProto {
  88. ed := &legacyEnumDescriptorProto{}
  89. for len(b) > 0 {
  90. num, typ, n := wire.ConsumeTag(b)
  91. legacyParseCheck(n)
  92. b = b[n:]
  93. switch typ {
  94. case wire.BytesType:
  95. v, n := wire.ConsumeBytes(b)
  96. legacyParseCheck(n)
  97. b = b[n:]
  98. switch num {
  99. case fieldnum.EnumDescriptorProto_Name:
  100. ed.Name = string(v)
  101. case fieldnum.EnumDescriptorProto_Value:
  102. ed.Value = append(ed.Value, legacyParseEnumValueDescProto(v))
  103. }
  104. default:
  105. n := wire.ConsumeFieldValue(num, typ, b)
  106. legacyParseCheck(n)
  107. b = b[n:]
  108. }
  109. }
  110. return ed
  111. }
  112. type legacyEnumValueDescriptorProto struct {
  113. Name string
  114. Number int32
  115. }
  116. func (ed legacyEnumValueDescriptorProto) GetName() string { return ed.Name }
  117. func (ed legacyEnumValueDescriptorProto) GetNumber() int32 { return ed.Number }
  118. func legacyParseEnumValueDescProto(b []byte) *legacyEnumValueDescriptorProto {
  119. vd := &legacyEnumValueDescriptorProto{}
  120. for len(b) > 0 {
  121. num, typ, n := wire.ConsumeTag(b)
  122. legacyParseCheck(n)
  123. b = b[n:]
  124. switch typ {
  125. case wire.VarintType:
  126. v, n := wire.ConsumeVarint(b)
  127. legacyParseCheck(n)
  128. b = b[n:]
  129. switch num {
  130. case fieldnum.EnumValueDescriptorProto_Number:
  131. vd.Number = int32(v)
  132. }
  133. case wire.BytesType:
  134. v, n := wire.ConsumeBytes(b)
  135. legacyParseCheck(n)
  136. b = b[n:]
  137. switch num {
  138. case fieldnum.EnumDescriptorProto_Name:
  139. vd.Name = string(v)
  140. }
  141. default:
  142. n := wire.ConsumeFieldValue(num, typ, b)
  143. legacyParseCheck(n)
  144. b = b[n:]
  145. }
  146. }
  147. return vd
  148. }
  149. func legacyParseCheck(n int) {
  150. if n < 0 {
  151. panic(wire.ParseError(n))
  152. }
  153. }