-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Introduce ConversionContext and clean up MappingMongoConverter #3575
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0e3d4c0
Prepare issue branch.
mp911de bab34c7
Deduplicate code
mp911de 3b8f28a
Introduce ConversionContext.
mp911de c792536
Polishing.
mp911de 9419861
Polishing.
mp911de 8e56d3c
Remove MapUtils and use BsonUtils instead.
christophstrobl 2f802cd
Remove duplicate code.
christophstrobl 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
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
926 changes: 485 additions & 441 deletions
926
...db/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -17,12 +17,14 @@ | |
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Date; | ||
import java.util.Map; | ||
import java.util.StringJoiner; | ||
import java.util.function.Function; | ||
import java.util.stream.StreamSupport; | ||
|
||
import org.bson.BSONObject; | ||
import org.bson.BsonBinary; | ||
import org.bson.BsonBoolean; | ||
import org.bson.BsonDouble; | ||
|
@@ -36,11 +38,11 @@ | |
import org.bson.conversions.Bson; | ||
import org.bson.json.JsonParseException; | ||
import org.bson.types.ObjectId; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.mongodb.CodecRegistryProvider; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.CollectionUtils; | ||
import org.springframework.util.ObjectUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
|
@@ -50,6 +52,8 @@ | |
import com.mongodb.MongoClientSettings; | ||
|
||
/** | ||
* Internal API for operations on {@link Bson} elements that can be either {@link Document} or {@link DBObject}. | ||
* | ||
* @author Christoph Strobl | ||
* @author Mark Paluch | ||
* @since 2.0 | ||
|
@@ -62,6 +66,47 @@ public static <T> T get(Bson bson, String key) { | |
return (T) asMap(bson).get(key); | ||
} | ||
|
||
/** | ||
* Remove {@code _id : null} from the given {@link Bson} if present. | ||
* | ||
* @param bson must not be {@literal null}. | ||
* @since 2.5 | ||
*/ | ||
public static void removeNullId(Bson bson) { | ||
|
||
if (!contains(bson, "_id", null)) { | ||
return; | ||
} | ||
|
||
removeFrom(bson, "_id"); | ||
} | ||
|
||
/** | ||
* Check if a given entry (key/value pair) is present in the given {@link Bson}. | ||
* | ||
* @param bson must not be {@literal null}. | ||
* @param key must not be {@literal null}. | ||
* @param value can be {@literal null}. | ||
* @return {@literal true} if (key/value pair) is present. | ||
* @since 2.5 | ||
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 should be |
||
*/ | ||
public static boolean contains(Bson bson, String key, @Nullable Object value) { | ||
|
||
if (bson instanceof Document) { | ||
|
||
Document doc = (Document) bson; | ||
return doc.containsKey(key) && ObjectUtils.nullSafeEquals(doc.get(key), value); | ||
} | ||
if (bson instanceof BSONObject) { | ||
|
||
BSONObject bsonObject = (BSONObject) bson; | ||
return bsonObject.containsField(key) && ObjectUtils.nullSafeEquals(bsonObject.get(key), value); | ||
} | ||
|
||
Map<String, Object> map = asMap(bson); | ||
return map.containsKey(key) && ObjectUtils.nullSafeEquals(map.get(key), value); | ||
} | ||
|
||
public static Map<String, Object> asMap(Bson bson) { | ||
|
||
if (bson instanceof Document) { | ||
|
@@ -70,21 +115,78 @@ public static Map<String, Object> asMap(Bson bson) { | |
if (bson instanceof BasicDBObject) { | ||
return ((BasicDBObject) bson); | ||
} | ||
if (bson instanceof DBObject) { | ||
return ((DBObject) bson).toMap(); | ||
} | ||
|
||
return (Map) bson.toBsonDocument(Document.class, MongoClientSettings.getDefaultCodecRegistry()); | ||
} | ||
|
||
public static void addToMap(Bson bson, String key, @Nullable Object value) { | ||
|
||
if (bson instanceof Document) { | ||
|
||
((Document) bson).put(key, value); | ||
return; | ||
} | ||
if (bson instanceof DBObject) { | ||
((DBObject) bson).put(key, value); | ||
if (bson instanceof BSONObject) { | ||
|
||
((BSONObject) bson).put(key, value); | ||
return; | ||
} | ||
throw new IllegalArgumentException("o_O what's that? Cannot add value to " + bson.getClass()); | ||
|
||
throw new IllegalArgumentException(String.format( | ||
"Cannot add key/value pair to %s. as map. Given Bson must be a Document or BSONObject!", bson.getClass())); | ||
} | ||
|
||
/** | ||
* Add all entries from the given {@literal source} {@link Map} to the {@literal target}. | ||
* | ||
* @param target must not be {@literal null}. | ||
* @param source must not be {@literal null}. | ||
* @since 2.5 | ||
*/ | ||
public static void addAllToMap(Bson target, Map<String, ?> source) { | ||
|
||
if (target instanceof Document) { | ||
|
||
((Document) target).putAll(source); | ||
return; | ||
} | ||
|
||
if (target instanceof BSONObject) { | ||
|
||
((BSONObject) target).putAll(source); | ||
return; | ||
} | ||
|
||
throw new IllegalArgumentException( | ||
String.format("Cannot add all to %s. Given Bson must be a Document or BSONObject.", target.getClass())); | ||
} | ||
|
||
/** | ||
* Remove the given {@literal key} from the {@link Bson} value. | ||
* | ||
* @param bson must not be {@literal null}. | ||
* @param key must not be {@literal null}. | ||
* @since 2.5 | ||
*/ | ||
static void removeFrom(Bson bson, String key) { | ||
|
||
if (bson instanceof Document) { | ||
|
||
((Document) bson).remove(key); | ||
return; | ||
} | ||
|
||
if (bson instanceof BSONObject) { | ||
|
||
((BSONObject) bson).removeField(key); | ||
return; | ||
} | ||
|
||
throw new IllegalArgumentException( | ||
String.format("Cannot remove from %s. Given Bson must be a Document or BSONObject.", bson.getClass())); | ||
} | ||
|
||
/** | ||
|
@@ -282,6 +384,23 @@ public static Document parse(String json, @Nullable CodecRegistryProvider codecR | |
.orElseGet(() -> new DocumentCodec(codecRegistryProvider.getCodecRegistry()))); | ||
} | ||
|
||
/** | ||
* Returns given object as {@link Collection}. Will return the {@link Collection} as is if the source is a | ||
* {@link Collection} already, will convert an array into a {@link Collection} or simply create a single element | ||
* collection for everything else. | ||
* | ||
* @param source | ||
* @return | ||
*/ | ||
public static Collection<?> asCollection(Object source) { | ||
|
||
if (source instanceof Collection) { | ||
return (Collection<?>) source; | ||
} | ||
|
||
return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source); | ||
} | ||
|
||
@Nullable | ||
private static String toJson(@Nullable Object value) { | ||
|
||
|
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
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.
This should be
@since 3.2