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 666fad1
Show file tree
Hide file tree
Showing 17 changed files with 553 additions and 1,510 deletions.
18 changes: 13 additions & 5 deletions bson/bsoncodec.go
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.binaryAsSlice = 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
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.binaryAsSlice = 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

0 comments on commit 666fad1

Please sign in to comment.