unmarshal.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package bson
  7. import (
  8. "bytes"
  9. "go.mongodb.org/mongo-driver/bson/bsoncodec"
  10. "go.mongodb.org/mongo-driver/bson/bsonrw"
  11. "go.mongodb.org/mongo-driver/bson/bsontype"
  12. )
  13. // Unmarshaler is the interface implemented by types that can unmarshal a BSON
  14. // document representation of themselves. The input can be assumed to be a valid
  15. // encoding of a BSON document. UnmarshalBSON must copy the JSON data if it
  16. // wishes to retain the data after returning.
  17. //
  18. // Unmarshaler is only used to unmarshal full BSON documents. To create custom
  19. // BSON unmarshaling behavior for individual values in a BSON document,
  20. // implement the ValueUnmarshaler interface instead.
  21. type Unmarshaler interface {
  22. UnmarshalBSON([]byte) error
  23. }
  24. // ValueUnmarshaler is the interface implemented by types that can unmarshal a
  25. // BSON value representation of themselves. The input can be assumed to be a
  26. // valid encoding of a BSON value. UnmarshalBSONValue must copy the BSON value
  27. // bytes if it wishes to retain the data after returning.
  28. //
  29. // ValueUnmarshaler is only used to unmarshal individual values in a BSON
  30. // document. To create custom BSON unmarshaling behavior for an entire BSON
  31. // document, implement the Unmarshaler interface instead.
  32. type ValueUnmarshaler interface {
  33. UnmarshalBSONValue(bsontype.Type, []byte) error
  34. }
  35. // Unmarshal parses the BSON-encoded data and stores the result in the value
  36. // pointed to by val. If val is nil or not a pointer, Unmarshal returns
  37. // InvalidUnmarshalError.
  38. func Unmarshal(data []byte, val interface{}) error {
  39. return UnmarshalWithRegistry(DefaultRegistry, data, val)
  40. }
  41. // UnmarshalWithRegistry parses the BSON-encoded data using Registry r and
  42. // stores the result in the value pointed to by val. If val is nil or not
  43. // a pointer, UnmarshalWithRegistry returns InvalidUnmarshalError.
  44. //
  45. // Deprecated: Use [NewDecoder] and specify the Registry by calling [Decoder.SetRegistry] instead:
  46. //
  47. // dec, err := bson.NewDecoder(bsonrw.NewBSONDocumentReader(data))
  48. // if err != nil {
  49. // panic(err)
  50. // }
  51. // dec.SetRegistry(reg)
  52. //
  53. // See [Decoder] for more examples.
  54. func UnmarshalWithRegistry(r *bsoncodec.Registry, data []byte, val interface{}) error {
  55. vr := bsonrw.NewBSONDocumentReader(data)
  56. return unmarshalFromReader(bsoncodec.DecodeContext{Registry: r}, vr, val)
  57. }
  58. // UnmarshalWithContext parses the BSON-encoded data using DecodeContext dc and
  59. // stores the result in the value pointed to by val. If val is nil or not
  60. // a pointer, UnmarshalWithRegistry returns InvalidUnmarshalError.
  61. //
  62. // Deprecated: Use [NewDecoder] and use the Decoder configuration methods to set the desired unmarshal
  63. // behavior instead:
  64. //
  65. // dec, err := bson.NewDecoder(bsonrw.NewBSONDocumentReader(data))
  66. // if err != nil {
  67. // panic(err)
  68. // }
  69. // dec.DefaultDocumentM()
  70. //
  71. // See [Decoder] for more examples.
  72. func UnmarshalWithContext(dc bsoncodec.DecodeContext, data []byte, val interface{}) error {
  73. vr := bsonrw.NewBSONDocumentReader(data)
  74. return unmarshalFromReader(dc, vr, val)
  75. }
  76. // UnmarshalValue parses the BSON value of type t with bson.DefaultRegistry and
  77. // stores the result in the value pointed to by val. If val is nil or not a pointer,
  78. // UnmarshalValue returns an error.
  79. func UnmarshalValue(t bsontype.Type, data []byte, val interface{}) error {
  80. return UnmarshalValueWithRegistry(DefaultRegistry, t, data, val)
  81. }
  82. // UnmarshalValueWithRegistry parses the BSON value of type t with registry r and
  83. // stores the result in the value pointed to by val. If val is nil or not a pointer,
  84. // UnmarshalValue returns an error.
  85. //
  86. // Deprecated: Using a custom registry to unmarshal individual BSON values will not be supported in
  87. // Go Driver 2.0.
  88. func UnmarshalValueWithRegistry(r *bsoncodec.Registry, t bsontype.Type, data []byte, val interface{}) error {
  89. vr := bsonrw.NewBSONValueReader(t, data)
  90. return unmarshalFromReader(bsoncodec.DecodeContext{Registry: r}, vr, val)
  91. }
  92. // UnmarshalExtJSON parses the extended JSON-encoded data and stores the result
  93. // in the value pointed to by val. If val is nil or not a pointer, Unmarshal
  94. // returns InvalidUnmarshalError.
  95. func UnmarshalExtJSON(data []byte, canonical bool, val interface{}) error {
  96. return UnmarshalExtJSONWithRegistry(DefaultRegistry, data, canonical, val)
  97. }
  98. // UnmarshalExtJSONWithRegistry parses the extended JSON-encoded data using
  99. // Registry r and stores the result in the value pointed to by val. If val is
  100. // nil or not a pointer, UnmarshalWithRegistry returns InvalidUnmarshalError.
  101. //
  102. // Deprecated: Use [NewDecoder] and specify the Registry by calling [Decoder.SetRegistry] instead:
  103. //
  104. // vr, err := bsonrw.NewExtJSONValueReader(bytes.NewReader(data), true)
  105. // if err != nil {
  106. // panic(err)
  107. // }
  108. // dec, err := bson.NewDecoder(vr)
  109. // if err != nil {
  110. // panic(err)
  111. // }
  112. // dec.SetRegistry(reg)
  113. //
  114. // See [Decoder] for more examples.
  115. func UnmarshalExtJSONWithRegistry(r *bsoncodec.Registry, data []byte, canonical bool, val interface{}) error {
  116. ejvr, err := bsonrw.NewExtJSONValueReader(bytes.NewReader(data), canonical)
  117. if err != nil {
  118. return err
  119. }
  120. return unmarshalFromReader(bsoncodec.DecodeContext{Registry: r}, ejvr, val)
  121. }
  122. // UnmarshalExtJSONWithContext parses the extended JSON-encoded data using
  123. // DecodeContext dc and stores the result in the value pointed to by val. If val is
  124. // nil or not a pointer, UnmarshalWithRegistry returns InvalidUnmarshalError.
  125. //
  126. // Deprecated: Use [NewDecoder] and use the Decoder configuration methods to set the desired unmarshal
  127. // behavior instead:
  128. //
  129. // vr, err := bsonrw.NewExtJSONValueReader(bytes.NewReader(data), true)
  130. // if err != nil {
  131. // panic(err)
  132. // }
  133. // dec, err := bson.NewDecoder(vr)
  134. // if err != nil {
  135. // panic(err)
  136. // }
  137. // dec.DefaultDocumentM()
  138. //
  139. // See [Decoder] for more examples.
  140. func UnmarshalExtJSONWithContext(dc bsoncodec.DecodeContext, data []byte, canonical bool, val interface{}) error {
  141. ejvr, err := bsonrw.NewExtJSONValueReader(bytes.NewReader(data), canonical)
  142. if err != nil {
  143. return err
  144. }
  145. return unmarshalFromReader(dc, ejvr, val)
  146. }
  147. func unmarshalFromReader(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val interface{}) error {
  148. dec := decPool.Get().(*Decoder)
  149. defer decPool.Put(dec)
  150. err := dec.Reset(vr)
  151. if err != nil {
  152. return err
  153. }
  154. err = dec.SetContext(dc)
  155. if err != nil {
  156. return err
  157. }
  158. return dec.Decode(val)
  159. }