-
Notifications
You must be signed in to change notification settings - Fork 897
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-2896 Add IsZero to BSON RawValue #1332
Changes from 2 commits
17bb347
a3472c9
3970d95
3bfa20d
4938834
63d9d8d
11923dd
e0114b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,15 +45,29 @@ func (pc PrimitiveCodecs) RegisterPrimitiveCodecs(rb *bsoncodec.RegistryBuilder) | |
|
||
// RawValueEncodeValue is the ValueEncoderFunc for RawValue. | ||
// | ||
// Deprecated: Use bson.NewRegistry to get a registry with all primitive encoders and decoders | ||
// registered. | ||
// If the RawValue's Type is "invalid" and the RawValue's Value is not empty or | ||
// nil, then this method will return an error. | ||
// | ||
// Deprecated: Use bson.NewRegistry to get a registry with all primitive | ||
// encoders and decoders registered. | ||
func (PrimitiveCodecs) RawValueEncodeValue(_ bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { | ||
if !val.IsValid() || val.Type() != tRawValue { | ||
return bsoncodec.ValueEncoderError{Name: "RawValueEncodeValue", Types: []reflect.Type{tRawValue}, Received: val} | ||
return bsoncodec.ValueEncoderError{ | ||
Name: "RawValueEncodeValue", | ||
Types: []reflect.Type{tRawValue}, | ||
Received: val, | ||
} | ||
} | ||
|
||
rawvalue := val.Interface().(RawValue) | ||
|
||
if rawvalue.Type.String() == "invalid" { | ||
return bsoncodec.ValueEncoderError{ | ||
Name: "RawValueEncodeValue", | ||
Types: []reflect.Type{tRawValue}, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error value will result in an error message like: "RawValueEncodeValue can only encode valid reflect.Kind, but got " which doesn't really describe the reason for the error. Instead, we should return an error that specifically describes the error condition. Something like: fmt.Errorf("the RawValue Type specifies an invalid BSON type: %#x", rawvalue.Type)
// E.g. error: "the RawValue Type specifies an invalid BSON type: 0x1e" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error should result in a message like this:
Do you have an example that excludes in the invalid part? I would expect any invalidly typed RawValue to return "invalid" because of the bsontype.Type String method. See here for the error logic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're correct, I misunderstood the logic in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
} | ||
|
||
return bsonrw.Copier{}.CopyValueFromBytes(vw, rawvalue.Type, rawvalue.Value) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a magic string that depends on the logic in the bsontype.Type's String() method, here. Do we want to create a function to contain this logic in the bsontype package? Something like "Type.Valid()"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems reasonable to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a "check validity" method sounds like a good idea. I recommend naming it
IsValid
to stick with the convention provided byreflect
and (some) other methods in the Go driver.