Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame] | 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 | |
| 7 | // Package primitive contains types similar to Go primitives for BSON types can do not have direct |
| 8 | // Go primitive representations. |
| 9 | package primitive |
| 10 | |
| 11 | import ( |
| 12 | "bytes" |
| 13 | "fmt" |
| 14 | ) |
| 15 | |
| 16 | // Binary represents a BSON binary value. |
| 17 | type Binary struct { |
| 18 | Subtype byte |
| 19 | Data []byte |
| 20 | } |
| 21 | |
| 22 | // Equal compaes bp to bp2 and returns true is the are equal. |
| 23 | func (bp Binary) Equal(bp2 Binary) bool { |
| 24 | if bp.Subtype != bp2.Subtype { |
| 25 | return false |
| 26 | } |
| 27 | return bytes.Equal(bp.Data, bp2.Data) |
| 28 | } |
| 29 | |
| 30 | // Undefined represents the BSON undefined value type. |
| 31 | type Undefined struct{} |
| 32 | |
| 33 | // DateTime represents the BSON datetime value. |
| 34 | type DateTime int64 |
| 35 | |
| 36 | // Null repreesnts the BSON null value. |
| 37 | type Null struct{} |
| 38 | |
| 39 | // Regex represents a BSON regex value. |
| 40 | type Regex struct { |
| 41 | Pattern string |
| 42 | Options string |
| 43 | } |
| 44 | |
| 45 | func (rp Regex) String() string { |
| 46 | return fmt.Sprintf(`{"pattern": "%s", "options": "%s"}`, rp.Pattern, rp.Options) |
| 47 | } |
| 48 | |
| 49 | // Equal compaes rp to rp2 and returns true is the are equal. |
| 50 | func (rp Regex) Equal(rp2 Regex) bool { |
| 51 | return rp.Pattern == rp2.Pattern && rp.Options == rp.Options |
| 52 | } |
| 53 | |
| 54 | // DBPointer represents a BSON dbpointer value. |
| 55 | type DBPointer struct { |
| 56 | DB string |
| 57 | Pointer ObjectID |
| 58 | } |
| 59 | |
| 60 | func (d DBPointer) String() string { |
| 61 | return fmt.Sprintf(`{"db": "%s", "pointer": "%s"}`, d.DB, d.Pointer) |
| 62 | } |
| 63 | |
| 64 | // Equal compaes d to d2 and returns true is the are equal. |
| 65 | func (d DBPointer) Equal(d2 DBPointer) bool { |
| 66 | return d.DB == d2.DB && bytes.Equal(d.Pointer[:], d2.Pointer[:]) |
| 67 | } |
| 68 | |
| 69 | // JavaScript represents a BSON JavaScript code value. |
| 70 | type JavaScript string |
| 71 | |
| 72 | // Symbol represents a BSON symbol value. |
| 73 | type Symbol string |
| 74 | |
| 75 | // CodeWithScope represents a BSON JavaScript code with scope value. |
| 76 | type CodeWithScope struct { |
| 77 | Code JavaScript |
| 78 | Scope interface{} |
| 79 | } |
| 80 | |
| 81 | func (cws CodeWithScope) String() string { |
| 82 | return fmt.Sprintf(`{"code": "%s", "scope": %v}`, cws.Code, cws.Scope) |
| 83 | } |
| 84 | |
| 85 | // Timestamp represents a BSON timestamp value. |
| 86 | type Timestamp struct { |
| 87 | T uint32 |
| 88 | I uint32 |
| 89 | } |
| 90 | |
| 91 | // Equal compaes tp to tp2 and returns true is the are equal. |
| 92 | func (tp Timestamp) Equal(tp2 Timestamp) bool { |
| 93 | return tp.T == tp2.T && tp.I == tp2.I |
| 94 | } |
| 95 | |
| 96 | // MinKey represents the BSON minkey value. |
| 97 | type MinKey struct{} |
| 98 | |
| 99 | // MaxKey represents the BSON maxkey value. |
| 100 | type MaxKey struct{} |
| 101 | |
| 102 | // D represents a BSON Document. This type can be used to represent BSON in a concise and readable |
| 103 | // manner. It should generally be used when serializing to BSON. For deserializing, the Raw or |
| 104 | // Document types should be used. |
| 105 | // |
| 106 | // Example usage: |
| 107 | // |
| 108 | // primitive.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}} |
| 109 | // |
| 110 | // This type should be used in situations where order matters, such as MongoDB commands. If the |
| 111 | // order is not important, a map is more comfortable and concise. |
| 112 | type D []E |
| 113 | |
| 114 | // Map creates a map from the elements of the D. |
| 115 | func (d D) Map() M { |
| 116 | m := make(M, len(d)) |
| 117 | for _, e := range d { |
| 118 | m[e.Key] = e.Value |
| 119 | } |
| 120 | return m |
| 121 | } |
| 122 | |
| 123 | // E represents a BSON element for a D. It is usually used inside a D. |
| 124 | type E struct { |
| 125 | Key string |
| 126 | Value interface{} |
| 127 | } |
| 128 | |
| 129 | // M is an unordered, concise representation of a BSON Document. It should generally be used to |
| 130 | // serialize BSON when the order of the elements of a BSON document do not matter. If the element |
| 131 | // order matters, use a D instead. |
| 132 | // |
| 133 | // Example usage: |
| 134 | // |
| 135 | // primitive.M{"foo": "bar", "hello": "world", "pi": 3.14159} |
| 136 | // |
| 137 | // This type is handled in the encoders as a regular map[string]interface{}. The elements will be |
| 138 | // serialized in an undefined, random order, and the order will be different each time. |
| 139 | type M map[string]interface{} |
| 140 | |
| 141 | // An A represents a BSON array. This type can be used to represent a BSON array in a concise and |
| 142 | // readable manner. It should generally be used when serializing to BSON. For deserializing, the |
| 143 | // RawArray or Array types should be used. |
| 144 | // |
| 145 | // Example usage: |
| 146 | // |
| 147 | // primitive.A{"bar", "world", 3.14159, primitive.D{{"qux", 12345}}} |
| 148 | // |
| 149 | type A []interface{} |