Skip to content

Commit 06bdfb1

Browse files
committed
Adds support for go time struct.
1 parent 5f9341c commit 06bdfb1

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

bson/encode.go

Lines changed: 12 additions & 4 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
}

0 commit comments

Comments
 (0)