Skip to content

Commit d133ef1

Browse files
committed
Polishing.
Refine API naming towards merge/property instead of combine/specify. Tweak documentation. Introduce Resolution.ofValue(…) for easier creation. See #3870 Original pull request: #3986.
1 parent 7617099 commit d133ef1

File tree

10 files changed

+170
-162
lines changed

10 files changed

+170
-162
lines changed

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

+4-9
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,8 @@ public MongoJsonSchemaCreator filter(Predicate<JsonSchemaPropertyContext> filter
9494
}
9595

9696
@Override
97-
public PropertySpecifier specify(String path) {
98-
return new PropertySpecifier() {
99-
@Override
100-
public MongoJsonSchemaCreator types(Class<?>... types) {
101-
return specifyTypesFor(path, types);
102-
}
103-
};
97+
public PropertySpecifier property(String path) {
98+
return types -> withTypesFor(path, types);
10499
}
105100

106101
/**
@@ -111,7 +106,7 @@ public MongoJsonSchemaCreator types(Class<?>... types) {
111106
* @return new instance of {@link MongoJsonSchemaCreator}.
112107
* @since 3.4
113108
*/
114-
public MongoJsonSchemaCreator specifyTypesFor(String path, Class<?>... types) {
109+
public MongoJsonSchemaCreator withTypesFor(String path, Class<?>... types) {
115110

116111
LinkedMultiValueMap<String, Class<?>> clone = mergeProperties.clone();
117112
for (Class<?> type : types) {
@@ -213,7 +208,7 @@ private JsonSchemaProperty computeSchemaForProperty(List<MongoPersistentProperty
213208
}
214209
}
215210
return targetProperties.size() == 1 ? targetProperties.iterator().next()
216-
: JsonSchemaProperty.combined(targetProperties);
211+
: JsonSchemaProperty.merged(targetProperties);
217212
}
218213
}
219214

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

+10-11
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,17 @@ public interface MongoJsonSchemaCreator {
7979
MongoJsonSchema createSchemaFor(Class<?> type);
8080

8181
/**
82-
* Create a combined {@link MongoJsonSchema} out of the individual schemas of the given types by combining their
82+
* Create a merged {@link MongoJsonSchema} out of the individual schemas of the given types by merging their
8383
* properties into one large {@link MongoJsonSchema schema}.
84-
*
84+
*
8585
* @param types must not be {@literal null} nor contain {@literal null}.
8686
* @return new instance of {@link MongoJsonSchema}.
8787
* @since 3.4
8888
*/
89-
default MongoJsonSchema combineSchemaFor(Class<?>... types) {
89+
default MongoJsonSchema mergedSchemaFor(Class<?>... types) {
9090

9191
MongoJsonSchema[] schemas = Arrays.stream(types).map(this::createSchemaFor).toArray(MongoJsonSchema[]::new);
92-
return MongoJsonSchema.combined(schemas);
92+
return MongoJsonSchema.merge(schemas);
9393
}
9494

9595
/**
@@ -108,32 +108,32 @@ default MongoJsonSchema combineSchemaFor(Class<?>... types) {
108108
* @return new instance of {@link PropertySpecifier}.
109109
* @since 3.4
110110
*/
111-
PropertySpecifier specify(String path);
111+
PropertySpecifier property(String path);
112112

113113
/**
114114
* The context in which a specific {@link #getProperty()} is encountered during schema creation.
115-
*
115+
*
116116
* @since 3.3
117117
*/
118118
interface JsonSchemaPropertyContext {
119119

120120
/**
121121
* The path to a given field/property in dot notation.
122-
*
122+
*
123123
* @return never {@literal null}.
124124
*/
125125
String getPath();
126126

127127
/**
128128
* The current property.
129-
*
129+
*
130130
* @return never {@literal null}.
131131
*/
132132
MongoPersistentProperty getProperty();
133133

134134
/**
135135
* Obtain the {@link MongoPersistentEntity} for a given property.
136-
*
136+
*
137137
* @param property must not be {@literal null}.
138138
* @param <T>
139139
* @return {@literal null} if the property is not an entity. It is nevertheless recommend to check
@@ -234,7 +234,6 @@ static MongoJsonSchemaCreator create() {
234234
}
235235

236236
/**
237-
* @since 3.4
238237
* @author Christoph Strobl
239238
* @since 3.4
240239
*/
@@ -246,6 +245,6 @@ interface PropertySpecifier {
246245
* @param types must not be {@literal null}.
247246
* @return the source
248247
*/
249-
MongoJsonSchemaCreator types(Class<?>... types);
248+
MongoJsonSchemaCreator withTypes(Class<?>... types);
250249
}
251250
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/JsonSchemaProperty.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,14 @@ static JsonSchemaProperty required(JsonSchemaProperty property) {
236236
}
237237

238238
/**
239-
* Combines multiple {@link JsonSchemaProperty} with potentially different attributes into one.
239+
* Merges multiple {@link JsonSchemaProperty} with potentially different attributes into one.
240240
*
241241
* @param properties must not be {@literal null}.
242242
* @return new instance of {@link JsonSchemaProperty}.
243243
* @since 3.4
244244
*/
245-
static JsonSchemaProperty combined(Collection<JsonSchemaProperty> properties) {
246-
return new CombinedJsonSchemaProperty(properties);
245+
static JsonSchemaProperty merged(Collection<JsonSchemaProperty> properties) {
246+
return new MergedJsonSchemaProperty(properties);
247247
}
248248

249249
/**
+6-5
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,30 @@
2424
import org.bson.Document;
2525

2626
/**
27-
* {@link MongoJsonSchema} implementation that is capable of combining properties of different schemas into one.
27+
* {@link MongoJsonSchema} implementation that is capable of merging properties from different schemas into a single
28+
* one.
2829
*
2930
* @author Christoph Strobl
3031
* @since 3.4
3132
*/
32-
class CombinedJsonSchema implements MongoJsonSchema {
33+
class MergedJsonSchema implements MongoJsonSchema {
3334

3435
private final List<MongoJsonSchema> schemaList;
3536
private final BiFunction<Map<String, Object>, Map<String, Object>, Document> mergeFunction;
3637

37-
CombinedJsonSchema(List<MongoJsonSchema> schemaList, ConflictResolutionFunction conflictResolutionFunction) {
38+
MergedJsonSchema(List<MongoJsonSchema> schemaList, ConflictResolutionFunction conflictResolutionFunction) {
3839
this(schemaList, new TypeUnifyingMergeFunction(conflictResolutionFunction));
3940
}
4041

41-
CombinedJsonSchema(List<MongoJsonSchema> schemaList,
42+
MergedJsonSchema(List<MongoJsonSchema> schemaList,
4243
BiFunction<Map<String, Object>, Map<String, Object>, Document> mergeFunction) {
4344

4445
this.schemaList = new ArrayList<>(schemaList);
4546
this.mergeFunction = mergeFunction;
4647
}
4748

4849
@Override
49-
public MongoJsonSchema combineWith(Collection<MongoJsonSchema> sources) {
50+
public MongoJsonSchema mergeWith(Collection<MongoJsonSchema> sources) {
5051

5152
schemaList.addAll(sources);
5253
return this;
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,24 @@
3030
* @author Christoph Strobl
3131
* @since 3.4
3232
*/
33-
class CombinedJsonSchemaProperty implements JsonSchemaProperty {
33+
class MergedJsonSchemaProperty implements JsonSchemaProperty {
3434

3535
private final Iterable<JsonSchemaProperty> properties;
3636
private final BiFunction<Map<String, Object>, Map<String, Object>, Document> mergeFunction;
3737

38-
CombinedJsonSchemaProperty(Iterable<JsonSchemaProperty> properties) {
38+
MergedJsonSchemaProperty(Iterable<JsonSchemaProperty> properties) {
3939
this(properties, (k, a, b) -> {
4040
throw new IllegalStateException(
41-
String.format("Error resolving conflict for %s. No conflict resolution function defined.", k));
41+
String.format("Error resolving conflict for '%s'. No conflict resolution function defined.", k));
4242
});
4343
}
4444

45-
CombinedJsonSchemaProperty(Iterable<JsonSchemaProperty> properties,
45+
MergedJsonSchemaProperty(Iterable<JsonSchemaProperty> properties,
4646
ConflictResolutionFunction conflictResolutionFunction) {
4747
this(properties, new TypeUnifyingMergeFunction(conflictResolutionFunction));
4848
}
4949

50-
CombinedJsonSchemaProperty(Iterable<JsonSchemaProperty> properties,
50+
MergedJsonSchemaProperty(Iterable<JsonSchemaProperty> properties,
5151
BiFunction<Map<String, Object>, Map<String, Object>, Document> mergeFunction) {
5252

5353
this.properties = properties;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/MongoJsonSchema.java

+65-26
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.bson.Document;
2626
import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.ObjectJsonSchemaObject;
2727
import org.springframework.lang.Nullable;
28+
import org.springframework.util.Assert;
2829

2930
/**
3031
* Interface defining MongoDB-specific JSON schema object. New objects can be built with {@link #builder()}, for
@@ -79,7 +80,7 @@ default Document toDocument() {
7980

8081
/**
8182
* Create the {@link Document} defining the schema. <br />
82-
* Property and field names need to be mapped to the domain type ones by running the {@link Document} through a
83+
* Property and field names need to be mapped to the domain type property by running the {@link Document} through a
8384
* {@link org.springframework.data.mongodb.core.convert.JsonSchemaMapper} to apply field name customization.
8485
*
8586
* @return never {@literal null}.
@@ -108,69 +109,69 @@ static MongoJsonSchema of(Document document) {
108109
}
109110

110111
/**
111-
* Create a new {@link MongoJsonSchema} combining properties from the given sources.
112+
* Create a new {@link MongoJsonSchema} merging properties from the given sources.
112113
*
113114
* @param sources must not be {@literal null}.
114115
* @return new instance of {@link MongoJsonSchema}.
115116
* @since 3.4
116117
*/
117-
static MongoJsonSchema combined(MongoJsonSchema... sources) {
118-
return combined((path, a, b) -> {
119-
throw new IllegalStateException(
120-
String.format("Failure combining schema for path %s holding values a) %s and b) %s.", path.dotPath(), a, b));
118+
static MongoJsonSchema merge(MongoJsonSchema... sources) {
119+
return merge((path, left, right) -> {
120+
throw new IllegalStateException(String.format("Cannot merge schema for path '%s' holding values '%s' and '%s'.",
121+
path.dotPath(), left, right));
121122
}, sources);
122123
}
123124

124125
/**
125-
* Create a new {@link MongoJsonSchema} combining properties from the given sources.
126+
* Create a new {@link MongoJsonSchema} merging properties from the given sources.
126127
*
127128
* @param sources must not be {@literal null}.
128129
* @return new instance of {@link MongoJsonSchema}.
129130
* @since 3.4
130131
*/
131-
static MongoJsonSchema combined(ConflictResolutionFunction mergeFunction, MongoJsonSchema... sources) {
132-
return new CombinedJsonSchema(Arrays.asList(sources), mergeFunction);
132+
static MongoJsonSchema merge(ConflictResolutionFunction mergeFunction, MongoJsonSchema... sources) {
133+
return new MergedJsonSchema(Arrays.asList(sources), mergeFunction);
133134
}
134135

135136
/**
136-
* Create a new {@link MongoJsonSchema} combining properties from the given sources.
137+
* Create a new {@link MongoJsonSchema} merging properties from the given sources.
137138
*
138139
* @param sources must not be {@literal null}.
139140
* @return new instance of {@link MongoJsonSchema}.
140141
* @since 3.4
141142
*/
142-
default MongoJsonSchema combineWith(MongoJsonSchema... sources) {
143-
return combineWith(Arrays.asList(sources));
143+
default MongoJsonSchema mergeWith(MongoJsonSchema... sources) {
144+
return mergeWith(Arrays.asList(sources));
144145
}
145146

146147
/**
147-
* Create a new {@link MongoJsonSchema} combining properties from the given sources.
148+
* Create a new {@link MongoJsonSchema} merging properties from the given sources.
148149
*
149150
* @param sources must not be {@literal null}.
150151
* @return new instance of {@link MongoJsonSchema}.
151152
* @since 3.4
152153
*/
153-
default MongoJsonSchema combineWith(Collection<MongoJsonSchema> sources) {
154-
return combineWith(sources, (path, a, b) -> {
155-
throw new IllegalStateException(
156-
String.format("Failure combining schema for path %s holding values a) %s and b) %s.", path.dotPath(), a, b));
154+
default MongoJsonSchema mergeWith(Collection<MongoJsonSchema> sources) {
155+
return mergeWith(sources, (path, left, right) -> {
156+
throw new IllegalStateException(String.format("Cannot merge schema for path '%s' holding values '%s' and '%s'.",
157+
path.dotPath(), left, right));
157158
});
158159
}
159160

160161
/**
161-
* Create a new {@link MongoJsonSchema} combining properties from the given sources.
162+
* Create a new {@link MongoJsonSchema} merging properties from the given sources.
162163
*
163164
* @param sources must not be {@literal null}.
164165
* @return new instance of {@link MongoJsonSchema}.
165166
* @since 3.4
166167
*/
167-
default MongoJsonSchema combineWith(Collection<MongoJsonSchema> sources,
168+
default MongoJsonSchema mergeWith(Collection<MongoJsonSchema> sources,
168169
ConflictResolutionFunction conflictResolutionFunction) {
169170

170171
List<MongoJsonSchema> schemaList = new ArrayList<>(sources.size() + 1);
171172
schemaList.add(this);
172173
schemaList.addAll(new ArrayList<>(sources));
173-
return new CombinedJsonSchema(schemaList, conflictResolutionFunction);
174+
return new MergedJsonSchema(schemaList, conflictResolutionFunction);
174175
}
175176

176177
/**
@@ -183,8 +184,8 @@ static MongoJsonSchemaBuilder builder() {
183184
}
184185

185186
/**
186-
* A resolution function that may be called on conflicting paths. Eg. when trying to merge properties with different
187-
* values into one.
187+
* A resolution function that is called on conflicting paths when trying to merge properties with different values
188+
* into a single value.
188189
*
189190
* @author Christoph Strobl
190191
* @since 3.4
@@ -193,12 +194,14 @@ static MongoJsonSchemaBuilder builder() {
193194
interface ConflictResolutionFunction {
194195

195196
/**
197+
* Resolve the conflict for two values under the same {@code path}.
198+
*
196199
* @param path the {@link Path} leading to the conflict.
197-
* @param a can be {@literal null}.
198-
* @param b can be {@literal null}.
200+
* @param left can be {@literal null}.
201+
* @param right can be {@literal null}.
199202
* @return never {@literal null}.
200203
*/
201-
Resolution resolveConflict(Path path, @Nullable Object a, @Nullable Object b);
204+
Resolution resolveConflict(Path path, @Nullable Object left, @Nullable Object right);
202205

203206
/**
204207
* @author Christoph Strobl
@@ -218,7 +221,7 @@ interface Path {
218221
}
219222

220223
/**
221-
* The result after processing a conflict when combining schemas. May indicate to {@link #SKIP skip} the entry
224+
* The result after processing a conflict when merging schemas. May indicate to {@link #SKIP skip} the entry
222225
* entirely.
223226
*
224227
* @author Christoph Strobl
@@ -260,6 +263,42 @@ public Object setValue(Object value) {
260263
static Resolution skip() {
261264
return SKIP;
262265
}
266+
267+
/**
268+
* Construct a resolution for a {@link Path} using the given {@code value}.
269+
*
270+
* @param path the conflicting path.
271+
* @param value the value to apply.
272+
* @return
273+
*/
274+
static Resolution ofValue(Path path, Object value) {
275+
276+
Assert.notNull(path, "Path must not be null");
277+
278+
return ofValue(path.currentElement(), value);
279+
}
280+
281+
/**
282+
* Construct a resolution from a {@code key} and {@code value}.
283+
*
284+
* @param key name of the path segment, typically {@link Path#currentElement()}
285+
* @param value the value to apply.
286+
* @return
287+
*/
288+
static Resolution ofValue(String key, Object value) {
289+
290+
return new Resolution() {
291+
@Override
292+
public String getKey() {
293+
return key;
294+
}
295+
296+
@Override
297+
public Object getValue() {
298+
return value;
299+
}
300+
};
301+
}
263302
}
264303
}
265304

0 commit comments

Comments
 (0)