@@ -32,6 +32,8 @@ import (
32
32
"encoding/json"
33
33
"encoding/xml"
34
34
"errors"
35
+ "fmt"
36
+ "math/rand"
35
37
"net/url"
36
38
"reflect"
37
39
"strings"
@@ -286,12 +288,12 @@ func (s *S) TestPtrInline(c *C) {
286
288
},
287
289
{
288
290
// nil embed struct
289
- In : & inlinePtrStruct {A : 3 },
291
+ In : & inlinePtrStruct {A : 3 },
290
292
Out : bson.M {"a" : 3 },
291
293
},
292
294
{
293
295
// nil embed struct
294
- In : & inlinePtrPtrStruct {B : 5 },
296
+ In : & inlinePtrPtrStruct {B : 5 },
295
297
Out : bson.M {"b" : 5 },
296
298
},
297
299
}
@@ -1205,18 +1207,18 @@ type inlineBadKeyMap struct {
1205
1207
M map [int ]int `bson:",inline"`
1206
1208
}
1207
1209
type inlineUnexported struct {
1208
- M map [string ]interface {} `bson:",inline"`
1209
- unexported `bson:",inline"`
1210
+ M map [string ]interface {} `bson:",inline"`
1211
+ unexported `bson:",inline"`
1210
1212
}
1211
1213
type MStruct struct {
1212
1214
M int `bson:"m,omitempty"`
1213
1215
}
1214
1216
type inlinePtrStruct struct {
1215
- A int
1217
+ A int
1216
1218
* MStruct `bson:",inline"`
1217
1219
}
1218
1220
type inlinePtrPtrStruct struct {
1219
- B int
1221
+ B int
1220
1222
* inlinePtrStruct `bson:",inline"`
1221
1223
}
1222
1224
type unexported struct {
@@ -1274,11 +1276,11 @@ func (s ifaceSlice) GetBSON() (interface{}, error) {
1274
1276
1275
1277
type (
1276
1278
MyString string
1277
- MyBytes []byte
1278
- MyBool bool
1279
- MyD []bson.DocElem
1280
- MyRawD []bson.RawDocElem
1281
- MyM map [string ]interface {}
1279
+ MyBytes []byte
1280
+ MyBool bool
1281
+ MyD []bson.DocElem
1282
+ MyRawD []bson.RawDocElem
1283
+ MyM map [string ]interface {}
1282
1284
)
1283
1285
1284
1286
var (
@@ -1989,3 +1991,49 @@ func (s *S) TestMarshalRespectNil(c *C) {
1989
1991
c .Assert (testStruct2 .Map , NotNil )
1990
1992
c .Assert (testStruct2 .MapPtr , NotNil )
1991
1993
}
1994
+
1995
+ func (s * S ) TestMongoTimestampTime (c * C ) {
1996
+ t := time .Now ()
1997
+ ts , err := bson .NewMongoTimestamp (t , 123 )
1998
+ c .Assert (err , IsNil )
1999
+ c .Assert (ts .Time ().Unix (), Equals , t .Unix ())
2000
+ }
2001
+
2002
+ func (s * S ) TestMongoTimestampCounter (c * C ) {
2003
+ rnd := rand .Uint32 ()
2004
+ ts , err := bson .NewMongoTimestamp (time .Now (), rnd )
2005
+ c .Assert (err , IsNil )
2006
+ c .Assert (ts .Counter (), Equals , rnd )
2007
+ }
2008
+
2009
+ func (s * S ) TestMongoTimestampError (c * C ) {
2010
+ t := time .Date (1969 , time .December , 31 , 23 , 59 , 59 , 999 , time .UTC )
2011
+ ts , err := bson .NewMongoTimestamp (t , 321 )
2012
+ c .Assert (int64 (ts ), Equals , int64 (- 1 ))
2013
+ c .Assert (err , ErrorMatches , "invalid value for time" )
2014
+ }
2015
+
2016
+ func ExampleNewMongoTimestamp () {
2017
+
2018
+ var counter uint32 = 1
2019
+ var t time.Time
2020
+
2021
+ for i := 1 ; i <= 3 ; i ++ {
2022
+
2023
+ if c := time .Now (); t .Unix () == c .Unix () {
2024
+ counter ++
2025
+ } else {
2026
+ t = c
2027
+ counter = 1
2028
+ }
2029
+
2030
+ ts , err := bson .NewMongoTimestamp (t , counter )
2031
+ if err != nil {
2032
+ fmt .Printf ("NewMongoTimestamp error: %v" , err )
2033
+ } else {
2034
+ fmt .Printf ("NewMongoTimestamp encoded timestamp: %d\n " , ts )
2035
+ }
2036
+
2037
+ time .Sleep (500 * time .Millisecond )
2038
+ }
2039
+ }
0 commit comments