decode.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. if err != nil {
  47. return err
  48. }
  49. if o.AllowPartial {
  50. return nil
  51. }
  52. return IsInitialized(m)
  53. }
  54. func (o UnmarshalOptions) unmarshalMessageFast(b []byte, m Message) error {
  55. methods := protoMethods(m)
  56. if methods == nil || methods.Unmarshal == nil {
  57. return errInternalNoFast
  58. }
  59. return methods.Unmarshal(b, m, protoiface.UnmarshalOptions(o))
  60. }
  61. func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
  62. messageDesc := m.Descriptor()
  63. fieldDescs := messageDesc.Fields()
  64. for len(b) > 0 {
  65. // Parse the tag (field number and wire type).
  66. num, wtyp, tagLen := wire.ConsumeTag(b)
  67. if tagLen < 0 {
  68. return wire.ParseError(tagLen)
  69. }
  70. // Parse the field value.
  71. fd := fieldDescs.ByNumber(num)
  72. if fd == nil && messageDesc.ExtensionRanges().Has(num) {
  73. extType, err := o.Resolver.FindExtensionByNumber(messageDesc.FullName(), num)
  74. if err != nil && err != protoregistry.NotFound {
  75. return err
  76. }
  77. fd = extType
  78. }
  79. var err error
  80. var valLen int
  81. switch {
  82. case fd == nil:
  83. err = errUnknown
  84. case fd.IsList():
  85. valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
  86. case fd.IsMap():
  87. valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
  88. default:
  89. valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
  90. }
  91. if err == errUnknown {
  92. valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
  93. if valLen < 0 {
  94. return wire.ParseError(valLen)
  95. }
  96. m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
  97. } else if err != nil {
  98. return err
  99. }
  100. b = b[tagLen+valLen:]
  101. }
  102. return nil
  103. }
  104. func (o UnmarshalOptions) unmarshalSingular(b []byte, wtyp wire.Type, m protoreflect.Message, fd protoreflect.FieldDescriptor) (n int, err error) {
  105. v, n, err := o.unmarshalScalar(b, wtyp, fd)
  106. if err != nil {
  107. return 0, err
  108. }
  109. switch fd.Kind() {
  110. case protoreflect.GroupKind, protoreflect.MessageKind:
  111. // Messages are merged with any existing message value,
  112. // unless the message is part of a oneof.
  113. //
  114. // TODO: C++ merges into oneofs, while v1 does not.
  115. // Evaluate which behavior to pick.
  116. var m2 protoreflect.Message
  117. if m.Has(fd) && fd.ContainingOneof() == nil {
  118. m2 = m.Mutable(fd).Message()
  119. } else {
  120. m2 = m.NewMessage(fd)
  121. m.Set(fd, protoreflect.ValueOf(m2))
  122. }
  123. // Pass up errors (fatal and otherwise).
  124. if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
  125. return n, err
  126. }
  127. default:
  128. // Non-message scalars replace the previous value.
  129. m.Set(fd, v)
  130. }
  131. return n, nil
  132. }
  133. func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp wire.Type, mapv protoreflect.Map, fd protoreflect.FieldDescriptor) (n int, err error) {
  134. if wtyp != wire.BytesType {
  135. return 0, errUnknown
  136. }
  137. b, n = wire.ConsumeBytes(b)
  138. if n < 0 {
  139. return 0, wire.ParseError(n)
  140. }
  141. var (
  142. keyField = fd.MapKey()
  143. valField = fd.MapValue()
  144. key protoreflect.Value
  145. val protoreflect.Value
  146. haveKey bool
  147. haveVal bool
  148. )
  149. switch valField.Kind() {
  150. case protoreflect.GroupKind, protoreflect.MessageKind:
  151. val = protoreflect.ValueOf(mapv.NewMessage())
  152. }
  153. // Map entries are represented as a two-element message with fields
  154. // containing the key and value.
  155. for len(b) > 0 {
  156. num, wtyp, n := wire.ConsumeTag(b)
  157. if n < 0 {
  158. return 0, wire.ParseError(n)
  159. }
  160. b = b[n:]
  161. err = errUnknown
  162. switch num {
  163. case 1:
  164. key, n, err = o.unmarshalScalar(b, wtyp, keyField)
  165. if err != nil {
  166. break
  167. }
  168. haveKey = true
  169. case 2:
  170. var v protoreflect.Value
  171. v, n, err = o.unmarshalScalar(b, wtyp, valField)
  172. if err != nil {
  173. break
  174. }
  175. switch valField.Kind() {
  176. case protoreflect.GroupKind, protoreflect.MessageKind:
  177. if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
  178. return 0, err
  179. }
  180. default:
  181. val = v
  182. }
  183. haveVal = true
  184. }
  185. if err == errUnknown {
  186. n = wire.ConsumeFieldValue(num, wtyp, b)
  187. if n < 0 {
  188. return 0, wire.ParseError(n)
  189. }
  190. } else if err != nil {
  191. return 0, err
  192. }
  193. b = b[n:]
  194. }
  195. // Every map entry should have entries for key and value, but this is not strictly required.
  196. if !haveKey {
  197. key = keyField.Default()
  198. }
  199. if !haveVal {
  200. switch valField.Kind() {
  201. case protoreflect.GroupKind, protoreflect.MessageKind:
  202. default:
  203. val = valField.Default()
  204. }
  205. }
  206. mapv.Set(key.MapKey(), val)
  207. return n, nil
  208. }
  209. // errUnknown is used internally to indicate fields which should be added
  210. // to the unknown field set of a message. It is never returned from an exported
  211. // function.
  212. var errUnknown = errors.New("BUG: internal error (unknown)")