25
25
import org .bson .Document ;
26
26
import org .springframework .data .mongodb .core .schema .TypedJsonSchemaObject .ObjectJsonSchemaObject ;
27
27
import org .springframework .lang .Nullable ;
28
+ import org .springframework .util .Assert ;
28
29
29
30
/**
30
31
* Interface defining MongoDB-specific JSON schema object. New objects can be built with {@link #builder()}, for
@@ -79,7 +80,7 @@ default Document toDocument() {
79
80
80
81
/**
81
82
* 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
83
84
* {@link org.springframework.data.mongodb.core.convert.JsonSchemaMapper} to apply field name customization.
84
85
*
85
86
* @return never {@literal null}.
@@ -108,69 +109,69 @@ static MongoJsonSchema of(Document document) {
108
109
}
109
110
110
111
/**
111
- * Create a new {@link MongoJsonSchema} combining properties from the given sources.
112
+ * Create a new {@link MongoJsonSchema} merging properties from the given sources.
112
113
*
113
114
* @param sources must not be {@literal null}.
114
115
* @return new instance of {@link MongoJsonSchema}.
115
116
* @since 3.4
116
117
*/
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 ));
121
122
}, sources );
122
123
}
123
124
124
125
/**
125
- * Create a new {@link MongoJsonSchema} combining properties from the given sources.
126
+ * Create a new {@link MongoJsonSchema} merging properties from the given sources.
126
127
*
127
128
* @param sources must not be {@literal null}.
128
129
* @return new instance of {@link MongoJsonSchema}.
129
130
* @since 3.4
130
131
*/
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 );
133
134
}
134
135
135
136
/**
136
- * Create a new {@link MongoJsonSchema} combining properties from the given sources.
137
+ * Create a new {@link MongoJsonSchema} merging properties from the given sources.
137
138
*
138
139
* @param sources must not be {@literal null}.
139
140
* @return new instance of {@link MongoJsonSchema}.
140
141
* @since 3.4
141
142
*/
142
- default MongoJsonSchema combineWith (MongoJsonSchema ... sources ) {
143
- return combineWith (Arrays .asList (sources ));
143
+ default MongoJsonSchema mergeWith (MongoJsonSchema ... sources ) {
144
+ return mergeWith (Arrays .asList (sources ));
144
145
}
145
146
146
147
/**
147
- * Create a new {@link MongoJsonSchema} combining properties from the given sources.
148
+ * Create a new {@link MongoJsonSchema} merging properties from the given sources.
148
149
*
149
150
* @param sources must not be {@literal null}.
150
151
* @return new instance of {@link MongoJsonSchema}.
151
152
* @since 3.4
152
153
*/
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 ));
157
158
});
158
159
}
159
160
160
161
/**
161
- * Create a new {@link MongoJsonSchema} combining properties from the given sources.
162
+ * Create a new {@link MongoJsonSchema} merging properties from the given sources.
162
163
*
163
164
* @param sources must not be {@literal null}.
164
165
* @return new instance of {@link MongoJsonSchema}.
165
166
* @since 3.4
166
167
*/
167
- default MongoJsonSchema combineWith (Collection <MongoJsonSchema > sources ,
168
+ default MongoJsonSchema mergeWith (Collection <MongoJsonSchema > sources ,
168
169
ConflictResolutionFunction conflictResolutionFunction ) {
169
170
170
171
List <MongoJsonSchema > schemaList = new ArrayList <>(sources .size () + 1 );
171
172
schemaList .add (this );
172
173
schemaList .addAll (new ArrayList <>(sources ));
173
- return new CombinedJsonSchema (schemaList , conflictResolutionFunction );
174
+ return new MergedJsonSchema (schemaList , conflictResolutionFunction );
174
175
}
175
176
176
177
/**
@@ -183,8 +184,8 @@ static MongoJsonSchemaBuilder builder() {
183
184
}
184
185
185
186
/**
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 .
188
189
*
189
190
* @author Christoph Strobl
190
191
* @since 3.4
@@ -193,12 +194,14 @@ static MongoJsonSchemaBuilder builder() {
193
194
interface ConflictResolutionFunction {
194
195
195
196
/**
197
+ * Resolve the conflict for two values under the same {@code path}.
198
+ *
196
199
* @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}.
199
202
* @return never {@literal null}.
200
203
*/
201
- Resolution resolveConflict (Path path , @ Nullable Object a , @ Nullable Object b );
204
+ Resolution resolveConflict (Path path , @ Nullable Object left , @ Nullable Object right );
202
205
203
206
/**
204
207
* @author Christoph Strobl
@@ -218,7 +221,7 @@ interface Path {
218
221
}
219
222
220
223
/**
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
222
225
* entirely.
223
226
*
224
227
* @author Christoph Strobl
@@ -260,6 +263,42 @@ public Object setValue(Object value) {
260
263
static Resolution skip () {
261
264
return SKIP ;
262
265
}
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
+ }
263
302
}
264
303
}
265
304
0 commit comments