-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Support recursive types in parameterized records #1007
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
Conversation
bson-record-codec/src/test/unit/org/bson/codecs/record/RecordCodecTest.java
Show resolved
Hide resolved
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.
Interesting, would an alternative be to keep this within the RecordCodec implementaiton - so no changes to the CodecRegistry?
For example would just adding a LazyRecordCodec
be enough to break any cycles eg:
class LazyRecordCodec<T> implements Codec<T> {
private final Supplier<Codec<T>> supplier;
private volatile Codec<T> wrapped;
LazyRecordCodec(final Supplier<Codec<T>> supplier) {
this.supplier = supplier;
}
@Override
public void encode(final BsonWriter writer, final T value, final EncoderContext encoderContext) {
getWrapped().encode(writer, value, encoderContext);
}
@Override
public Class<T> getEncoderClass() {
return getWrapped().getEncoderClass();
}
@Override
public T decode(final BsonReader reader, final DecoderContext decoderContext) {
return getWrapped().decode(reader, decoderContext);
}
private Codec<T> getWrapped() {
if (wrapped == null) {
wrapped = supplier.get();
}
return wrapped;
}
}
Then the codec could be:
this.codec = new LazyRecordCodec<>(() -> computeCodec(typeParameters, component, codecRegistry));
The downside is it makes all records codecs lazy but the upside would be no changes required outside the RecordCodec.
Yes, I think we could handle cycles outside the registry. But it's cleaner if we put it in the registry, and we get the additional benefit of caching parameterized types. |
bson-record-codec/src/test/unit/org/bson/codecs/record/RecordCodecTest.java
Outdated
Show resolved
Hide resolved
bson-record-codec/src/test/unit/org/bson/codecs/record/RecordCodecTest.java
Outdated
Show resolved
Hide resolved
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.
One question - but its looking good
Provide codec support for records whose type declarations contain cycles. JAVA-4745
This reverts commit 126853ac7d79074be7de85fb2d9c91a73a1c40f1.
Provide codec support for records whose type declarations contain cycles.
JAVA-4745
Looking for feedback on the general approach. The codec registry changes could use some more unit tests if we want to proceed with this.