primitive.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 primitive contains types similar to Go primitives for BSON types that do not have direct
  7. // Go primitive representations.
  8. package primitive // import "go.mongodb.org/mongo-driver/bson/primitive"
  9. import (
  10. "bytes"
  11. "encoding/json"
  12. "fmt"
  13. "time"
  14. )
  15. // Binary represents a BSON binary value.
  16. type Binary struct {
  17. Subtype byte
  18. Data []byte
  19. }
  20. // Equal compares bp to bp2 and returns true if they are equal.
  21. func (bp Binary) Equal(bp2 Binary) bool {
  22. if bp.Subtype != bp2.Subtype {
  23. return false
  24. }
  25. return bytes.Equal(bp.Data, bp2.Data)
  26. }
  27. // IsZero returns if bp is the empty Binary.
  28. func (bp Binary) IsZero() bool {
  29. return bp.Subtype == 0 && len(bp.Data) == 0
  30. }
  31. // Undefined represents the BSON undefined value type.
  32. type Undefined struct{}
  33. // DateTime represents the BSON datetime value.
  34. type DateTime int64
  35. var _ json.Marshaler = DateTime(0)
  36. var _ json.Unmarshaler = (*DateTime)(nil)
  37. // MarshalJSON marshal to time type.
  38. func (d DateTime) MarshalJSON() ([]byte, error) {
  39. return json.Marshal(d.Time().UTC())
  40. }
  41. // UnmarshalJSON creates a primitive.DateTime from a JSON string.
  42. func (d *DateTime) UnmarshalJSON(data []byte) error {
  43. // Ignore "null" to keep parity with the time.Time type and the standard library. Decoding "null" into a non-pointer
  44. // DateTime field will leave the field unchanged. For pointer values, the encoding/json will set the pointer to nil
  45. // and will not defer to the UnmarshalJSON hook.
  46. if string(data) == "null" {
  47. return nil
  48. }
  49. var tempTime time.Time
  50. if err := json.Unmarshal(data, &tempTime); err != nil {
  51. return err
  52. }
  53. *d = NewDateTimeFromTime(tempTime)
  54. return nil
  55. }
  56. // Time returns the date as a time type.
  57. func (d DateTime) Time() time.Time {
  58. return time.Unix(int64(d)/1000, int64(d)%1000*1000000)
  59. }
  60. // NewDateTimeFromTime creates a new DateTime from a Time.
  61. func NewDateTimeFromTime(t time.Time) DateTime {
  62. return DateTime(t.Unix()*1e3 + int64(t.Nanosecond())/1e6)
  63. }
  64. // Null represents the BSON null value.
  65. type Null struct{}
  66. // Regex represents a BSON regex value.
  67. type Regex struct {
  68. Pattern string
  69. Options string
  70. }
  71. func (rp Regex) String() string {
  72. return fmt.Sprintf(`{"pattern": "%s", "options": "%s"}`, rp.Pattern, rp.Options)
  73. }
  74. // Equal compares rp to rp2 and returns true if they are equal.
  75. func (rp Regex) Equal(rp2 Regex) bool {
  76. return rp.Pattern == rp2.Pattern && rp.Options == rp2.Options
  77. }
  78. // IsZero returns if rp is the empty Regex.
  79. func (rp Regex) IsZero() bool {
  80. return rp.Pattern == "" && rp.Options == ""
  81. }
  82. // DBPointer represents a BSON dbpointer value.
  83. type DBPointer struct {
  84. DB string
  85. Pointer ObjectID
  86. }
  87. func (d DBPointer) String() string {
  88. return fmt.Sprintf(`{"db": "%s", "pointer": "%s"}`, d.DB, d.Pointer)
  89. }
  90. // Equal compares d to d2 and returns true if they are equal.
  91. func (d DBPointer) Equal(d2 DBPointer) bool {
  92. return d == d2
  93. }
  94. // IsZero returns if d is the empty DBPointer.
  95. func (d DBPointer) IsZero() bool {
  96. return d.DB == "" && d.Pointer.IsZero()
  97. }
  98. // JavaScript represents a BSON JavaScript code value.
  99. type JavaScript string
  100. // Symbol represents a BSON symbol value.
  101. type Symbol string
  102. // CodeWithScope represents a BSON JavaScript code with scope value.
  103. type CodeWithScope struct {
  104. Code JavaScript
  105. Scope interface{}
  106. }
  107. func (cws CodeWithScope) String() string {
  108. return fmt.Sprintf(`{"code": "%s", "scope": %v}`, cws.Code, cws.Scope)
  109. }
  110. // Timestamp represents a BSON timestamp value.
  111. type Timestamp struct {
  112. T uint32
  113. I uint32
  114. }
  115. // After reports whether the time instant tp is after tp2.
  116. func (tp Timestamp) After(tp2 Timestamp) bool {
  117. return tp.T > tp2.T || (tp.T == tp2.T && tp.I > tp2.I)
  118. }
  119. // Before reports whether the time instant tp is before tp2.
  120. func (tp Timestamp) Before(tp2 Timestamp) bool {
  121. return tp.T < tp2.T || (tp.T == tp2.T && tp.I < tp2.I)
  122. }
  123. // Equal compares tp to tp2 and returns true if they are equal.
  124. func (tp Timestamp) Equal(tp2 Timestamp) bool {
  125. return tp.T == tp2.T && tp.I == tp2.I
  126. }
  127. // IsZero returns if tp is the zero Timestamp.
  128. func (tp Timestamp) IsZero() bool {
  129. return tp.T == 0 && tp.I == 0
  130. }
  131. // Compare compares the time instant tp with tp2. If tp is before tp2, it returns -1; if tp is after
  132. // tp2, it returns +1; if they're the same, it returns 0.
  133. func (tp Timestamp) Compare(tp2 Timestamp) int {
  134. switch {
  135. case tp.Equal(tp2):
  136. return 0
  137. case tp.Before(tp2):
  138. return -1
  139. default:
  140. return +1
  141. }
  142. }
  143. // CompareTimestamp compares the time instant tp with tp2. If tp is before tp2, it returns -1; if tp is after
  144. // tp2, it returns +1; if they're the same, it returns 0.
  145. //
  146. // Deprecated: Use Timestamp.Compare instead.
  147. func CompareTimestamp(tp, tp2 Timestamp) int {
  148. return tp.Compare(tp2)
  149. }
  150. // MinKey represents the BSON minkey value.
  151. type MinKey struct{}
  152. // MaxKey represents the BSON maxkey value.
  153. type MaxKey struct{}
  154. // D is an ordered representation of a BSON document. This type should be used when the order of the elements matters,
  155. // such as MongoDB command documents. If the order of the elements does not matter, an M should be used instead.
  156. //
  157. // Example usage:
  158. //
  159. // bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
  160. type D []E
  161. // Map creates a map from the elements of the D.
  162. //
  163. // Deprecated: Converting directly from a D to an M will not be supported in Go Driver 2.0. Instead,
  164. // users should marshal the D to BSON using bson.Marshal and unmarshal it to M using bson.Unmarshal.
  165. func (d D) Map() M {
  166. m := make(M, len(d))
  167. for _, e := range d {
  168. m[e.Key] = e.Value
  169. }
  170. return m
  171. }
  172. // E represents a BSON element for a D. It is usually used inside a D.
  173. type E struct {
  174. Key string
  175. Value interface{}
  176. }
  177. // M is an unordered representation of a BSON document. This type should be used when the order of the elements does not
  178. // matter. This type is handled as a regular map[string]interface{} when encoding and decoding. Elements will be
  179. // serialized in an undefined, random order. If the order of the elements matters, a D should be used instead.
  180. //
  181. // Example usage:
  182. //
  183. // bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}
  184. type M map[string]interface{}
  185. // An A is an ordered representation of a BSON array.
  186. //
  187. // Example usage:
  188. //
  189. // bson.A{"bar", "world", 3.14159, bson.D{{"qux", 12345}}}
  190. type A []interface{}