Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GODRIVER-2722 Remove/unexport unnecessary interfaces. #1669

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions bson/bsoncodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,3 @@ func decodeTypeOrValueWithInfo(vd ValueDecoder, dc DecodeContext, vr ValueReader
err := vd.DecodeValue(dc, vr, val)
return val, err
}

// CodecZeroer is the interface implemented by Codecs that can also determine if
// a value of the type that would be encoded is zero.
//
// Deprecated: Defining custom rules for the zero/empty value will not be supported in Go Driver
// 2.0. Users who want to omit empty complex values should use a pointer field and set the value to
// nil instead.
type CodecZeroer interface {
IsTypeZero(interface{}) bool
}
16 changes: 8 additions & 8 deletions bson/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ func copyDocumentToBytes(src ValueReader) ([]byte, error) {
// Deprecated: Copying BSON documents using the ValueWriter and ValueReader interfaces will not be
// supported in Go Driver 2.0.
func appendDocumentBytes(dst []byte, src ValueReader) ([]byte, error) {
if br, ok := src.(BytesReader); ok {
_, dst, err := br.ReadValueBytes(dst)
if br, ok := src.(bytesReader); ok {
_, dst, err := br.readValueBytes(dst)
return dst, err
}

Expand All @@ -182,8 +182,8 @@ func appendDocumentBytes(dst []byte, src ValueReader) ([]byte, error) {
// Deprecated: Copying BSON arrays using the ValueWriter and ValueReader interfaces will not be
// supported in Go Driver 2.0.
func appendArrayBytes(dst []byte, src ValueReader) ([]byte, error) {
if br, ok := src.(BytesReader); ok {
_, dst, err := br.ReadValueBytes(dst)
if br, ok := src.(bytesReader); ok {
_, dst, err := br.readValueBytes(dst)
return dst, err
}

Expand All @@ -201,8 +201,8 @@ func appendArrayBytes(dst []byte, src ValueReader) ([]byte, error) {
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.UnmarshalValue] instead.
func copyValueFromBytes(dst ValueWriter, t Type, src []byte) error {
if wvb, ok := dst.(BytesWriter); ok {
return wvb.WriteValueBytes(t, src)
if wvb, ok := dst.(bytesWriter); ok {
return wvb.writeValueBytes(t, src)
}

vr := vrPool.Get().(*valueReader)
Expand All @@ -228,8 +228,8 @@ func CopyValueToBytes(src ValueReader) (Type, []byte, error) {
// Deprecated: Appending individual BSON elements to an existing slice will not be supported in Go
// Driver 2.0.
func appendValueBytes(dst []byte, src ValueReader) (Type, []byte, error) {
if br, ok := src.(BytesReader); ok {
return br.ReadValueBytes(dst)
if br, ok := src.(bytesReader); ok {
return br.readValueBytes(dst)
}

vw := vwPool.Get().(*valueWriter)
Expand Down
45 changes: 0 additions & 45 deletions bson/default_value_encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func registerDefaultEncoders(reg *Registry) {
reg.RegisterKindEncoder(reflect.Ptr, &pointerCodec{})
reg.RegisterInterfaceEncoder(tValueMarshaler, ValueEncoderFunc(valueMarshalerEncodeValue))
reg.RegisterInterfaceEncoder(tMarshaler, ValueEncoderFunc(marshalerEncodeValue))
reg.RegisterInterfaceEncoder(tProxy, ValueEncoderFunc(proxyEncodeValue))
}

// booleanEncodeValue is the ValueEncoderFunc for bool types.
Expand Down Expand Up @@ -333,50 +332,6 @@ func marshalerEncodeValue(_ EncodeContext, vw ValueWriter, val reflect.Value) er
return copyValueFromBytes(vw, TypeEmbeddedDocument, data)
}

// proxyEncodeValue is the ValueEncoderFunc for Proxy implementations.
func proxyEncodeValue(ec EncodeContext, vw ValueWriter, val reflect.Value) error {
// Either val or a pointer to val must implement Proxy
switch {
case !val.IsValid():
return ValueEncoderError{Name: "ProxyEncodeValue", Types: []reflect.Type{tProxy}, Received: val}
case val.Type().Implements(tProxy):
// If Proxy is implemented on a concrete type, make sure that val isn't a nil pointer
if isImplementationNil(val, tProxy) {
return vw.WriteNull()
}
case reflect.PtrTo(val.Type()).Implements(tProxy) && val.CanAddr():
val = val.Addr()
default:
return ValueEncoderError{Name: "ProxyEncodeValue", Types: []reflect.Type{tProxy}, Received: val}
}

m, ok := val.Interface().(Proxy)
if !ok {
return vw.WriteNull()
}
v, err := m.ProxyBSON()
if err != nil {
return err
}
if v == nil {
encoder, err := ec.LookupEncoder(nil)
if err != nil {
return err
}
return encoder.EncodeValue(ec, vw, reflect.ValueOf(nil))
}
vv := reflect.ValueOf(v)
switch vv.Kind() {
case reflect.Ptr, reflect.Interface:
vv = vv.Elem()
}
encoder, err := ec.LookupEncoder(vv.Type())
if err != nil {
return err
}
return encoder.EncodeValue(ec, vw, vv)
}

// javaScriptEncodeValue is the ValueEncoderFunc for the JavaScript type.
func javaScriptEncodeValue(_ EncodeContext, vw ValueWriter, val reflect.Value) error {
if !val.IsValid() || val.Type() != tJavaScript {
Expand Down
138 changes: 6 additions & 132 deletions bson/default_value_encoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@ func TestDefaultValueEncoders(t *testing.T) {
d128 := NewDecimal128(12345, 67890)
var nilValueMarshaler *testValueMarshaler
var nilMarshaler *testMarshaler
var nilProxy *testProxy

vmStruct := struct{ V testValueMarshalPtr }{testValueMarshalPtr{t: TypeString, buf: []byte{0x04, 0x00, 0x00, 0x00, 'f', 'o', 'o', 0x00}}}
mStruct := struct{ V testMarshalPtr }{testMarshalPtr{buf: bsoncore.BuildDocument(nil, bsoncore.AppendDoubleElement(nil, "pi", 3.14159))}}
pStruct := struct{ V testProxyPtr }{testProxyPtr{ret: int64(1234567890)}}

type subtest struct {
name string
Expand Down Expand Up @@ -703,76 +701,6 @@ func TestDefaultValueEncoders(t *testing.T) {
},
},
},
{
"ProxyEncodeValue",
ValueEncoderFunc(proxyEncodeValue),
[]subtest{
{
"wrong type",
wrong,
nil,
nil,
nothing,
ValueEncoderError{Name: "ProxyEncodeValue", Types: []reflect.Type{tProxy}, Received: reflect.ValueOf(wrong)},
},
{
"Proxy error",
testProxy{err: errors.New("proxy error")},
nil,
nil,
nothing,
errors.New("proxy error"),
},
{
"Lookup error",
testProxy{ret: nil},
&EncodeContext{Registry: buildDefaultRegistry()},
nil,
nothing,
ErrNoEncoder{Type: nil},
},
{
"success struct implementation",
testProxy{ret: int64(1234567890)},
&EncodeContext{Registry: buildDefaultRegistry()},
nil,
writeInt64,
nil,
},
{
"success ptr to struct implementation",
&testProxy{ret: int64(1234567890)},
&EncodeContext{Registry: buildDefaultRegistry()},
nil,
writeInt64,
nil,
},
{
"success nil ptr to struct implementation",
nilProxy,
nil,
nil,
writeNull,
nil,
},
{
"success ptr to ptr implementation",
&testProxyPtr{ret: int64(1234567890)},
&EncodeContext{Registry: buildDefaultRegistry()},
nil,
writeInt64,
nil,
},
{
"unaddressable ptr implementation",
testProxyPtr{ret: int64(1234567890)},
nil,
nil,
nothing,
ValueEncoderError{Name: "ProxyEncodeValue", Types: []reflect.Type{tProxy}, Received: reflect.ValueOf(testProxyPtr{})},
},
},
},
{
"PointerCodec.EncodeValue",
&pointerCodec{},
Expand Down Expand Up @@ -831,14 +759,6 @@ func TestDefaultValueEncoders(t *testing.T) {
writeDocumentEnd,
nil,
},
{
"Proxy",
&pStruct,
&EncodeContext{Registry: buildDefaultRegistry()},
nil,
writeDocumentEnd,
nil,
},
},
},
{
Expand Down Expand Up @@ -1554,10 +1474,8 @@ func TestDefaultValueEncoders(t *testing.T) {
AC Decimal128
AD *time.Time
AE testValueMarshaler
AF Proxy
AG testProxy
AH map[string]interface{}
AI CodeWithScope
AF map[string]interface{}
AG CodeWithScope
}{
A: true,
B: 123,
Expand All @@ -1583,10 +1501,8 @@ func TestDefaultValueEncoders(t *testing.T) {
AC: decimal128,
AD: &now,
AE: testValueMarshaler{t: TypeString, buf: bsoncore.AppendString(nil, "hello, world")},
AF: testProxy{ret: struct{ Hello string }{Hello: "world!"}},
AG: testProxy{ret: struct{ Pi float64 }{Pi: 3.14159}},
AH: nil,
AI: CodeWithScope{Code: "var hello = 'world';", Scope: D{{"pi", 3.14159}}},
AF: nil,
AG: CodeWithScope{Code: "var hello = 'world';", Scope: D{{"pi", 3.14159}}},
},
buildDocument(func(doc []byte) []byte {
doc = bsoncore.AppendBooleanElement(doc, "a", true)
Expand All @@ -1611,10 +1527,8 @@ func TestDefaultValueEncoders(t *testing.T) {
doc = bsoncore.AppendDecimal128Element(doc, "ac", decimal128.h, decimal128.l)
doc = bsoncore.AppendDateTimeElement(doc, "ad", now.UnixNano()/int64(time.Millisecond))
doc = bsoncore.AppendStringElement(doc, "ae", "hello, world")
doc = bsoncore.AppendDocumentElement(doc, "af", buildDocument(bsoncore.AppendStringElement(nil, "hello", "world!")))
doc = bsoncore.AppendDocumentElement(doc, "ag", buildDocument(bsoncore.AppendDoubleElement(nil, "pi", 3.14159)))
doc = bsoncore.AppendNullElement(doc, "ah")
doc = bsoncore.AppendCodeWithScopeElement(doc, "ai",
doc = bsoncore.AppendNullElement(doc, "af")
doc = bsoncore.AppendCodeWithScopeElement(doc, "ag",
"var hello = 'world';", buildDocument(bsoncore.AppendDoubleElement(nil, "pi", 3.14159)),
)
return doc
Expand Down Expand Up @@ -1649,8 +1563,6 @@ func TestDefaultValueEncoders(t *testing.T) {
AC []Decimal128
AD []*time.Time
AE []testValueMarshaler
AF []Proxy
AG []testProxy
}{
A: []bool{true},
B: []int32{123},
Expand Down Expand Up @@ -1684,14 +1596,6 @@ func TestDefaultValueEncoders(t *testing.T) {
{t: TypeString, buf: bsoncore.AppendString(nil, "hello")},
{t: TypeString, buf: bsoncore.AppendString(nil, "world")},
},
AF: []Proxy{
testProxy{ret: struct{ Hello string }{Hello: "world!"}},
testProxy{ret: struct{ Foo string }{Foo: "bar"}},
},
AG: []testProxy{
{ret: struct{ One int64 }{One: 1234567890}},
{ret: struct{ Pi float64 }{Pi: 3.14159}},
},
},
buildDocument(func(doc []byte) []byte {
doc = appendArrayElement(doc, "a", bsoncore.AppendBooleanElement(nil, "0", true))
Expand Down Expand Up @@ -1741,22 +1645,6 @@ func TestDefaultValueEncoders(t *testing.T) {
doc = appendArrayElement(doc, "ae",
bsoncore.AppendStringElement(bsoncore.AppendStringElement(nil, "0", "hello"), "1", "world"),
)
doc = appendArrayElement(doc, "af",
bsoncore.AppendDocumentElement(
bsoncore.AppendDocumentElement(nil, "0",
bsoncore.BuildDocument(nil, bsoncore.AppendStringElement(nil, "hello", "world!")),
), "1",
bsoncore.BuildDocument(nil, bsoncore.AppendStringElement(nil, "foo", "bar")),
),
)
doc = appendArrayElement(doc, "ag",
bsoncore.AppendDocumentElement(
bsoncore.AppendDocumentElement(nil, "0",
bsoncore.BuildDocument(nil, bsoncore.AppendInt64Element(nil, "one", 1234567890)),
), "1",
bsoncore.BuildDocument(nil, bsoncore.AppendDoubleElement(nil, "pi", 3.14159)),
),
)
return doc
}(nil)),
nil,
Expand Down Expand Up @@ -1868,17 +1756,3 @@ type testMarshalPtr struct {
func (tvm *testMarshalPtr) MarshalBSON() ([]byte, error) {
return tvm.buf, tvm.err
}

type testProxy struct {
ret interface{}
err error
}

func (tp testProxy) ProxyBSON() (interface{}, error) { return tp.ret, tp.err }

type testProxyPtr struct {
ret interface{}
err error
}

func (tp *testProxyPtr) ProxyBSON() (interface{}, error) { return tp.ret, tp.err }
14 changes: 0 additions & 14 deletions bson/proxy.go

This file was deleted.

10 changes: 4 additions & 6 deletions bson/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ type ValueReader interface {
ReadUndefined() error
}

// BytesReader is a generic interface used to read BSON bytes from a
// ValueReader. This imterface is meant to be a superset of ValueReader, so that
// bytesReader is a generic interface used to read BSON bytes from a
// ValueReader. This interface is meant to be a superset of ValueReader, so that
// types that implement ValueReader may also implement this interface.
//
// The bytes of the value will be appended to dst.
//
// Deprecated: BytesReader will not be supported in Go Driver 2.0.
type BytesReader interface {
ReadValueBytes(dst []byte) (Type, []byte, error)
type bytesReader interface {
readValueBytes(dst []byte) (Type, []byte, error)
}
4 changes: 1 addition & 3 deletions bson/struct_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ func (sc *structCodec) EncodeValue(ec EncodeContext, vw ValueWriter, val reflect
encoder := desc.encoder

var empty bool
if cz, ok := encoder.(CodecZeroer); ok {
empty = cz.IsTypeZero(rv.Interface())
} else if rv.Kind() == reflect.Interface {
if rv.Kind() == reflect.Interface {
// isEmpty will not treat an interface rv as an interface, so we need to check for the
// nil interface separately.
empty = rv.IsNil()
Expand Down
Loading
Loading