Skip to content

Commit

Permalink
#97: Fixed DateTime UnmarshalBSONValue panics on a null bson value
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Dimov <3619341+dimovnike@users.noreply.github.com>
  • Loading branch information
dimovnike committed Feb 5, 2022
1 parent 6d7da0f commit 9835f0b
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"database/sql/driver"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"
Expand Down Expand Up @@ -227,6 +228,15 @@ func (t DateTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
// assumed to be valid. UnmarshalBSONValue must copy the BSON value bytes if it
// wishes to retain the data after returning.
func (t *DateTime) UnmarshalBSONValue(tpe bsontype.Type, data []byte) error {
if tpe == bsontype.Null {
*t = DateTime{}
return nil
}

if len(data) != 8 {
return errors.New("bson date field length not exactly 8 bytes")
}

i64 := int64(binary.LittleEndian.Uint64(data))
// TODO: Use bsonprim.DateTime.Time() method
*t = DateTime(time.Unix(i64/1000, i64%1000*1000000))
Expand Down

0 comments on commit 9835f0b

Please sign in to comment.