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-2612 Remove RegistryBuilder #1659

Merged
merged 4 commits into from
Jun 27, 2024

Conversation

qingyang-hu
Copy link
Collaborator

@qingyang-hu qingyang-hu commented Jun 5, 2024

GODRIVER-2612

Summary

  • Deprecate RegistryBuilder.
  • Un-expose codecs.

@mongodb-drivers-pr-bot mongodb-drivers-pr-bot bot added the priority-3-low Low Priority PR for Review label Jun 5, 2024
Copy link
Contributor

mongodb-drivers-pr-bot bot commented Jun 5, 2024

API Change Report

./bson

incompatible changes

ArrayCodec: removed
ByteSliceCodec: removed
DefaultStructTagParser: removed
DefaultValueDecoders: removed
DefaultValueEncoders: removed
EmptyInterfaceCodec: removed
ErrNotInterface: removed
JSONFallbackStructTagParser: removed
MapCodec: removed
NewArrayCodec: removed
NewByteSliceCodec: removed
NewEmptyInterfaceCodec: removed
NewMapCodec: removed
NewPointerCodec: removed
NewRegistryBuilder: removed
NewSliceCodec: removed
NewStringCodec: removed
NewStructCodec: removed
NewTimeCodec: removed
NewUIntCodec: removed
PointerCodec: removed
PrimitiveCodecs: removed
RegistryBuilder: removed
SliceCodec: removed
StringCodec: removed
StructCodec: removed
StructTagParser: removed
StructTagParserFunc: removed
StructTags: removed
TimeCodec: removed
UIntCodec: removed

compatible changes

ErrMgoSetZero: added
NewMgoRegistry: added
NewRespectNilValuesMgoRegistry: added

./bson/bsonoptions

incompatible changes

package removed

./bson/mgocompat

incompatible changes

ErrSetZero: removed
Getter: removed
GetterEncodeValue: removed
NewRegistryBuilder: removed
NewRespectNilValuesRegistryBuilder: removed
Setter: removed
SetterDecodeValue: removed

@qingyang-hu qingyang-hu force-pushed the godriver2976p3 branch 2 times, most recently from f1fce18 to 639d4a8 Compare June 5, 2024 22:14
@qingyang-hu qingyang-hu changed the title GODERIVER-2612 Deprecate RegistryBuilder. GODERIVER-2612 Remove RegistryBuilder. Jun 6, 2024
@blink1073 blink1073 changed the title GODERIVER-2612 Remove RegistryBuilder. GODRIVER-2612 Remove RegistryBuilder. Jun 7, 2024
@blink1073 blink1073 changed the title GODRIVER-2612 Remove RegistryBuilder. GODRIVER-2612 Remove RegistryBuilder Jun 7, 2024
@qingyang-hu qingyang-hu marked this pull request as ready for review June 11, 2024 20:19
// used by collection type decoders (e.g. map, slice, etc) to set individual values in a
// collection.
_ typeDecoder = defaultByteSliceCodec
_ typeDecoder = (*byteSliceCodec)(nil)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The traditional way we've done this in the driver is by instantiating a struct, e.g. _ typeDecoder = &byteSliceCodec{}. Is there a reason to do it the way you propose? Additionally, we should put the variable inline since we just make one assertion:

var _ typeDecoder = (*byteSliceCodec)(nil)


"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)

var (
defaultValueDecoders DefaultValueDecoders
errCannotTruncate = errors.New("float64 can only be truncated to a lower precision type when truncation is enabled")
errCannotTruncate = errors.New("float64 can only be truncated to a lower precision type when truncation is enabled")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggest putting the variable inline since there is just one:

ver errCannotTruncate = errors.New("float64 can only be truncated to a lower precision type when truncation is enabled")

}

func (DefaultValueDecoders) javaScriptDecodeType(_ DecodeContext, vr ValueReader, t reflect.Type) (reflect.Value, error) {
func javaScriptDecodeType(_ DecodeContext, vr ValueReader, t reflect.Type) (reflect.Value, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggest removing the empty DecodeContext parameter.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

javaScriptDecodeType is used with decodeAdapter to satisfy the ValueDecoder interface. Although most typeDecoderFuncs in decodeAdapter don't use the DecodeContext parameter, the codeWithScopeDecodeType still requires a DecodeContext. Also for the extensibility, it still makes sense to keep the parameter.

// value decoders registered.
func (dvd DefaultValueDecoders) JavaScriptDecodeValue(dctx DecodeContext, vr ValueReader, val reflect.Value) error {
// javaScriptDecodeValue is the ValueDecoderFunc for the JavaScript type.
func javaScriptDecodeValue(dctx DecodeContext, vr ValueReader, val reflect.Value) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The dctx parameter is unused, suggest removing it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The same appears to be true for symbolDecodeValue, binaryDecodeValue, undefinedDecodeValue, objectIDDecodeValue, dateTimeDecodeValue, nullDecodeValue, regexDecodeValue, dbPointerDecodeValue, timestampDecodeValue, coreDocumentDecodeValue, unmarshalerDecodeValue, valueUnmarshalerDecodeValue, urlDecodeValue, jsonNumberDecodeValue, decimal128DecodeValue, maxKeyDecodeValue, and minKeyDecodeValuem,

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it's reasonable to keep the DecodeContext parameter considering the extensibility of the typeDecoderFunc in decodeAdapter, e.g. codeWithScopeDecodeType.

// value encoders registered.
func (dve DefaultValueEncoders) FloatEncodeValue(_ EncodeContext, vw ValueWriter, val reflect.Value) error {
// floatEncodeValue is the ValueEncoderFunc for float types.
func floatEncodeValue(_ EncodeContext, vw ValueWriter, val reflect.Value) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The same argument for the decode *Value functions applies here. There doesn't appear to be a need to include the empty EncodeContext parameter for most of these functions.

// to be used by collection type decoders (e.g. map, slice, etc) to set individual values in a
// collection.
_ typeDecoder = defaultEmptyInterfaceCodec
_ typeDecoder = (*emptyInterfaceCodec)(nil)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggest inlining the variable and using the conventional type assertion pattern: var _ interface = &struct{}

Comment on lines 83 to 84
_ ValueEncoder = (*structCodec)(nil)
_ ValueDecoder = (*structCodec)(nil)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider using the traditional convention of _ ValueEncoder = &structCodec{}

@qingyang-hu qingyang-hu added priority-2-medium Medium Priority PR for Review and removed priority-3-low Low Priority PR for Review labels Jun 18, 2024

var (
// ErrSetZero may be returned from a SetBSON method to have the value set to its respective zero value.
ErrSetZero = errors.New("set to zero")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: Consider modifying this name to indicate it's only used with the mgo-compatibility registries. E.g. ErrMgoSetZero

// itself will not be changed as usual.
//
// If setting the value works, the method should return nil or alternatively
// mgocompat.ErrSetZero to set the respective field to its zero value (nil for
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: Update comment to remove mgocompat package name.

// structCodec is the Codec used for struct values.
type structCodec struct {
cache sync.Map // map[reflect.Type]*structDescription
elemEncoder mapElementsEncoder
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: Consider using a field name that better describes the use case. E.g. inlineMapEncoder.

@@ -216,14 +181,17 @@ func (sc *StructCodec) EncodeValue(ec EncodeContext, vw ValueWriter, val reflect
}
}

if sd.inlineMap >= 0 {
if sd.inlineMap >= 0 && sc.elemEncoder != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This check will result in omitting the map elements if there is no elemEncoder, which could result in a subtle bug that drops data. The only code that creates a structCodec is the bson package, so that's not a case we need to handle gracefully. A panic would be a much more obvious indicator of such a bug. We should remove this nil check.

Suggested change
if sd.inlineMap >= 0 && sc.elemEncoder != nil {
if sd.inlineMap >= 0 {

Additionally, we could panic in newStructCodec if elemEncoder is nil, although not all code paths that use a structCodec call newStructCodec.

Copy link
Collaborator

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

@qingyang-hu qingyang-hu merged commit fedd2a6 into mongodb:master Jun 27, 2024
26 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-2-medium Medium Priority PR for Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants