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 bsonx |
| 8 | |
| 9 | import ( |
| 10 | "encoding/binary" |
| 11 | "math" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/mongodb/mongo-go-driver/bson/bsontype" |
| 15 | "github.com/mongodb/mongo-go-driver/bson/primitive" |
| 16 | ) |
| 17 | |
| 18 | // IDoc is the interface implemented by Doc and MDoc. It allows either of these types to be provided |
| 19 | // to the Document function to create a Value. |
| 20 | type IDoc interface { |
| 21 | idoc() |
| 22 | } |
| 23 | |
| 24 | // Double constructs a BSON double Value. |
| 25 | func Double(f64 float64) Val { |
| 26 | v := Val{t: bsontype.Double} |
| 27 | binary.LittleEndian.PutUint64(v.bootstrap[0:8], math.Float64bits(f64)) |
| 28 | return v |
| 29 | } |
| 30 | |
| 31 | // String constructs a BSON string Value. |
| 32 | func String(str string) Val { return Val{t: bsontype.String}.writestring(str) } |
| 33 | |
| 34 | // Document constructs a Value from the given IDoc. If nil is provided, a BSON Null value will be |
| 35 | // returned. |
| 36 | func Document(doc IDoc) Val { |
| 37 | var v Val |
| 38 | switch tt := doc.(type) { |
| 39 | case Doc: |
| 40 | if tt == nil { |
| 41 | v.t = bsontype.Null |
| 42 | break |
| 43 | } |
| 44 | v.t = bsontype.EmbeddedDocument |
| 45 | v.primitive = tt |
| 46 | case MDoc: |
| 47 | if tt == nil { |
| 48 | v.t = bsontype.Null |
| 49 | break |
| 50 | } |
| 51 | v.t = bsontype.EmbeddedDocument |
| 52 | v.primitive = tt |
| 53 | default: |
| 54 | v.t = bsontype.Null |
| 55 | } |
| 56 | return v |
| 57 | } |
| 58 | |
| 59 | // Array constructs a Value from arr. If arr is nil, a BSON Null value is returned. |
| 60 | func Array(arr Arr) Val { |
| 61 | if arr == nil { |
| 62 | return Val{t: bsontype.Null} |
| 63 | } |
| 64 | return Val{t: bsontype.Array, primitive: arr} |
| 65 | } |
| 66 | |
| 67 | // Binary constructs a BSON binary Value. |
| 68 | func Binary(subtype byte, data []byte) Val { |
| 69 | return Val{t: bsontype.Binary, primitive: primitive.Binary{Subtype: subtype, Data: data}} |
| 70 | } |
| 71 | |
| 72 | // Undefined constructs a BSON binary Value. |
| 73 | func Undefined() Val { return Val{t: bsontype.Undefined} } |
| 74 | |
| 75 | // ObjectID constructs a BSON objectid Value. |
| 76 | func ObjectID(oid primitive.ObjectID) Val { |
| 77 | v := Val{t: bsontype.ObjectID} |
| 78 | copy(v.bootstrap[0:12], oid[:]) |
| 79 | return v |
| 80 | } |
| 81 | |
| 82 | // Boolean constructs a BSON boolean Value. |
| 83 | func Boolean(b bool) Val { |
| 84 | v := Val{t: bsontype.Boolean} |
| 85 | if b { |
| 86 | v.bootstrap[0] = 0x01 |
| 87 | } |
| 88 | return v |
| 89 | } |
| 90 | |
| 91 | // DateTime constructs a BSON datetime Value. |
| 92 | func DateTime(dt int64) Val { return Val{t: bsontype.DateTime}.writei64(dt) } |
| 93 | |
| 94 | // Time constructs a BSON datetime Value. |
| 95 | func Time(t time.Time) Val { |
| 96 | return Val{t: bsontype.DateTime}.writei64(t.Unix()*1e3 + int64(t.Nanosecond()/1e6)) |
| 97 | } |
| 98 | |
| 99 | // Null constructs a BSON binary Value. |
| 100 | func Null() Val { return Val{t: bsontype.Null} } |
| 101 | |
| 102 | // Regex constructs a BSON regex Value. |
| 103 | func Regex(pattern, options string) Val { |
| 104 | regex := primitive.Regex{Pattern: pattern, Options: options} |
| 105 | return Val{t: bsontype.Regex, primitive: regex} |
| 106 | } |
| 107 | |
| 108 | // DBPointer constructs a BSON dbpointer Value. |
| 109 | func DBPointer(ns string, ptr primitive.ObjectID) Val { |
| 110 | dbptr := primitive.DBPointer{DB: ns, Pointer: ptr} |
| 111 | return Val{t: bsontype.DBPointer, primitive: dbptr} |
| 112 | } |
| 113 | |
| 114 | // JavaScript constructs a BSON javascript Value. |
| 115 | func JavaScript(js string) Val { |
| 116 | return Val{t: bsontype.JavaScript}.writestring(js) |
| 117 | } |
| 118 | |
| 119 | // Symbol constructs a BSON symbol Value. |
| 120 | func Symbol(symbol string) Val { |
| 121 | return Val{t: bsontype.Symbol}.writestring(symbol) |
| 122 | } |
| 123 | |
| 124 | // CodeWithScope constructs a BSON code with scope Value. |
| 125 | func CodeWithScope(code string, scope IDoc) Val { |
| 126 | cws := primitive.CodeWithScope{Code: primitive.JavaScript(code), Scope: scope} |
| 127 | return Val{t: bsontype.CodeWithScope, primitive: cws} |
| 128 | } |
| 129 | |
| 130 | // Int32 constructs a BSON int32 Value. |
| 131 | func Int32(i32 int32) Val { |
| 132 | v := Val{t: bsontype.Int32} |
| 133 | v.bootstrap[0] = byte(i32) |
| 134 | v.bootstrap[1] = byte(i32 >> 8) |
| 135 | v.bootstrap[2] = byte(i32 >> 16) |
| 136 | v.bootstrap[3] = byte(i32 >> 24) |
| 137 | return v |
| 138 | } |
| 139 | |
| 140 | // Timestamp constructs a BSON timestamp Value. |
| 141 | func Timestamp(t, i uint32) Val { |
| 142 | v := Val{t: bsontype.Timestamp} |
| 143 | v.bootstrap[0] = byte(i) |
| 144 | v.bootstrap[1] = byte(i >> 8) |
| 145 | v.bootstrap[2] = byte(i >> 16) |
| 146 | v.bootstrap[3] = byte(i >> 24) |
| 147 | v.bootstrap[4] = byte(t) |
| 148 | v.bootstrap[5] = byte(t >> 8) |
| 149 | v.bootstrap[6] = byte(t >> 16) |
| 150 | v.bootstrap[7] = byte(t >> 24) |
| 151 | return v |
| 152 | } |
| 153 | |
| 154 | // Int64 constructs a BSON int64 Value. |
| 155 | func Int64(i64 int64) Val { return Val{t: bsontype.Int64}.writei64(i64) } |
| 156 | |
| 157 | // Decimal128 constructs a BSON decimal128 Value. |
| 158 | func Decimal128(d128 primitive.Decimal128) Val { |
| 159 | return Val{t: bsontype.Decimal128, primitive: d128} |
| 160 | } |
| 161 | |
| 162 | // MinKey constructs a BSON minkey Value. |
| 163 | func MinKey() Val { return Val{t: bsontype.MinKey} } |
| 164 | |
| 165 | // MaxKey constructs a BSON maxkey Value. |
| 166 | func MaxKey() Val { return Val{t: bsontype.MaxKey} } |