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

187 lines
4.9KB

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