Skip to content

Commit

Permalink
GODRIVER-3425 Reference ObjectIdAsHexString decoder option in excepti…
Browse files Browse the repository at this point in the history
…on message. (mongodb#1896)
  • Loading branch information
qingyang-hu authored Nov 21, 2024
1 parent 17aeb0b commit 1ada5fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
3 changes: 2 additions & 1 deletion bson/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,8 @@ func TestDecoderConfiguration(t *testing.T) {

var got objectIDTest
err := dec.Decode(&got)
assert.EqualError(t, err, "error decoding key id: decoding an object ID to a non-hexadecimal string representation is not supported")
const want = "error decoding key id: decoding an object ID into a string is not supported by default (set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal string)"
assert.EqualError(t, err, want)
})
t.Run("DefaultDocumentM top-level", func(t *testing.T) {
t.Parallel()
Expand Down
13 changes: 8 additions & 5 deletions bson/string_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bson

import (
"errors"
"fmt"
"reflect"
)
Expand Down Expand Up @@ -50,14 +51,16 @@ func (sc *stringCodec) decodeType(dc DecodeContext, vr ValueReader, t reflect.Ty
return emptyValue, err
}
case TypeObjectID:
oid, err := vr.ReadObjectID()
if err != nil {
return emptyValue, err
}
if dc.objectIDAsHexString {
oid, err := vr.ReadObjectID()
if err != nil {
return emptyValue, err
}
str = oid.Hex()
} else {
return emptyValue, fmt.Errorf("decoding an object ID to a non-hexadecimal string representation is not supported")
const msg = "decoding an object ID into a string is not supported by default " +
"(set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal string)"
return emptyValue, errors.New(msg)
}
case TypeSymbol:
str, err = vr.ReadSymbol()
Expand Down

0 comments on commit 1ada5fa

Please sign in to comment.