decode.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright 2018 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 proto
  5. import (
  6. "google.golang.org/protobuf/internal/encoding/wire"
  7. "google.golang.org/protobuf/internal/errors"
  8. "google.golang.org/protobuf/internal/pragma"
  9. "google.golang.org/protobuf/reflect/protoreflect"
  10. "google.golang.org/protobuf/reflect/protoregistry"
  11. "google.golang.org/protobuf/runtime/protoiface"
  12. )
  13. // UnmarshalOptions configures the unmarshaler.
  14. //
  15. // Example usage:
  16. // err := UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, m)
  17. type UnmarshalOptions struct {
  18. // AllowPartial accepts input for messages that will result in missing
  19. // required fields. If AllowPartial is false (the default), Unmarshal will
  20. // return an error if there are any missing required fields.
  21. AllowPartial bool
  22. // If DiscardUnknown is set, unknown fields are ignored.
  23. DiscardUnknown bool
  24. // Resolver is used for looking up types when unmarshaling extension fields.
  25. // If nil, this defaults to using protoregistry.GlobalTypes.
  26. Resolver interface {
  27. protoregistry.ExtensionTypeResolver
  28. }
  29. pragma.NoUnkeyedLiterals
  30. }
  31. var _ = protoiface.UnmarshalOptions(UnmarshalOptions{})
  32. // Unmarshal parses the wire-format message in b and places the result in m.
  33. func Unmarshal(b []byte, m Message) error {
  34. return UnmarshalOptions{}.Unmarshal(b, m)
  35. }
  36. // Unmarshal parses the wire-format message in b and places the result in m.
  37. func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
  38. if o.Resolver == nil {
  39. o.Resolver = protoregistry.GlobalTypes
  40. }
  41. // TODO: Reset m?
  42. err := o.unmarshalMessageFast(b, m)
  43. if err == errInternalNoFast {
  44. err = o.unmarshalMessage(b, m.ProtoReflect())
  45. }
  46. var nerr errors.NonFatal
  47. if !nerr.Merge(err) {
  48. return err
  49. }
  50. if !o.AllowPartial {
  51. nerr.Merge(IsInitialized(m))
  52. }
  53. return nerr.E
  54. }
  55. func (o UnmarshalOptions) unmarshalMessageFast(b []byte, m Message) error {
  56. methods := protoMethods(m)
  57. if methods == nil || methods.Unmarshal == nil {
  58. return errInternalNoFast
  59. }
  60. return methods.Unmarshal(b, m, protoiface.UnmarshalOptions(o))
  61. }
  62. func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
  63. messageDesc := m.Descriptor()
  64. fieldDescs := messageDesc.Fields()
  65. var nerr errors.NonFatal
  66. for len(b) > 0 {
  67. // Parse the tag (field number and wire type).
  68. num, wtyp, tagLen := wire.ConsumeTag(b)
  69. if tagLen < 0 {
  70. return wire.ParseError(tagLen)
  71. }
  72. // Parse the field value.
  73. fd := fieldDescs.ByNumber(num)
  74. if fd == nil && messageDesc.ExtensionRanges().Has(num) {
  75. extType, err := o.Resolver.FindExtensionByNumber(messageDesc.FullName(), num)
  76. if err != nil && err != protoregistry.NotFound {
  77. return err
  78. }
  79. fd = extType
  80. }
  81. var err error
  82. var valLen int
  83. switch {
  84. case fd == nil:
  85. err = errUnknown
  86. case fd.IsList():
  87. valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
  88. case fd.IsMap():
  89. valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
  90. default:
  91. valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
  92. }
  93. if err == errUnknown {
  94. valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
  95. if valLen < 0 {
  96. return wire.ParseError(valLen)
  97. }
  98. m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
  99. } else if !nerr.Merge(err) {
  100. return err
  101. }
  102. b = b[tagLen+valLen:]
  103. }
  104. return nerr.E
  105. }
  106. func (o UnmarshalOptions) unmarshalSingular(b []byte, wtyp wire.Type, m protoreflect.Message, fd protoreflect.FieldDescriptor) (n int, err error) {
  107. var nerr errors.NonFatal
  108. v, n, err := o.unmarshalScalar(b, wtyp, fd)
  109. if !nerr.Merge(err) {
  110. return 0, err
  111. }
  112. switch fd.Kind() {
  113. case protoreflect.GroupKind, protoreflect.MessageKind:
  114. // Messages are merged with any existing message value,
  115. // unless the message is part of a oneof.
  116. //
  117. // TODO: C++ merges into oneofs, while v1 does not.
  118. // Evaluate which behavior to pick.
  119. var m2 protoreflect.Message
  120. if m.Has(fd) && fd.ContainingOneof() == nil {
  121. m2 = m.Mutable(fd).Message()
  122. } else {
  123. m2 = m.NewMessage(fd)
  124. m.Set(fd, protoreflect.ValueOf(m2))
  125. }
  126. // Pass up errors (fatal and otherwise).
  127. if err := o.unmarshalMessage(v.Bytes(), m2); !nerr.Merge(err) {
  128. return n, err
  129. }
  130. default:
  131. // Non-message scalars replace the previous value.
  132. m.Set(fd, v)
  133. }
  134. return n, nerr.E
  135. }
  136. func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp wire.Type, mapv protoreflect.Map, fd protoreflect.FieldDescriptor) (n int, err error) {
  137. if wtyp != wire.BytesType {
  138. return 0, errUnknown
  139. }
  140. b, n = wire.ConsumeBytes(b)
  141. if n < 0 {
  142. return 0, wire.ParseError(n)
  143. }
  144. var (
  145. keyField = fd.MapKey()
  146. valField = fd.MapValue()
  147. key protoreflect.Value
  148. val protoreflect.Value
  149. haveKey bool
  150. haveVal bool
  151. )
  152. switch valField.Kind() {
  153. case protoreflect.GroupKind, protoreflect.MessageKind:
  154. val = protoreflect.ValueOf(mapv.NewMessage())
  155. }
  156. // Map entries are represented as a two-element message with fields
  157. // containing the key and value.
  158. var nerr errors.NonFatal
  159. for len(b) > 0 {
  160. num, wtyp, n := wire.ConsumeTag(b)
  161. if n < 0 {
  162. return 0, wire.ParseError(n)
  163. }
  164. b = b[n:]
  165. err = errUnknown
  166. switch num {
  167. case 1:
  168. key, n, err = o.unmarshalScalar(b, wtyp, keyField)
  169. if !nerr.Merge(err) {
  170. break
  171. }
  172. err = nil
  173. haveKey = true
  174. case 2:
  175. var v protoreflect.Value
  176. v, n, err = o.unmarshalScalar(b, wtyp, valField)
  177. if !nerr.Merge(err) {
  178. break
  179. }
  180. err = nil
  181. switch valField.Kind() {
  182. case protoreflect.GroupKind, protoreflect.MessageKind:
  183. if err := o.unmarshalMessage(v.Bytes(), val.Message()); !nerr.Merge(err) {
  184. return 0, err
  185. }
  186. default:
  187. val = v
  188. }
  189. haveVal = true
  190. }
  191. if err == errUnknown {
  192. n = wire.ConsumeFieldValue(num, wtyp, b)
  193. if n < 0 {
  194. return 0, wire.ParseError(n)
  195. }
  196. } else if err != nil {
  197. return 0, err
  198. }
  199. b = b[n:]
  200. }
  201. // Every map entry should have entries for key and value, but this is not strictly required.
  202. if !haveKey {
  203. key = keyField.Default()
  204. }
  205. if !haveVal {
  206. switch valField.Kind() {
  207. case protoreflect.GroupKind, protoreflect.MessageKind:
  208. default:
  209. val = valField.Default()
  210. }
  211. }
  212. mapv.Set(key.MapKey(), val)
  213. return n, nerr.E
  214. }
  215. // errUnknown is used internally to indicate fields which should be added
  216. // to the unknown field set of a message. It is never returned from an exported
  217. // function.
  218. var errUnknown = errors.New("BUG: internal error (unknown)")