Skip to content

Handle Collection<> parameters to repository query methods. #1271

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

Merged
merged 1 commit into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@
import static org.springframework.data.couchbase.core.query.N1QLExpression.x;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Formatter;
import java.util.LinkedList;
import java.util.List;

import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.couchbase.core.mapping.CouchbaseList;
import org.springframework.lang.Nullable;

import com.couchbase.client.core.error.InvalidArgumentException;
import com.couchbase.client.java.json.JsonArray;
import com.couchbase.client.java.json.JsonObject;
import com.couchbase.client.java.json.JsonValue;
import org.springframework.util.CollectionUtils;

/**
* @author Michael Nitschinger
Expand Down Expand Up @@ -412,8 +417,8 @@ private String maybeWrapValue(N1QLExpression key, Object value, int[] paramIndex
try {
params.add(convert(converter, value));
} catch (InvalidArgumentException iae) {
if (value instanceof Object[]) {
addAsArray(params, value, converter);
if (value instanceof Object[] || value instanceof Collection) {
addAsCollection(params, asCollection(value), converter);
} else {
throw iae;
}
Expand Down Expand Up @@ -462,15 +467,28 @@ private static Object convert(CouchbaseConverter converter, Object value) {
return converter != null ? converter.convertForWriteIfNeeded(value) : value;
}

private void addAsArray(JsonArray posValues, Object o, CouchbaseConverter converter) {
Object[] array = (Object[]) o;
private void addAsCollection(JsonArray posValues, Collection collection, CouchbaseConverter converter) {
JsonArray ja = JsonValue.ja();
for (Object e : array) {
for (Object e : collection) {
ja.add(String.valueOf(convert(converter, e)));
}
posValues.add(ja);
}

/**
* Returns a collection from the given source object. From MappingCouchbaseConverter.
*
* @param source the source object.
* @return the target collection.
*/
private static Collection<?> asCollection(final Object source) {
if (source instanceof Collection) {
return (Collection<?>) source;
}
return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source);
}


private String maybeBackTic(String value) {
if (value == null || (value.startsWith("`") && value.endsWith("`"))) {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public interface AirportRepository extends CouchbaseRepository<Airport, String>,
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Airport findByIata(Iata iata);

@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Airport findByIataIn(java.util.Collection<Iata> iatas);

@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Airport findByIataIn(Iata[] iata);

// NOT_BOUNDED to test ScanConsistency
// @ScanConsistency(query = QueryScanConsistency.NOT_BOUNDED)
Airport iata(String iata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.springframework.data.couchbase.domain.AirportMini;
import org.springframework.data.couchbase.domain.AirportRepository;
import org.springframework.data.couchbase.domain.AirportRepositoryScanConsistencyTest;
import org.springframework.data.couchbase.domain.Iata;
import org.springframework.data.couchbase.domain.NaiveAuditorAware;
import org.springframework.data.couchbase.domain.Person;
import org.springframework.data.couchbase.domain.PersonRepository;
Expand Down Expand Up @@ -370,10 +371,21 @@ void findByEnum() {
try {
vie = new Airport("airports::vie", "vie", "loww");
vie = airportRepository.save(vie);
Airport airport2 = airportRepository.findByIata(vie.getIata());
Airport airport2 = airportRepository.findByIata(Iata.vie);
assertNotNull(airport2, "should have found " + vie);
assertEquals(airport2.getId(), vie.getId());

Airport airport3 = airportRepository.findByIataIn(new Iata[]{Iata.vie, Iata.xxx});
assertNotNull(airport3, "should have found " + vie);
assertEquals(airport3.getId(), vie.getId());

java.util.Collection<Iata> iatas = new ArrayList<>();
iatas.add(Iata.vie);
iatas.add(Iata.xxx);
Airport airport4 = airportRepository.findByIataIn( iatas );
assertNotNull(airport4, "should have found " + vie);
assertEquals(airport4.getId(), vie.getId());

} finally {
airportRepository.delete(vie);
}
Expand Down