decode.go 5.9 KB

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