本站源代码
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.

164 lines
5.5KB

  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 bsoncodec // import "go.mongodb.org/mongo-driver/bson/bsoncodec"
  7. import (
  8. "fmt"
  9. "reflect"
  10. "strings"
  11. "go.mongodb.org/mongo-driver/bson/bsonrw"
  12. "go.mongodb.org/mongo-driver/bson/bsontype"
  13. )
  14. // Marshaler is an interface implemented by types that can marshal themselves
  15. // into a BSON document represented as bytes. The bytes returned must be a valid
  16. // BSON document if the error is nil.
  17. type Marshaler interface {
  18. MarshalBSON() ([]byte, error)
  19. }
  20. // ValueMarshaler is an interface implemented by types that can marshal
  21. // themselves into a BSON value as bytes. The type must be the valid type for
  22. // the bytes returned. The bytes and byte type together must be valid if the
  23. // error is nil.
  24. type ValueMarshaler interface {
  25. MarshalBSONValue() (bsontype.Type, []byte, error)
  26. }
  27. // Unmarshaler is an interface implemented by types that can unmarshal a BSON
  28. // document representation of themselves. The BSON bytes can be assumed to be
  29. // valid. UnmarshalBSON must copy the BSON bytes if it wishes to retain the data
  30. // after returning.
  31. type Unmarshaler interface {
  32. UnmarshalBSON([]byte) error
  33. }
  34. // ValueUnmarshaler is an interface implemented by types that can unmarshal a
  35. // BSON value representaiton of themselves. The BSON bytes and type can be
  36. // assumed to be valid. UnmarshalBSONValue must copy the BSON value bytes if it
  37. // wishes to retain the data after returning.
  38. type ValueUnmarshaler interface {
  39. UnmarshalBSONValue(bsontype.Type, []byte) error
  40. }
  41. // ValueEncoderError is an error returned from a ValueEncoder when the provided value can't be
  42. // encoded by the ValueEncoder.
  43. type ValueEncoderError struct {
  44. Name string
  45. Types []reflect.Type
  46. Kinds []reflect.Kind
  47. Received reflect.Value
  48. }
  49. func (vee ValueEncoderError) Error() string {
  50. typeKinds := make([]string, 0, len(vee.Types)+len(vee.Kinds))
  51. for _, t := range vee.Types {
  52. typeKinds = append(typeKinds, t.String())
  53. }
  54. for _, k := range vee.Kinds {
  55. if k == reflect.Map {
  56. typeKinds = append(typeKinds, "map[string]*")
  57. continue
  58. }
  59. typeKinds = append(typeKinds, k.String())
  60. }
  61. received := vee.Received.Kind().String()
  62. if vee.Received.IsValid() {
  63. received = vee.Received.Type().String()
  64. }
  65. return fmt.Sprintf("%s can only encode valid %s, but got %s", vee.Name, strings.Join(typeKinds, ", "), received)
  66. }
  67. // ValueDecoderError is an error returned from a ValueDecoder when the provided value can't be
  68. // decoded by the ValueDecoder.
  69. type ValueDecoderError struct {
  70. Name string
  71. Types []reflect.Type
  72. Kinds []reflect.Kind
  73. Received reflect.Value
  74. }
  75. func (vde ValueDecoderError) Error() string {
  76. typeKinds := make([]string, 0, len(vde.Types)+len(vde.Kinds))
  77. for _, t := range vde.Types {
  78. typeKinds = append(typeKinds, t.String())
  79. }
  80. for _, k := range vde.Kinds {
  81. if k == reflect.Map {
  82. typeKinds = append(typeKinds, "map[string]*")
  83. continue
  84. }
  85. typeKinds = append(typeKinds, k.String())
  86. }
  87. received := vde.Received.Kind().String()
  88. if vde.Received.IsValid() {
  89. received = vde.Received.Type().String()
  90. }
  91. return fmt.Sprintf("%s can only decode valid and settable %s, but got %s", vde.Name, strings.Join(typeKinds, ", "), received)
  92. }
  93. // EncodeContext is the contextual information required for a Codec to encode a
  94. // value.
  95. type EncodeContext struct {
  96. *Registry
  97. MinSize bool
  98. }
  99. // DecodeContext is the contextual information required for a Codec to decode a
  100. // value.
  101. type DecodeContext struct {
  102. *Registry
  103. Truncate bool
  104. // Ancestor is the type of a containing document. This is mainly used to determine what type
  105. // should be used when decoding an embedded document into an empty interface. For example, if
  106. // Ancestor is a bson.M, BSON embedded document values being decoded into an empty interface
  107. // will be decoded into a bson.M.
  108. Ancestor reflect.Type
  109. }
  110. // ValueCodec is the interface that groups the methods to encode and decode
  111. // values.
  112. type ValueCodec interface {
  113. ValueEncoder
  114. ValueDecoder
  115. }
  116. // ValueEncoder is the interface implemented by types that can handle the encoding of a value.
  117. type ValueEncoder interface {
  118. EncodeValue(EncodeContext, bsonrw.ValueWriter, reflect.Value) error
  119. }
  120. // ValueEncoderFunc is an adapter function that allows a function with the correct signature to be
  121. // used as a ValueEncoder.
  122. type ValueEncoderFunc func(EncodeContext, bsonrw.ValueWriter, reflect.Value) error
  123. // EncodeValue implements the ValueEncoder interface.
  124. func (fn ValueEncoderFunc) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
  125. return fn(ec, vw, val)
  126. }
  127. // ValueDecoder is the interface implemented by types that can handle the decoding of a value.
  128. type ValueDecoder interface {
  129. DecodeValue(DecodeContext, bsonrw.ValueReader, reflect.Value) error
  130. }
  131. // ValueDecoderFunc is an adapter function that allows a function with the correct signature to be
  132. // used as a ValueDecoder.
  133. type ValueDecoderFunc func(DecodeContext, bsonrw.ValueReader, reflect.Value) error
  134. // DecodeValue implements the ValueDecoder interface.
  135. func (fn ValueDecoderFunc) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
  136. return fn(dc, vr, val)
  137. }
  138. // CodecZeroer is the interface implemented by Codecs that can also determine if
  139. // a value of the type that would be encoded is zero.
  140. type CodecZeroer interface {
  141. IsTypeZero(interface{}) bool
  142. }
上海开阖软件有限公司 沪ICP备12045867号-1