Skip to content

Commit d5470bb

Browse files
committed
Merge branch 'master' of https://github.com/1995parham/mongo-go-driver into master
Change-Id: I48ce43198453a241b1747c56da55c7847689faae
2 parents ae1ce2a + cc824fa commit d5470bb

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

bson/encode.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"reflect"
1515
"strconv"
1616
"strings"
17+
"time"
1718

1819
"github.com/mongodb/mongo-go-driver/bson/objectid"
1920
)
@@ -564,11 +565,18 @@ func (e *encoder) elemFromValue(key string, val reflect.Value, minsize bool) (*E
564565
elem = EC.ArrayFromElements(key, arrayElems...)
565566
}
566567
case reflect.Struct:
567-
structElems, err := e.encodeStruct(val)
568-
if err != nil {
569-
return nil, err
568+
switch val.Interface().(type) {
569+
case time.Time:
570+
t := val.Interface().(time.Time)
571+
572+
elem = EC.DateTime(key, t.UnixNano()/int64(time.Millisecond))
573+
default:
574+
structElems, err := e.encodeStruct(val)
575+
if err != nil {
576+
return nil, err
577+
}
578+
elem = EC.SubDocumentFromElements(key, structElems...)
570579
}
571-
elem = EC.SubDocumentFromElements(key, structElems...)
572580
default:
573581
return nil, fmt.Errorf("Unsupported value type %s", val.Kind())
574582
}
@@ -661,11 +669,18 @@ func (e *encoder) valueFromValue(val reflect.Value, minsize bool) (*Value, error
661669
elem = VC.ArrayFromValues(arrayElems...)
662670
}
663671
case reflect.Struct:
664-
structElems, err := e.encodeStruct(val)
665-
if err != nil {
666-
return nil, err
672+
switch val.Interface().(type) {
673+
case time.Time:
674+
t := val.Interface().(time.Time)
675+
676+
elem = VC.DateTime(t.UnixNano() / int64(time.Millisecond))
677+
default:
678+
structElems, err := e.encodeStruct(val)
679+
if err != nil {
680+
return nil, err
681+
}
682+
elem = VC.DocumentFromElements(structElems...)
667683
}
668-
elem = VC.DocumentFromElements(structElems...)
669684
default:
670685
return nil, fmt.Errorf("Unsupported value type %s", val.Kind())
671686
}

bson/encode_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"bytes"
1111
"io"
1212
"testing"
13+
"time"
1314

1415
"github.com/google/go-cmp/cmp"
1516
"github.com/mongodb/mongo-go-driver/bson/objectid"
@@ -360,6 +361,7 @@ func reflectionEncoderTest(t *testing.T) {
360361
oids := []objectid.ObjectID{objectid.New(), objectid.New(), objectid.New()}
361362
var str = new(string)
362363
*str = "bar"
364+
now := time.Now()
363365

364366
testCases := []struct {
365367
name string
@@ -738,6 +740,7 @@ func reflectionEncoderTest(t *testing.T) {
738740
V _Interface
739741
W map[struct{}]struct{}
740742
X map[struct{}]struct{}
743+
Z time.Time
741744
}{
742745
A: true,
743746
B: 123,
@@ -766,6 +769,7 @@ func reflectionEncoderTest(t *testing.T) {
766769
V: _Interface((*_impl)(nil)), // typed nil
767770
W: map[struct{}]struct{}{},
768771
X: nil,
772+
Z: now,
769773
},
770774
docToBytes(NewDocument(
771775
EC.Boolean("a", true),
@@ -791,6 +795,7 @@ func reflectionEncoderTest(t *testing.T) {
791795
EC.Null("v"),
792796
EC.SubDocument("w", NewDocument()),
793797
EC.Null("x"),
798+
EC.DateTime("z", now.UnixNano()/int64(time.Millisecond)),
794799
)),
795800
nil,
796801
},
@@ -823,6 +828,7 @@ func reflectionEncoderTest(t *testing.T) {
823828
W []map[struct{}]struct{}
824829
X []map[struct{}]struct{}
825830
Y []map[struct{}]struct{}
831+
Z []time.Time
826832
}{
827833
A: []bool{true},
828834
B: []int32{123},
@@ -854,6 +860,7 @@ func reflectionEncoderTest(t *testing.T) {
854860
W: nil,
855861
X: []map[struct{}]struct{}{}, // Should be empty BSON Array
856862
Y: []map[struct{}]struct{}{{}}, // Should be BSON array with one element, an empty BSON SubDocument
863+
Z: []time.Time{now, now},
857864
},
858865
docToBytes(NewDocument(
859866
EC.ArrayFromElements("a", VC.Boolean(true)),
@@ -880,6 +887,7 @@ func reflectionEncoderTest(t *testing.T) {
880887
EC.Null("w"),
881888
EC.Array("x", NewArray()),
882889
EC.ArrayFromElements("y", VC.Document(NewDocument())),
890+
EC.ArrayFromElements("z", VC.DateTime(now.UnixNano()/int64(time.Millisecond)), VC.DateTime(now.UnixNano()/int64(time.Millisecond))),
883891
)),
884892
nil,
885893
},

0 commit comments

Comments
 (0)