primitive.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 can 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 is the 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. // MarshalJSON marshal to time type
  36. func (d DateTime) MarshalJSON() ([]byte, error) {
  37. return json.Marshal(d.Time())
  38. }
  39. // Time returns the date as a time type.
  40. func (d DateTime) Time() time.Time {
  41. return time.Unix(int64(d)/1000, int64(d)%1000*1000000)
  42. }
  43. // NewDateTimeFromTime creates a new DateTime from a Time.
  44. func NewDateTimeFromTime(t time.Time) DateTime {
  45. return DateTime(t.UnixNano() / 1000000)
  46. }
  47. // Null represents the BSON null value.
  48. type Null struct{}
  49. // Regex represents a BSON regex value.
  50. type Regex struct {
  51. Pattern string
  52. Options string
  53. }
  54. func (rp Regex) String() string {
  55. return fmt.Sprintf(`{"pattern": "%s", "options": "%s"}`, rp.Pattern, rp.Options)
  56. }
  57. // Equal compares rp to rp2 and returns true is the are equal.
  58. func (rp Regex) Equal(rp2 Regex) bool {
  59. return rp.Pattern == rp2.Pattern && rp.Options == rp.Options
  60. }
  61. // IsZero returns if rp is the empty Regex
  62. func (rp Regex) IsZero() bool {
  63. return rp.Pattern == "" && rp.Options == ""
  64. }
  65. // DBPointer represents a BSON dbpointer value.
  66. type DBPointer struct {
  67. DB string
  68. Pointer ObjectID
  69. }
  70. func (d DBPointer) String() string {
  71. return fmt.Sprintf(`{"db": "%s", "pointer": "%s"}`, d.DB, d.Pointer)
  72. }
  73. // Equal compares d to d2 and returns true is the are equal.
  74. func (d DBPointer) Equal(d2 DBPointer) bool {
  75. return d.DB == d2.DB && bytes.Equal(d.Pointer[:], d2.Pointer[:])
  76. }
  77. // IsZero returns if d is the empty DBPointer
  78. func (d DBPointer) IsZero() bool {
  79. return d.DB == "" && d.Pointer.IsZero()
  80. }
  81. // JavaScript represents a BSON JavaScript code value.
  82. type JavaScript string
  83. // Symbol represents a BSON symbol value.
  84. type Symbol string
  85. // CodeWithScope represents a BSON JavaScript code with scope value.
  86. type CodeWithScope struct {
  87. Code JavaScript
  88. Scope interface{}
  89. }
  90. func (cws CodeWithScope) String() string {
  91. return fmt.Sprintf(`{"code": "%s", "scope": %v}`, cws.Code, cws.Scope)
  92. }
  93. // Timestamp represents a BSON timestamp value.
  94. type Timestamp struct {
  95. T uint32
  96. I uint32
  97. }
  98. // Equal compares tp to tp2 and returns true is the are equal.
  99. func (tp Timestamp) Equal(tp2 Timestamp) bool {
  100. return tp.T == tp2.T && tp.I == tp2.I
  101. }
  102. // IsZero returns if tp is the zero Timestamp
  103. func (tp Timestamp) IsZero() bool {
  104. return tp.T == 0 && tp.I == 0
  105. }
  106. // CompareTimestamp returns an integer comparing two Timestamps, where T is compared first, followed by I.
  107. // Returns 0 if tp = tp2, 1 if tp > tp2, -1 if tp < tp2.
  108. func CompareTimestamp(tp, tp2 Timestamp) int {
  109. if tp.Equal(tp2) {
  110. return 0
  111. }
  112. if tp.T > tp2.T {
  113. return 1
  114. }
  115. if tp.T < tp2.T {
  116. return -1
  117. }
  118. // Compare I values because T values are equal
  119. if tp.I > tp2.I {
  120. return 1
  121. }
  122. return -1
  123. }
  124. // MinKey represents the BSON minkey value.
  125. type MinKey struct{}
  126. // MaxKey represents the BSON maxkey value.
  127. type MaxKey struct{}
  128. // D represents a BSON Document. This type can be used to represent BSON in a concise and readable
  129. // manner. It should generally be used when serializing to BSON. For deserializing, the Raw or
  130. // Document types should be used.
  131. //
  132. // Example usage:
  133. //
  134. // primitive.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
  135. //
  136. // This type should be used in situations where order matters, such as MongoDB commands. If the
  137. // order is not important, a map is more comfortable and concise.
  138. type D []E
  139. // Map creates a map from the elements of the D.
  140. func (d D) Map() M {
  141. m := make(M, len(d))
  142. for _, e := range d {
  143. m[e.Key] = e.Value
  144. }
  145. return m
  146. }
  147. // E represents a BSON element for a D. It is usually used inside a D.
  148. type E struct {
  149. Key string
  150. Value interface{}
  151. }
  152. // M is an unordered, concise representation of a BSON Document. It should generally be used to
  153. // serialize BSON when the order of the elements of a BSON document do not matter. If the element
  154. // order matters, use a D instead.
  155. //
  156. // Example usage:
  157. //
  158. // primitive.M{"foo": "bar", "hello": "world", "pi": 3.14159}
  159. //
  160. // This type is handled in the encoders as a regular map[string]interface{}. The elements will be
  161. // serialized in an undefined, random order, and the order will be different each time.
  162. type M map[string]interface{}
  163. // An A represents a BSON array. This type can be used to represent a BSON array in a concise and
  164. // readable manner. It should generally be used when serializing to BSON. For deserializing, the
  165. // RawArray or Array types should be used.
  166. //
  167. // Example usage:
  168. //
  169. // primitive.A{"bar", "world", 3.14159, primitive.D{{"qux", 12345}}}
  170. //
  171. type A []interface{}