Skip to content

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

Merged
merged 5 commits into from
Jun 27, 2024

Conversation

norareidy
Copy link
Contributor

@norareidy norareidy commented May 30, 2024

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

  • Is this free of any warnings or errors in the RST?
  • Did you run a spell-check?
  • Did you run a grammar-check?
  • Are all the links working?
  • Are the facets and meta keywords accurate?

Copy link
Contributor

@jordan-smith721 jordan-smith721 left a 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``
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestoin for clarity:

Suggested change
``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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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

@norareidy norareidy requested review from a team and rozza and removed request for a team June 3, 2024 14:03
@@ -0,0 +1,29 @@
package fundamentals;
Copy link
Contributor Author

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> {
Copy link
Member

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;
    }
}

Copy link
Contributor Author

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!

Copy link
Member

@rozza rozza left a 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.

@norareidy norareidy requested a review from rozza June 21, 2024 17:30
@norareidy norareidy merged commit c4dca6a into mongodb:master Jun 27, 2024
2 checks passed
@norareidy norareidy deleted the DOCSP-18711-bson-representation branch June 27, 2024 14:36
norareidy added a commit that referenced this pull request Jun 27, 2024
* DOCSP-18711: Explain BsonRepresentation limitation

* edits

* JS feedback

* tech review

* reduce scroll

(cherry picked from commit c4dca6a)
norareidy added a commit that referenced this pull request Jun 27, 2024
* DOCSP-18711: Explain BsonRepresentation limitation

* edits

* JS feedback

* tech review

* reduce scroll

(cherry picked from commit c4dca6a)
norareidy added a commit that referenced this pull request Jun 27, 2024
* DOCSP-18711: Explain BsonRepresentation limitation

* edits

* JS feedback

* tech review

* reduce scroll

(cherry picked from commit c4dca6a)
norareidy added a commit that referenced this pull request Jun 27, 2024
* DOCSP-18711: Explain BsonRepresentation limitation

* edits

* JS feedback

* tech review

* reduce scroll

(cherry picked from commit c4dca6a)
(cherry picked from commit 3cf764c)
norareidy added a commit that referenced this pull request Jun 27, 2024
* DOCSP-18711: Explain BsonRepresentation limitation

* edits

* JS feedback

* tech review

* reduce scroll

(cherry picked from commit c4dca6a)
(cherry picked from commit 3cf764c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants