From 1ae03773368b0b56cbc54f3980c1e915cdfe6dcf Mon Sep 17 00:00:00 2001 From: Qingyang Hu Date: Mon, 18 Nov 2024 16:48:16 -0500 Subject: [PATCH 1/3] GODRIVER-3425 Reference ObjectIdAsHexString decoder option in exception message --- bson/string_codec.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bson/string_codec.go b/bson/string_codec.go index a780677013..e24db39bce 100644 --- a/bson/string_codec.go +++ b/bson/string_codec.go @@ -7,6 +7,7 @@ package bson import ( + "errors" "fmt" "reflect" ) @@ -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") + msg := "decoding an object ID into a plain string is not supported " + + "(set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal representation)" + return emptyValue, errors.New(msg) } case TypeSymbol: str, err = vr.ReadSymbol() From 7c96deeeac370cc25ad58aa98142e56dac9e1e52 Mon Sep 17 00:00:00 2001 From: Qingyang Hu Date: Tue, 19 Nov 2024 09:48:57 -0500 Subject: [PATCH 2/3] update test case --- bson/decoder_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bson/decoder_test.go b/bson/decoder_test.go index 4cebea9d0e..7b91590666 100644 --- a/bson/decoder_test.go +++ b/bson/decoder_test.go @@ -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 plain string is not supported (set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal representation)" + assert.EqualError(t, err, want) }) t.Run("DefaultDocumentM top-level", func(t *testing.T) { t.Parallel() From 956efeb8a7c75324cebddadda91cd0c9608adead Mon Sep 17 00:00:00 2001 From: Qingyang Hu Date: Wed, 20 Nov 2024 10:00:40 -0500 Subject: [PATCH 3/3] update wording --- bson/decoder_test.go | 2 +- bson/string_codec.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bson/decoder_test.go b/bson/decoder_test.go index 7b91590666..e88aca6d6b 100644 --- a/bson/decoder_test.go +++ b/bson/decoder_test.go @@ -647,7 +647,7 @@ func TestDecoderConfiguration(t *testing.T) { var got objectIDTest err := dec.Decode(&got) - const want = "error decoding key id: decoding an object ID into a plain string is not supported (set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal representation)" + 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) { diff --git a/bson/string_codec.go b/bson/string_codec.go index e24db39bce..456028c1de 100644 --- a/bson/string_codec.go +++ b/bson/string_codec.go @@ -58,8 +58,8 @@ func (sc *stringCodec) decodeType(dc DecodeContext, vr ValueReader, t reflect.Ty } str = oid.Hex() } else { - msg := "decoding an object ID into a plain string is not supported " + - "(set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal representation)" + 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: