Skip to content

Commit 92079ca

Browse files
mp911dechristophstrobl
authored andcommitted
Introduce ConversionContext.
Introduce a ConversionContext used during the mapping process to carry forward required information. ConversionContext serves as entrpoint for recursive (read) conversion of documents, lists, maps, and simple values. The actual decision which converter strategy to apply is now encapsulated by ConversionContext.convert(…) which removes strategy duplications from the actual conversion methods. Also, converter methods for documents, maps, lists, … are now protected for easier customization by subclasses. Closes #3571 Original Pull Request: #3575
1 parent 52b13cc commit 92079ca

File tree

5 files changed

+609
-437
lines changed

5 files changed

+609
-437
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DocumentAccessor.java

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public Object get(MongoPersistentProperty property) {
154154
* @param entity must not be {@literal null}.
155155
* @return
156156
*/
157+
@Nullable
157158
public Object getRawId(MongoPersistentEntity<?> entity) {
158159
return entity.hasIdProperty() ? get(entity.getRequiredIdProperty()) : BsonUtils.asMap(document).get("_id");
159160
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.convert;
17+
18+
import java.util.Collection;
19+
import java.util.Collections;
20+
import java.util.Map;
21+
22+
import org.bson.Document;
23+
import org.bson.conversions.Bson;
24+
25+
import org.springframework.lang.Nullable;
26+
import org.springframework.util.CollectionUtils;
27+
28+
import com.mongodb.DBObject;
29+
30+
/**
31+
* @author Mark Paluch
32+
*/
33+
class MapUtils {
34+
/**
35+
* Returns given object as {@link Collection}. Will return the {@link Collection} as is if the source is a
36+
* {@link Collection} already, will convert an array into a {@link Collection} or simply create a single element
37+
* collection for everything else.
38+
*
39+
* @param source
40+
* @return
41+
*/
42+
static Collection<?> asCollection(Object source) {
43+
44+
if (source instanceof Collection) {
45+
return (Collection<?>) source;
46+
}
47+
48+
return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source);
49+
}
50+
51+
@SuppressWarnings("unchecked")
52+
static Map<String, Object> asMap(Bson bson) {
53+
54+
if (bson instanceof Document) {
55+
return (Document) bson;
56+
}
57+
58+
if (bson instanceof DBObject) {
59+
return ((DBObject) bson).toMap();
60+
}
61+
62+
throw new IllegalArgumentException(
63+
String.format("Cannot read %s. as map. Given Bson must be a Document or DBObject!", bson.getClass()));
64+
}
65+
66+
static void addToMap(Bson bson, String key, @Nullable Object value) {
67+
68+
if (bson instanceof Document) {
69+
((Document) bson).put(key, value);
70+
return;
71+
}
72+
if (bson instanceof DBObject) {
73+
((DBObject) bson).put(key, value);
74+
return;
75+
}
76+
throw new IllegalArgumentException(String.format(
77+
"Cannot add key/value pair to %s. as map. Given Bson must be a Document or DBObject!", bson.getClass()));
78+
}
79+
80+
static void addAllToMap(Bson bson, Map<String, ?> value) {
81+
82+
if (bson instanceof Document) {
83+
((Document) bson).putAll(value);
84+
return;
85+
}
86+
87+
if (bson instanceof DBObject) {
88+
((DBObject) bson).putAll(value);
89+
return;
90+
}
91+
92+
throw new IllegalArgumentException(
93+
String.format("Cannot add all to %s. Given Bson must be a Document or DBObject.", bson.getClass()));
94+
}
95+
96+
static void removeFromMap(Bson bson, String key) {
97+
98+
if (bson instanceof Document) {
99+
((Document) bson).remove(key);
100+
return;
101+
}
102+
103+
if (bson instanceof DBObject) {
104+
((DBObject) bson).removeField(key);
105+
return;
106+
}
107+
108+
throw new IllegalArgumentException(
109+
String.format("Cannot remove from %s. Given Bson must be a Document or DBObject.", bson.getClass()));
110+
}
111+
}

0 commit comments

Comments
 (0)