decode.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. m2 := m.Mutable(fd).Message()
  112. if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
  113. return n, err
  114. }
  115. default:
  116. // Non-message scalars replace the previous value.
  117. m.Set(fd, v)
  118. }
  119. return n, nil
  120. }
  121. func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp wire.Type, mapv protoreflect.Map, fd protoreflect.FieldDescriptor) (n int, err error) {
  122. if wtyp != wire.BytesType {
  123. return 0, errUnknown
  124. }
  125. b, n = wire.ConsumeBytes(b)
  126. if n < 0 {
  127. return 0, wire.ParseError(n)
  128. }
  129. var (
  130. keyField = fd.MapKey()
  131. valField = fd.MapValue()
  132. key protoreflect.Value
  133. val protoreflect.Value
  134. haveKey bool
  135. haveVal bool
  136. )
  137. switch valField.Kind() {
  138. case protoreflect.GroupKind, protoreflect.MessageKind:
  139. val = protoreflect.ValueOf(mapv.NewMessage())
  140. }
  141. // Map entries are represented as a two-element message with fields
  142. // containing the key and value.
  143. for len(b) > 0 {
  144. num, wtyp, n := wire.ConsumeTag(b)
  145. if n < 0 {
  146. return 0, wire.ParseError(n)
  147. }
  148. b = b[n:]
  149. err = errUnknown
  150. switch num {
  151. case 1:
  152. key, n, err = o.unmarshalScalar(b, wtyp, keyField)
  153. if err != nil {
  154. break
  155. }
  156. haveKey = true
  157. case 2:
  158. var v protoreflect.Value
  159. v, n, err = o.unmarshalScalar(b, wtyp, valField)
  160. if err != nil {
  161. break
  162. }
  163. switch valField.Kind() {
  164. case protoreflect.GroupKind, protoreflect.MessageKind:
  165. if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
  166. return 0, err
  167. }
  168. default:
  169. val = v
  170. }
  171. haveVal = true
  172. }
  173. if err == errUnknown {
  174. n = wire.ConsumeFieldValue(num, wtyp, b)
  175. if n < 0 {
  176. return 0, wire.ParseError(n)
  177. }
  178. } else if err != nil {
  179. return 0, err
  180. }
  181. b = b[n:]
  182. }
  183. // Every map entry should have entries for key and value, but this is not strictly required.
  184. if !haveKey {
  185. key = keyField.Default()
  186. }
  187. if !haveVal {
  188. switch valField.Kind() {
  189. case protoreflect.GroupKind, protoreflect.MessageKind:
  190. default:
  191. val = valField.Default()
  192. }
  193. }
  194. mapv.Set(key.MapKey(), val)
  195. return n, nil
  196. }
  197. // errUnknown is used internally to indicate fields which should be added
  198. // to the unknown field set of a message. It is never returned from an exported
  199. // function.
  200. var errUnknown = errors.New("BUG: internal error (unknown)")