Skip to content

Commit

Permalink
GODRIVER-2866 Update method signatures of bson.ValueMarshaler/`bson…
Browse files Browse the repository at this point in the history
….ValueUnmarshaler` to only use basic types. (#1671)
  • Loading branch information
qingyang-hu authored Jun 27, 2024
1 parent fedd2a6 commit cef198c
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bson/default_value_decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion bson/default_value_encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions bson/default_value_encoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion bson/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions bson/marshal_value_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions bson/primitive_codecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion bson/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mongo/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit cef198c

Please sign in to comment.