-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Deprecate Parameterizable
, introduce default CodecProvider.get(Class<T>, List<Type>, CodecRegistry)
instead
#1115
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2df14bd
Introduce default `CodecProvider.get(Class<T> clazz, List<Type> typeA…
stIncMale 30670c8
Refactor `MapCodecV2`
stIncMale 1723be4
Refactor `DataClassCodec`
stIncMale 4902743
Remove assertions that contradict the `CodecRegistry.get` documentation
stIncMale fe1b60c
Improve `CodecProvider`'s docs
stIncMale 8ccf30c
Improve `CodecProvider`'s docs
stIncMale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,6 @@ | |
import org.bson.codecs.Codec; | ||
import org.bson.codecs.DecoderContext; | ||
import org.bson.codecs.EncoderContext; | ||
import org.bson.codecs.Parameterizable; | ||
import org.bson.codecs.RepresentationConfigurable; | ||
import org.bson.codecs.configuration.CodecConfigurationException; | ||
import org.bson.codecs.configuration.CodecRegistry; | ||
|
@@ -50,10 +49,9 @@ | |
import static java.lang.String.format; | ||
import static org.bson.assertions.Assertions.notNull; | ||
|
||
final class RecordCodec<T extends Record> implements Codec<T>, Parameterizable { | ||
final class RecordCodec<T extends Record> implements Codec<T> { | ||
private static final Logger LOGGER = Loggers.getLogger("RecordCodec"); | ||
private final Class<T> clazz; | ||
private final boolean requiresParameterization; | ||
private final Constructor<?> canonicalConstructor; | ||
private final List<ComponentModel> componentModels; | ||
private final ComponentModel componentModelForId; | ||
|
@@ -251,49 +249,21 @@ private static <T extends Annotation> void validateAnnotationOnlyOnField(final R | |
} | ||
} | ||
|
||
RecordCodec(final Class<T> clazz, final CodecRegistry codecRegistry) { | ||
this.clazz = notNull("class", clazz); | ||
if (clazz.getTypeParameters().length > 0) { | ||
requiresParameterization = true; | ||
canonicalConstructor = null; | ||
componentModels = null; | ||
fieldNameToComponentModel = null; | ||
componentModelForId = null; | ||
} else { | ||
requiresParameterization = false; | ||
canonicalConstructor = notNull("canonicalConstructor", getCanonicalConstructor(clazz)); | ||
componentModels = getComponentModels(clazz, codecRegistry, List.of()); | ||
fieldNameToComponentModel = componentModels.stream() | ||
.collect(Collectors.toMap(ComponentModel::getFieldName, Function.identity())); | ||
componentModelForId = getComponentModelForId(clazz, componentModels); | ||
} | ||
} | ||
|
||
RecordCodec(final Class<T> clazz, final CodecRegistry codecRegistry, final List<Type> types) { | ||
if (types.size() != clazz.getTypeParameters().length || types.isEmpty()) { | ||
RecordCodec(final Class<T> clazz, final List<Type> types, final CodecRegistry codecRegistry) { | ||
if (types.size() != clazz.getTypeParameters().length) { | ||
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 is so much nicer |
||
throw new CodecConfigurationException("Unexpected number of type parameters for record class " + clazz); | ||
} | ||
this.clazz = notNull("class", clazz); | ||
requiresParameterization = false; | ||
canonicalConstructor = notNull("canonicalConstructor", getCanonicalConstructor(clazz)); | ||
componentModels = getComponentModels(clazz, codecRegistry, types); | ||
fieldNameToComponentModel = componentModels.stream() | ||
.collect(Collectors.toMap(ComponentModel::getFieldName, Function.identity())); | ||
componentModelForId = getComponentModelForId(clazz, componentModels); | ||
} | ||
|
||
@Override | ||
public Codec<?> parameterize(final CodecRegistry codecRegistry, final List<Type> types) { | ||
return new RecordCodec<>(clazz, codecRegistry, types); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public T decode(final BsonReader reader, final DecoderContext decoderContext) { | ||
if (requiresParameterization) { | ||
throw new CodecConfigurationException("Can not decode to a record with type parameters that has not been parameterized"); | ||
} | ||
|
||
reader.readStartDocument(); | ||
|
||
Object[] constructorArguments = new Object[componentModels.size()]; | ||
|
@@ -320,10 +290,6 @@ public T decode(final BsonReader reader, final DecoderContext decoderContext) { | |
|
||
@Override | ||
public void encode(final BsonWriter writer, final T record, final EncoderContext encoderContext) { | ||
if (requiresParameterization) { | ||
throw new CodecConfigurationException("Can not decode to a record with type parameters that has not been parameterized"); | ||
} | ||
|
||
writer.writeStartDocument(); | ||
if (componentModelForId != null) { | ||
writeComponent(writer, record, componentModelForId); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If this provider is rolled back to the previous implementation, some tests in
DataClassCodecTest
(for example,testDataClassWithParameterizedDataClass
), which this PR did not modify, start failing withCodecConfigurationException: Could not find codec for number with type N
. I.e., they do test parameterization and it appear to work when the provider properly overrides the newget
method.