-
Notifications
You must be signed in to change notification settings - Fork 43
DOCSP-18711: Explain BsonRepresentation limitation #549
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
DOCSP-18711: Explain BsonRepresentation limitation #549
Conversation
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.
LGTM w/ two small things!
Codec must implement RepresentationConfigurable to support BsonRepresentation | ||
|
||
For example, the following code adds a ``purchaseDate`` field of type ``Long`` to the | ||
``Product`` POJO. This example attempts to represent ``Long`` values as ``DateTime`` |
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.
Suggestoin for clarity:
``Product`` POJO. This example attempts to represent ``Long`` values as ``DateTime`` | |
``Product`` POJO. This example attempts to use ``@BsonRepresentation`` to represent ``Long`` values as ``DateTime`` |
:start-after: start class | ||
:end-before: end class | ||
|
||
Then, you can add an instance of the ``LongCodec`` to your ``CodecRegistry``, which contains |
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.
Then, you can add an instance of the ``LongCodec`` to your ``CodecRegistry``, which contains | |
Then, add an instance of the ``LongCodec`` to your ``CodecRegistry``, which contains |
@@ -0,0 +1,29 @@ | |||
package fundamentals; |
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.
Note to tech reviewer: I'm a little uncertain about this example code. The example provided in the ticket didn't convert between Long and Datetime, so I wrote my own example. Let me know if this needs to be updated!
import org.bson.codecs.EncoderContext; | ||
|
||
// start class | ||
public class LongCodec implements Codec<Long> { |
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 would convert all Longs to a Date times, which wouldn't be desired by users.
@BsonRepresentation(BsonType.DATE_TIME)
private Long purchaseDate;
Requires a Codec that implements RepresentationConfigurable<Long>
. eg:
public class LongRepresentableCodec implements Codec<Long>, RepresentationConfigurable<Long> {
private final BsonType representation;
/**
* Constructs a LongRepresentableCodec with a Int64 representation.
*/
public LongRepresentableCodec() {
representation = BsonType.INT64;
}
private LongRepresentableCodec(final BsonType representation) {
this.representation = representation;
}
@Override
public BsonType getRepresentation() {
return representation;
}
@Override
public Codec<Long> withRepresentation(final BsonType representation) {
if (representation != BsonType.INT64 && representation != BsonType.DATE_TIME) {
throw new CodecConfigurationException(representation + " is not a supported representation for LongRepresentableCodec");
}
return new LongRepresentableCodec(representation);
}
@Override
public void encode(final BsonWriter writer, final Long value, final EncoderContext encoderContext) {
switch (representation) {
case INT64:
writer.writeInt64(value);
break;
case DATE_TIME:
writer.writeDateTime(value);
break;
default:
throw new BsonInvalidOperationException("Cannot encode a Long to a " + representation);
}
}
@Override
public Long decode(final BsonReader reader, final DecoderContext decoderContext) {
switch (representation) {
case INT64:
return reader.readInt64();
case DATE_TIME:
return reader.readDateTime();
default:
throw new CodecConfigurationException("Cannot decode " + representation + " to a Long");
}
}
@Override
public Class<Long> getEncoderClass() {
return Long.class;
}
}
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.
Great, thanks for providing this code!
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.
Provided a Codec<Long>
that implements RepresentationConfigurable<Long>
so that users can use it to either encode / decode long values or date times.
* DOCSP-18711: Explain BsonRepresentation limitation * edits * JS feedback * tech review * reduce scroll (cherry picked from commit c4dca6a)
* DOCSP-18711: Explain BsonRepresentation limitation * edits * JS feedback * tech review * reduce scroll (cherry picked from commit c4dca6a)
* DOCSP-18711: Explain BsonRepresentation limitation * edits * JS feedback * tech review * reduce scroll (cherry picked from commit c4dca6a)
Pull Request Info
PR Reviewing Guidelines
JIRA - https://jira.mongodb.org/browse/DOCSP-18711
Staging - https://preview-mongodbnorareidy.gatsbyjs.io/java/DOCSP-18711-bson-representation/fundamentals/data-formats/pojo-customization/#bsonrepresentation-error-example
Self-Review Checklist