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 1 commit
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
4 changes: 2 additions & 2 deletions bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func TestTimestamp(t *testing.T) {
func TestPrimitiveIsZero(t *testing.T) {
testcases := []struct {
name string
zero Zeroer
nonzero Zeroer
zero zeroer
nonzero zeroer
}{
{"binary", Binary{}, Binary{Data: []byte{0x01, 0x02, 0x03}, Subtype: 0xFF}},
{"decimal128", Decimal128{}, NewDecimal128(1, 2)},
Expand Down
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
2 changes: 1 addition & 1 deletion bson/default_value_encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ func (dve DefaultValueEncoders) ProxyEncodeValue(ec EncodeContext, vw ValueWrite
return ValueEncoderError{Name: "ProxyEncodeValue", Types: []reflect.Type{tProxy}, Received: val}
}

m, ok := val.Interface().(Proxy)
m, ok := val.Interface().(proxy)
if !ok {
return vw.WriteNull()
}
Expand Down
6 changes: 3 additions & 3 deletions bson/default_value_encoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ func TestDefaultValueEncoders(t *testing.T) {
AC Decimal128
AD *time.Time
AE testValueMarshaler
AF Proxy
AF proxy
AG testProxy
AH map[string]interface{}
AI CodeWithScope
Expand Down Expand Up @@ -1650,7 +1650,7 @@ func TestDefaultValueEncoders(t *testing.T) {
AC []Decimal128
AD []*time.Time
AE []testValueMarshaler
AF []Proxy
AF []proxy
AG []testProxy
}{
A: []bool{true},
Expand Down Expand Up @@ -1685,7 +1685,7 @@ func TestDefaultValueEncoders(t *testing.T) {
{t: TypeString, buf: bsoncore.AppendString(nil, "hello")},
{t: TypeString, buf: bsoncore.AppendString(nil, "world")},
},
AF: []Proxy{
AF: []proxy{
testProxy{ret: struct{ Hello string }{Hello: "world!"}},
testProxy{ret: struct{ Foo string }{Foo: "bar"}},
},
Expand Down
6 changes: 3 additions & 3 deletions bson/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
"time"
)

// Zeroer allows custom struct types to implement a report of zero
// state. All struct types that don't implement Zeroer or where IsZero
// zeroer allows custom struct types to implement a report of zero
// state. All struct types that don't implement zeroer or where IsZero
// returns false are considered to be not zero.
type Zeroer interface {
type zeroer interface {
IsZero() bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the recent relevance of IsZero, it's possible we may need to keep the Zeroer interface exported.

}

Expand Down
4 changes: 2 additions & 2 deletions bson/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

package bson

// Proxy is an interface implemented by types that cannot themselves be directly encoded. Types
// proxy is an interface implemented by types that cannot themselves be directly encoded. Types
// that implement this interface with have ProxyBSON called during the encoding process and that
// value will be encoded in place for the implementer.
type Proxy interface {
type proxy interface {
ProxyBSON() (interface{}, error)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should consider completely removing the ProxyBSON feature. Based on searching for uses in open-source repos, it seems completely unused.

}
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)
}
6 changes: 4 additions & 2 deletions bson/struct_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ func (sc *StructCodec) EncodeValue(ec EncodeContext, vw ValueWriter, val reflect
encoder := desc.encoder

var empty bool
if cz, ok := encoder.(CodecZeroer); ok {
if cz, ok := encoder.(interface {
IsTypeZero(interface{}) bool
}); ok {
empty = cz.IsTypeZero(rv.Interface())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should consider completely removing the IsTypeZero feature. Based on searching for uses in open-source repos, it seems completely unused.

} else if rv.Kind() == reflect.Interface {
// isEmpty will not treat an interface rv as an interface, so we need to check for the
Expand Down Expand Up @@ -398,7 +400,7 @@ func (sc *StructCodec) DecodeValue(dc DecodeContext, vr ValueReader, val reflect
func isEmpty(v reflect.Value, omitZeroStruct bool) bool {
kind := v.Kind()
if (kind != reflect.Ptr || !v.IsNil()) && v.Type().Implements(tZeroer) {
return v.Interface().(Zeroer).IsZero()
return v.Interface().(zeroer).IsZero()
}
switch kind {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
Expand Down
14 changes: 7 additions & 7 deletions bson/struct_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"go.mongodb.org/mongo-driver/internal/assert"
)

var _ Zeroer = zeroer{}
var _ zeroer = testZeroer{}

type zeroer struct {
type testZeroer struct {
val int
}

func (z zeroer) IsZero() bool {
func (z testZeroer) IsZero() bool {
return z.val != 0
}

Expand Down Expand Up @@ -84,22 +84,22 @@ func TestIsZero(t *testing.T) {
},
{
description: "zero struct that implements Zeroer",
value: zeroer{},
value: testZeroer{},
want: false,
},
{
description: "non-zero struct that implements Zeroer",
value: &zeroer{val: 1},
value: &testZeroer{val: 1},
want: true,
},
{
description: "pointer to zero struct that implements Zeroer",
value: &zeroer{},
value: &testZeroer{},
want: false,
},
{
description: "pointer to non-zero struct that implements Zeroer",
value: zeroer{val: 1},
value: testZeroer{val: 1},
want: true,
},
{
Expand Down
4 changes: 2 additions & 2 deletions bson/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ var tValueMarshaler = reflect.TypeOf((*ValueMarshaler)(nil)).Elem()
var tValueUnmarshaler = reflect.TypeOf((*ValueUnmarshaler)(nil)).Elem()
var tMarshaler = reflect.TypeOf((*Marshaler)(nil)).Elem()
var tUnmarshaler = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
var tProxy = reflect.TypeOf((*Proxy)(nil)).Elem()
var tZeroer = reflect.TypeOf((*Zeroer)(nil)).Elem()
var tProxy = reflect.TypeOf((*proxy)(nil)).Elem()
var tZeroer = reflect.TypeOf((*zeroer)(nil)).Elem()

var tBinary = reflect.TypeOf(Binary{})
var tUndefined = reflect.TypeOf(Undefined{})
Expand Down
2 changes: 1 addition & 1 deletion bson/value_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (vr *valueReader) nextElementLength() (int32, error) {
return length, err
}

func (vr *valueReader) ReadValueBytes(dst []byte) (Type, []byte, error) {
func (vr *valueReader) readValueBytes(dst []byte) (Type, []byte, error) {
switch vr.stack[vr.frame].mode {
case mTopLevel:
length, err := vr.peekLength()
Expand Down
6 changes: 3 additions & 3 deletions bson/value_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ func TestValueReader(t *testing.T) {
offset: tc.startingOffset,
}

_, got, err := vr.ReadValueBytes(nil)
_, got, err := vr.readValueBytes(nil)
if !errequal(t, err, tc.err) {
t.Errorf("Did not receive expected error; got %v; want %v", err, tc.err)
}
Expand Down Expand Up @@ -1481,7 +1481,7 @@ func TestValueReader(t *testing.T) {
},
frame: 0,
}
gotType, got, gotErr := vr.ReadValueBytes(nil)
gotType, got, gotErr := vr.readValueBytes(nil)
if !errors.Is(gotErr, tc.wantErr) {
t.Errorf("Did not receive expected error. got %v; want %v", gotErr, tc.wantErr)
}
Expand Down Expand Up @@ -1510,7 +1510,7 @@ func TestValueReader(t *testing.T) {
vr := &valueReader{stack: []vrState{{mode: mTopLevel}, {mode: mDocument}}, frame: 1}
wanterr := (&valueReader{stack: []vrState{{mode: mTopLevel}, {mode: mDocument}}, frame: 1}).
invalidTransitionErr(0, "ReadValueBytes", []mode{mElement, mValue})
_, _, goterr := vr.ReadValueBytes(nil)
_, _, goterr := vr.readValueBytes(nil)
if !cmp.Equal(goterr, wanterr, cmp.Comparer(assert.CompareErrors)) {
t.Errorf("Expected correct invalid transition error. got %v; want %v", goterr, wanterr)
}
Expand Down
2 changes: 1 addition & 1 deletion bson/value_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (vw *valueWriter) writeElementHeader(t Type, destination mode, callerName s
return nil
}

func (vw *valueWriter) WriteValueBytes(t Type, b []byte) error {
func (vw *valueWriter) writeValueBytes(t Type, b []byte) error {
if err := vw.writeElementHeader(t, mode(0), "WriteValueBytes"); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions bson/value_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func TestValueWriter(t *testing.T) {
vw := newValueWriterFromSlice(nil)
want := TransitionError{current: mTopLevel, destination: mode(0),
name: "WriteValueBytes", modes: []mode{mElement, mValue}, action: "write"}
got := vw.WriteValueBytes(TypeEmbeddedDocument, nil)
got := vw.writeValueBytes(TypeEmbeddedDocument, nil)
if !assert.CompareErrors(got, want) {
t.Errorf("Did not received expected error. got %v; want %v", got, want)
}
Expand All @@ -338,7 +338,7 @@ func TestValueWriter(t *testing.T) {
noerr(t, err)
_, err = vw.WriteDocumentElement("foo")
noerr(t, err)
err = vw.WriteValueBytes(TypeEmbeddedDocument, doc)
err = vw.writeValueBytes(TypeEmbeddedDocument, doc)
noerr(t, err)
err = vw.WriteDocumentEnd()
noerr(t, err)
Expand Down
8 changes: 3 additions & 5 deletions bson/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ type ValueWriterFlusher interface {
Flush() error
}

// BytesWriter is the interface used to write BSON bytes to a ValueWriter.
// bytesWriter is the interface used to write BSON bytes to a ValueWriter.
// This interface is meant to be a superset of ValueWriter, so that types that
// implement ValueWriter may also implement this interface.
//
// Deprecated: BytesWriter will not be supported in Go Driver 2.0.
type BytesWriter interface {
WriteValueBytes(t Type, b []byte) error
type bytesWriter interface {
writeValueBytes(t Type, b []byte) error
}

// SliceWriter allows a pointer to a slice of bytes to be used as an io.Writer.
Expand Down
Loading