Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Apr 24, 2024
1 parent b24ab57 commit 9f587a2
Show file tree
Hide file tree
Showing 18 changed files with 563 additions and 1,514 deletions.
18 changes: 13 additions & 5 deletions bson/bsoncodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,12 @@ type DecodeContext struct {
// error. DocumentType overrides the Ancestor field.
defaultDocumentType reflect.Type

binaryAsSlice bool
useJSONStructTags bool
useLocalTimeZone bool
zeroMaps bool
zeroStructs bool
binaryAsSlice bool
decodeObjectIDAsHex bool
useJSONStructTags bool
useLocalTimeZone bool
zeroMaps bool
zeroStructs bool
}

// BinaryAsSlice causes the Decoder to unmarshal BSON binary field values that are the "Generic" or
Expand All @@ -194,6 +195,13 @@ func (dc *DecodeContext) BinaryAsSlice() {
dc.binaryAsSlice = true
}

// DecodeObjectIDAsHex causes the Decoder to unmarshal BSON ObjectID as a hexadecimal string.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.Decoder.DecodeObjectIDAsHex] instead.
func (dc *DecodeContext) DecodeObjectIDAsHex() {
dc.decodeObjectIDAsHex = true
}

// UseJSONStructTags causes the Decoder to fall back to using the "json" struct tag if a "bson"
// struct tag is not specified.
//
Expand Down
19 changes: 14 additions & 5 deletions bson/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ type Decoder struct {
defaultDocumentM bool
defaultDocumentD bool

binaryAsSlice bool
useJSONStructTags bool
useLocalTimeZone bool
zeroMaps bool
zeroStructs bool
binaryAsSlice bool
decodeObjectIDAsHex bool
useJSONStructTags bool
useLocalTimeZone bool
zeroMaps bool
zeroStructs bool
}

// NewDecoder returns a new decoder that uses the DefaultRegistry to read from vr.
Expand Down Expand Up @@ -93,6 +94,9 @@ func (d *Decoder) Decode(val interface{}) error {
if d.binaryAsSlice {
d.dc.BinaryAsSlice()
}
if d.decodeObjectIDAsHex {
d.dc.DecodeObjectIDAsHex()
}
if d.useJSONStructTags {
d.dc.UseJSONStructTags()
}
Expand Down Expand Up @@ -145,6 +149,11 @@ func (d *Decoder) BinaryAsSlice() {
d.binaryAsSlice = true
}

// DecodeObjectIDAsHex causes the Decoder to unmarshal BSON ObjectID as a hexadecimal string.
func (d *Decoder) DecodeObjectIDAsHex() {
d.decodeObjectIDAsHex = true
}

// UseJSONStructTags causes the Decoder to fall back to using the "json" struct tag if a "bson"
// struct tag is not specified.
func (d *Decoder) UseJSONStructTags() {
Expand Down
Loading

0 comments on commit 9f587a2

Please sign in to comment.