From cef198c452eca7f0f331460b3f900cde810871ab Mon Sep 17 00:00:00 2001 From: Qingyang Hu <103950869+qingyang-hu@users.noreply.github.com> Date: Thu, 27 Jun 2024 11:27:09 -0400 Subject: [PATCH] GODRIVER-2866 Update method signatures of `bson.ValueMarshaler`/`bson.ValueUnmarshaler` to only use basic types. (#1671) --- bson/default_value_decoders.go | 2 +- bson/default_value_encoders.go | 2 +- bson/default_value_encoders_test.go | 4 ++-- bson/marshal.go | 2 +- bson/marshal_value_cases_test.go | 6 +++--- bson/primitive_codecs_test.go | 8 ++++---- bson/unmarshal.go | 2 +- mongo/mongo.go | 4 ++-- mongo/mongo_test.go | 4 ++-- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/bson/default_value_decoders.go b/bson/default_value_decoders.go index 94cd947ec9..2075c0ad3d 100644 --- a/bson/default_value_decoders.go +++ b/bson/default_value_decoders.go @@ -1190,7 +1190,7 @@ func valueUnmarshalerDecodeValue(_ DecodeContext, vr ValueReader, val reflect.Va // NB: this error should be unreachable due to the above checks return ValueDecoderError{Name: "ValueUnmarshalerDecodeValue", Types: []reflect.Type{tValueUnmarshaler}, Received: val} } - return m.UnmarshalBSONValue(t, src) + return m.UnmarshalBSONValue(byte(t), src) } // unmarshalerDecodeValue is the ValueDecoderFunc for Unmarshaler implementations. diff --git a/bson/default_value_encoders.go b/bson/default_value_encoders.go index c3ef806cb6..2e8ae5830c 100644 --- a/bson/default_value_encoders.go +++ b/bson/default_value_encoders.go @@ -302,7 +302,7 @@ func valueMarshalerEncodeValue(_ EncodeContext, vw ValueWriter, val reflect.Valu if err != nil { return err } - return copyValueFromBytes(vw, t, data) + return copyValueFromBytes(vw, Type(t), data) } // marshalerEncodeValue is the ValueEncoderFunc for Marshaler implementations. diff --git a/bson/default_value_encoders_test.go b/bson/default_value_encoders_test.go index fb16750007..ba7b70de02 100644 --- a/bson/default_value_encoders_test.go +++ b/bson/default_value_encoders_test.go @@ -1856,8 +1856,8 @@ type testValueMarshalPtr struct { err error } -func (tvm *testValueMarshalPtr) MarshalBSONValue() (Type, []byte, error) { - return tvm.t, tvm.buf, tvm.err +func (tvm *testValueMarshalPtr) MarshalBSONValue() (byte, []byte, error) { + return byte(tvm.t), tvm.buf, tvm.err } type testMarshalPtr struct { diff --git a/bson/marshal.go b/bson/marshal.go index 573de16398..d6e687ddae 100644 --- a/bson/marshal.go +++ b/bson/marshal.go @@ -34,7 +34,7 @@ type Marshaler interface { // create custom BSON marshaling behavior for an entire BSON document, implement // the Marshaler interface instead. type ValueMarshaler interface { - MarshalBSONValue() (Type, []byte, error) + MarshalBSONValue() (typ byte, data []byte, err error) } // Pool of buffers for marshalling BSON. diff --git a/bson/marshal_value_cases_test.go b/bson/marshal_value_cases_test.go index 29356ece67..289aa3543f 100644 --- a/bson/marshal_value_cases_test.go +++ b/bson/marshal_value_cases_test.go @@ -37,13 +37,13 @@ type marshalValueMarshaler struct { var _ ValueMarshaler = marshalValueMarshaler{} -func (mvi marshalValueMarshaler) MarshalBSONValue() (Type, []byte, error) { - return TypeInt32, bsoncore.AppendInt32(nil, int32(mvi.Foo)), nil +func (mvi marshalValueMarshaler) MarshalBSONValue() (byte, []byte, error) { + return byte(TypeInt32), bsoncore.AppendInt32(nil, int32(mvi.Foo)), nil } var _ ValueUnmarshaler = &marshalValueMarshaler{} -func (mvi *marshalValueMarshaler) UnmarshalBSONValue(_ Type, b []byte) error { +func (mvi *marshalValueMarshaler) UnmarshalBSONValue(_ byte, b []byte) error { v, _, _ := bsoncore.ReadInt32(b) mvi.Foo = int(v) return nil diff --git a/bson/primitive_codecs_test.go b/bson/primitive_codecs_test.go index 6b0c4c1e05..e9a7403551 100644 --- a/bson/primitive_codecs_test.go +++ b/bson/primitive_codecs_test.go @@ -1062,8 +1062,8 @@ type testValueMarshaler struct { err error } -func (tvm testValueMarshaler) MarshalBSONValue() (Type, []byte, error) { - return tvm.t, tvm.buf, tvm.err +func (tvm testValueMarshaler) MarshalBSONValue() (byte, []byte, error) { + return byte(tvm.t), tvm.buf, tvm.err } type testValueUnmarshaler struct { @@ -1072,8 +1072,8 @@ type testValueUnmarshaler struct { err error } -func (tvu *testValueUnmarshaler) UnmarshalBSONValue(t Type, val []byte) error { - tvu.t, tvu.val = t, val +func (tvu *testValueUnmarshaler) UnmarshalBSONValue(t byte, val []byte) error { + tvu.t, tvu.val = Type(t), val return tvu.err } func (tvu testValueUnmarshaler) Equal(tvu2 testValueUnmarshaler) bool { diff --git a/bson/unmarshal.go b/bson/unmarshal.go index 7caadc5dbc..49ed348ea4 100644 --- a/bson/unmarshal.go +++ b/bson/unmarshal.go @@ -31,7 +31,7 @@ type Unmarshaler interface { // document. To create custom BSON unmarshaling behavior for an entire BSON // document, implement the Unmarshaler interface instead. type ValueUnmarshaler interface { - UnmarshalBSONValue(Type, []byte) error + UnmarshalBSONValue(typ byte, data []byte) error } // Unmarshal parses the BSON-encoded data and stores the result in the value diff --git a/mongo/mongo.go b/mongo/mongo.go index 318c765000..7ba0dff24e 100644 --- a/mongo/mongo.go +++ b/mongo/mongo.go @@ -229,8 +229,8 @@ func marshalAggregatePipeline( if err != nil { return nil, false, err } - if btype != bson.TypeArray { - return nil, false, fmt.Errorf("ValueMarshaler returned a %v, but was expecting %v", btype, bson.TypeArray) + if typ := bson.Type(btype); typ != bson.TypeArray { + return nil, false, fmt.Errorf("ValueMarshaler returned a %v, but was expecting %v", typ, bson.TypeArray) } var hasOutputStage bool diff --git a/mongo/mongo_test.go b/mongo/mongo_test.go index 3c92fdb620..736594a6e6 100644 --- a/mongo/mongo_test.go +++ b/mongo/mongo_test.go @@ -616,6 +616,6 @@ type bvMarsh struct { err error } -func (b bvMarsh) MarshalBSONValue() (bson.Type, []byte, error) { - return b.t, b.data, b.err +func (b bvMarsh) MarshalBSONValue() (byte, []byte, error) { + return byte(b.t), b.data, b.err }