You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.8 KiB

  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 an interface implemented by types that can unmarshal a BSON
  14. // document representation of themselves. The BSON bytes can be assumed to be
  15. // valid. UnmarshalBSON must copy the BSON bytes if it wishes to retain the data
  16. // after returning.
  17. type Unmarshaler interface {
  18. UnmarshalBSON([]byte) error
  19. }
  20. // ValueUnmarshaler is an interface implemented by types that can unmarshal a
  21. // BSON value representaiton of themselves. The BSON bytes and type can be
  22. // assumed to be valid. UnmarshalBSONValue must copy the BSON value bytes if it
  23. // wishes to retain the data after returning.
  24. type ValueUnmarshaler interface {
  25. UnmarshalBSONValue(bsontype.Type, []byte) error
  26. }
  27. // Unmarshal parses the BSON-encoded data and stores the result in the value
  28. // pointed to by val. If val is nil or not a pointer, Unmarshal returns
  29. // InvalidUnmarshalError.
  30. func Unmarshal(data []byte, val interface{}) error {
  31. return UnmarshalWithRegistry(DefaultRegistry, data, val)
  32. }
  33. // UnmarshalWithRegistry parses the BSON-encoded data using Registry r and
  34. // stores the result in the value pointed to by val. If val is nil or not
  35. // a pointer, UnmarshalWithRegistry returns InvalidUnmarshalError.
  36. func UnmarshalWithRegistry(r *bsoncodec.Registry, data []byte, val interface{}) error {
  37. vr := bsonrw.NewBSONDocumentReader(data)
  38. return unmarshalFromReader(bsoncodec.DecodeContext{Registry: r}, vr, val)
  39. }
  40. // UnmarshalWithContext parses the BSON-encoded data using DecodeContext dc and
  41. // stores the result in the value pointed to by val. If val is nil or not
  42. // a pointer, UnmarshalWithRegistry returns InvalidUnmarshalError.
  43. func UnmarshalWithContext(dc bsoncodec.DecodeContext, data []byte, val interface{}) error {
  44. vr := bsonrw.NewBSONDocumentReader(data)
  45. return unmarshalFromReader(dc, vr, val)
  46. }
  47. // UnmarshalExtJSON parses the extended JSON-encoded data and stores the result
  48. // in the value pointed to by val. If val is nil or not a pointer, Unmarshal
  49. // returns InvalidUnmarshalError.
  50. func UnmarshalExtJSON(data []byte, canonical bool, val interface{}) error {
  51. return UnmarshalExtJSONWithRegistry(DefaultRegistry, data, canonical, val)
  52. }
  53. // UnmarshalExtJSONWithRegistry parses the extended JSON-encoded data using
  54. // Registry r and stores the result in the value pointed to by val. If val is
  55. // nil or not a pointer, UnmarshalWithRegistry returns InvalidUnmarshalError.
  56. func UnmarshalExtJSONWithRegistry(r *bsoncodec.Registry, data []byte, canonical bool, val interface{}) error {
  57. ejvr, err := bsonrw.NewExtJSONValueReader(bytes.NewReader(data), canonical)
  58. if err != nil {
  59. return err
  60. }
  61. return unmarshalFromReader(bsoncodec.DecodeContext{Registry: r}, ejvr, val)
  62. }
  63. // UnmarshalExtJSONWithContext parses the extended JSON-encoded data using
  64. // DecodeContext dc and stores the result in the value pointed to by val. If val is
  65. // nil or not a pointer, UnmarshalWithRegistry returns InvalidUnmarshalError.
  66. func UnmarshalExtJSONWithContext(dc bsoncodec.DecodeContext, data []byte, canonical bool, val interface{}) error {
  67. ejvr, err := bsonrw.NewExtJSONValueReader(bytes.NewReader(data), canonical)
  68. if err != nil {
  69. return err
  70. }
  71. return unmarshalFromReader(dc, ejvr, val)
  72. }
  73. func unmarshalFromReader(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val interface{}) error {
  74. dec := decPool.Get().(*Decoder)
  75. defer decPool.Put(dec)
  76. err := dec.Reset(vr)
  77. if err != nil {
  78. return err
  79. }
  80. err = dec.SetContext(dc)
  81. if err != nil {
  82. return err
  83. }
  84. return dec.Decode(val)
  85. }