Skip to content

Commit 574f973

Browse files
committed
Add type-safe property paths overloads (#2126)
Fixes #2126 Signed-off-by: Emilien Bevierre <emilien.bevierre@couchbase.com>
1 parent f6122cb commit 574f973

13 files changed

Lines changed: 467 additions & 0 deletions

src/main/java/org/springframework/data/couchbase/core/ExecutableFindByIdOperation.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
package org.springframework.data.couchbase.core;
1717

1818
import java.time.Duration;
19+
import java.util.Arrays;
1920
import java.util.Collection;
2021

22+
import org.springframework.data.core.TypedPropertyPath;
2123
import org.springframework.data.couchbase.core.support.OneAndAllId;
2224
import org.springframework.data.couchbase.core.support.InCollection;
2325
import org.springframework.data.couchbase.core.support.WithGetOptions;
@@ -33,6 +35,7 @@
3335
*
3436
* @author Christoph Strobl
3537
* @author Tigran Babloyan
38+
* @author Emilien Bevierre
3639
* @since 2.0
3740
*/
3841
public interface ExecutableFindByIdOperation {
@@ -122,6 +125,17 @@ interface FindByIdWithProjection<T> extends FindByIdInScope<T>, WithProjectionId
122125
*/
123126
@Override
124127
FindByIdInScope<T> project(String... fields);
128+
129+
/**
130+
* Type-safe variant of {@link #project(String...)} using property paths.
131+
*
132+
* @param fields the property paths to project.
133+
* @since 6.1
134+
*/
135+
@SuppressWarnings("unchecked")
136+
default FindByIdInScope<T> project(TypedPropertyPath<?, ?>... fields) {
137+
return project(Arrays.stream(fields).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
138+
}
125139
}
126140

127141
interface FindByIdWithExpiry<T> extends FindByIdWithProjection<T>, WithExpiry<T> {

src/main/java/org/springframework/data/couchbase/core/ExecutableFindByQueryOperation.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.util.stream.Stream;
2121

2222
import org.springframework.dao.IncorrectResultSizeDataAccessException;
23+
import java.util.Arrays;
24+
25+
import org.springframework.data.core.TypedPropertyPath;
2326
import org.springframework.data.couchbase.core.query.Query;
2427
import org.springframework.data.couchbase.core.query.QueryCriteriaDefinition;
2528
import org.springframework.data.couchbase.core.support.InCollection;
@@ -38,6 +41,7 @@
3841
* Query Operations
3942
*
4043
* @author Christoph Strobl
44+
* @author Emilien Bevierre
4145
* @since 2.0
4246
*/
4347
public interface ExecutableFindByQueryOperation {
@@ -270,6 +274,17 @@ interface FindByQueryWithProjecting<T> extends FindByQueryWithProjection<T> {
270274
* @throws IllegalArgumentException if returnType is {@literal null}.
271275
*/
272276
FindByQueryWithProjection<T> project(String[] fields);
277+
278+
/**
279+
* Type-safe variant of {@link #project(String[])} using property paths.
280+
*
281+
* @param fields the property paths to project.
282+
* @since 6.1
283+
*/
284+
@SuppressWarnings("unchecked")
285+
default FindByQueryWithProjection<T> project(TypedPropertyPath<?, ?>... fields) {
286+
return project(Arrays.stream(fields).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
287+
}
273288
}
274289

275290
/**
@@ -288,6 +303,17 @@ interface FindByQueryWithDistinct<T> extends FindByQueryWithProjecting<T>, WithD
288303
*/
289304
@Override
290305
FindByQueryWithProjection<T> distinct(String[] distinctFields);
306+
307+
/**
308+
* Type-safe variant of {@link #distinct(String[])} using property paths.
309+
*
310+
* @param distinctFields the property paths for distinct fields.
311+
* @since 6.1
312+
*/
313+
@SuppressWarnings("unchecked")
314+
default FindByQueryWithProjection<T> distinct(TypedPropertyPath<?, ?>... distinctFields) {
315+
return distinct(Arrays.stream(distinctFields).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
316+
}
291317
}
292318

293319
/**

src/main/java/org/springframework/data/couchbase/core/ExecutableMutateInByIdOperation.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@
1919
import com.couchbase.client.java.kv.MutateInOptions;
2020
import com.couchbase.client.java.kv.PersistTo;
2121
import com.couchbase.client.java.kv.ReplicateTo;
22+
import org.springframework.data.core.TypedPropertyPath;
2223
import org.springframework.data.couchbase.core.support.*;
2324
import reactor.core.publisher.Flux;
2425
import reactor.core.publisher.Mono;
2526

2627
import java.time.Duration;
28+
import java.util.Arrays;
2729
import java.util.Collection;
2830

2931
/**
3032
* Mutate In Operations
3133
*
3234
* @author Tigran Babloyan
35+
* @author Emilien Bevierre
3336
* @since 5.1
3437
*/
3538
public interface ExecutableMutateInByIdOperation {
@@ -98,6 +101,42 @@ interface MutateInByIdWithPaths<T> extends TerminatingMutateInById<T>, WithMutat
98101
* By default the CAS value is not provided.
99102
*/
100103
MutateInByIdWithPaths<T> withCasProvided();
104+
105+
/**
106+
* Type-safe variant of {@link #withRemovePaths(String...)} using property paths.
107+
* @since 6.1
108+
*/
109+
@SuppressWarnings("unchecked")
110+
default MutateInByIdWithPaths<T> withRemovePaths(TypedPropertyPath<?, ?>... removePaths) {
111+
return withRemovePaths(Arrays.stream(removePaths).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
112+
}
113+
114+
/**
115+
* Type-safe variant of {@link #withInsertPaths(String...)} using property paths.
116+
* @since 6.1
117+
*/
118+
@SuppressWarnings("unchecked")
119+
default MutateInByIdWithPaths<T> withInsertPaths(TypedPropertyPath<?, ?>... insertPaths) {
120+
return withInsertPaths(Arrays.stream(insertPaths).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
121+
}
122+
123+
/**
124+
* Type-safe variant of {@link #withUpsertPaths(String...)} using property paths.
125+
* @since 6.1
126+
*/
127+
@SuppressWarnings("unchecked")
128+
default MutateInByIdWithPaths<T> withUpsertPaths(TypedPropertyPath<?, ?>... upsertPaths) {
129+
return withUpsertPaths(Arrays.stream(upsertPaths).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
130+
}
131+
132+
/**
133+
* Type-safe variant of {@link #withReplacePaths(String...)} using property paths.
134+
* @since 6.1
135+
*/
136+
@SuppressWarnings("unchecked")
137+
default MutateInByIdWithPaths<T> withReplacePaths(TypedPropertyPath<?, ?>... replacePaths) {
138+
return withReplacePaths(Arrays.stream(replacePaths).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
139+
}
101140
}
102141

103142
/**

src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperation.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import reactor.core.publisher.Mono;
2020

2121
import java.time.Duration;
22+
import java.util.Arrays;
2223
import java.util.Collection;
2324

25+
import org.springframework.data.core.TypedPropertyPath;
2426
import org.springframework.data.couchbase.core.support.InCollection;
2527
import org.springframework.data.couchbase.core.support.InScope;
2628
import org.springframework.data.couchbase.core.support.OneAndAllIdReactive;
@@ -36,6 +38,7 @@
3638
*
3739
* @author Christoph Strobl
3840
* @author Tigran Babloyan
41+
* @author Emilien Bevierre
3942
* @since 2.0
4043
*/
4144
public interface ReactiveFindByIdOperation {
@@ -126,6 +129,17 @@ interface FindByIdWithProjection<T> extends FindByIdInScope<T>, WithProjectionId
126129
*/
127130
FindByIdInCollection<T> project(String... fields);
128131

132+
/**
133+
* Type-safe variant of {@link #project(String...)} using property paths.
134+
*
135+
* @param fields the property paths to project.
136+
* @since 6.1
137+
*/
138+
@SuppressWarnings("unchecked")
139+
default FindByIdInCollection<T> project(TypedPropertyPath<?, ?>... fields) {
140+
return project(Arrays.stream(fields).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
141+
}
142+
129143
}
130144

131145
interface FindByIdWithExpiry<T> extends FindByIdWithProjection<T>, WithExpiry<T> {

src/main/java/org/springframework/data/couchbase/core/ReactiveFindByQueryOperation.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
import reactor.core.publisher.Flux;
1919
import reactor.core.publisher.Mono;
2020

21+
import java.util.Arrays;
22+
2123
import org.springframework.dao.IncorrectResultSizeDataAccessException;
24+
import org.springframework.data.core.TypedPropertyPath;
2225
import org.springframework.data.couchbase.core.query.Query;
2326
import org.springframework.data.couchbase.core.query.QueryCriteriaDefinition;
2427
import org.springframework.data.couchbase.core.support.InCollection;
@@ -38,6 +41,7 @@
3841
*
3942
* @author Michael Nitschinger
4043
* @author Michael Reiche
44+
* @author Emilien Bevierre
4145
*/
4246
public interface ReactiveFindByQueryOperation {
4347

@@ -217,6 +221,16 @@ interface FindByQueryWithProjecting<T> extends FindByQueryWithProjection<T> {
217221
* @throws IllegalArgumentException if returnType is {@literal null}.
218222
*/
219223
FindByQueryWithProjection<T> project(String[] fields);
224+
225+
/**
226+
* Type-safe variant of {@link #project(String[])} using property paths.
227+
*
228+
* @param fields the property paths to project.
229+
* @since 6.1
230+
*/
231+
default FindByQueryWithProjection<T> project(TypedPropertyPath<?, ?>... fields) {
232+
return project(Arrays.stream(fields).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
233+
}
220234
}
221235

222236
/**
@@ -234,6 +248,16 @@ interface FindByQueryWithDistinct<T> extends FindByQueryWithProjecting<T>, WithD
234248
* @throws IllegalArgumentException if field is {@literal null}.
235249
*/
236250
FindByQueryWithProjection<T> distinct(String[] distinctFields);
251+
252+
/**
253+
* Type-safe variant of {@link #distinct(String[])} using property paths.
254+
*
255+
* @param distinctFields the property paths for distinct fields.
256+
* @since 6.1
257+
*/
258+
default FindByQueryWithProjection<T> distinct(TypedPropertyPath<?, ?>... distinctFields) {
259+
return distinct(Arrays.stream(distinctFields).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
260+
}
237261
}
238262

239263
/**

src/main/java/org/springframework/data/couchbase/core/ReactiveMutateInByIdOperation.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@
1919
import com.couchbase.client.java.kv.MutateInOptions;
2020
import com.couchbase.client.java.kv.PersistTo;
2121
import com.couchbase.client.java.kv.ReplicateTo;
22+
import org.springframework.data.core.TypedPropertyPath;
2223
import org.springframework.data.couchbase.core.support.*;
2324
import reactor.core.publisher.Flux;
2425
import reactor.core.publisher.Mono;
2526

2627
import java.time.Duration;
28+
import java.util.Arrays;
2729
import java.util.Collection;
2830

2931
/**
3032
* Mutate In Operations
3133
*
3234
* @author Tigran Babloyan
35+
* @author Emilien Bevierre
3336
* @since 5.1
3437
*/
3538
public interface ReactiveMutateInByIdOperation {
@@ -98,6 +101,38 @@ interface MutateInByIdWithPaths<T> extends TerminatingMutateInById<T>, WithMutat
98101
* By default the CAS value is not provided.
99102
*/
100103
MutateInByIdWithPaths<T> withCasProvided();
104+
105+
/**
106+
* Type-safe variant of {@link #withRemovePaths(String...)} using property paths.
107+
* @since 6.1
108+
*/
109+
default MutateInByIdWithPaths<T> withRemovePaths(TypedPropertyPath<?, ?>... removePaths) {
110+
return withRemovePaths(Arrays.stream(removePaths).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
111+
}
112+
113+
/**
114+
* Type-safe variant of {@link #withInsertPaths(String...)} using property paths.
115+
* @since 6.1
116+
*/
117+
default MutateInByIdWithPaths<T> withInsertPaths(TypedPropertyPath<?, ?>... insertPaths) {
118+
return withInsertPaths(Arrays.stream(insertPaths).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
119+
}
120+
121+
/**
122+
* Type-safe variant of {@link #withUpsertPaths(String...)} using property paths.
123+
* @since 6.1
124+
*/
125+
default MutateInByIdWithPaths<T> withUpsertPaths(TypedPropertyPath<?, ?>... upsertPaths) {
126+
return withUpsertPaths(Arrays.stream(upsertPaths).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
127+
}
128+
129+
/**
130+
* Type-safe variant of {@link #withReplacePaths(String...)} using property paths.
131+
* @since 6.1
132+
*/
133+
default MutateInByIdWithPaths<T> withReplacePaths(TypedPropertyPath<?, ?>... replacePaths) {
134+
return withReplacePaths(Arrays.stream(replacePaths).map(TypedPropertyPath::toDotPath).toArray(String[]::new));
135+
}
101136
}
102137

103138
/**

src/main/java/org/springframework/data/couchbase/core/query/Query.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.slf4j.Logger;
2727
import org.slf4j.LoggerFactory;
2828

29+
import org.springframework.data.core.TypedPropertyPath;
2930
import org.springframework.data.core.TypeInformation;
3031
import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate;
3132
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
@@ -47,6 +48,7 @@
4748
/**
4849
* @author Michael Nitschinger
4950
* @author Michael Reiche
51+
* @author Emilien Bevierre
5052
*/
5153
public class Query {
5254

@@ -176,6 +178,18 @@ public Query distinct(String[] distinctFields) {
176178
return this;
177179
}
178180

181+
/**
182+
* Type-safe variant of {@link #distinct(String[])} using property references.
183+
*
184+
* @param distinctFields the property references to use as distinct fields.
185+
* @since 6.1
186+
*/
187+
@SafeVarargs
188+
public final <T> Query distinct(TypedPropertyPath<T, ?>... distinctFields) {
189+
this.distinctFields = Arrays.stream(distinctFields).map(TypedPropertyPath::toDotPath).toArray(String[]::new);
190+
return this;
191+
}
192+
179193
/**
180194
* distinctFields for query (non-null but empty means all fields) ? {@code distinctFields}.
181195
*

0 commit comments

Comments
 (0)