Support enum arguments on repository queries.

Support enum in AbstractCouchbaseConverter.convertForWriteIfNeeded()
and also call that from
MappingCouchbaseConverter.getPotentiallyConvertedSimpleWrite()

Closes #1069.
Original pull request: #1112.

Co-authored-by: mikereiche <michael.reiche@couchbase.com>
This commit is contained in:
Michael Reiche
2021-03-24 14:25:28 -07:00
committed by mikereiche
parent fe8d3feb48
commit d16bbff4fe
5 changed files with 27 additions and 10 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.data.couchbase.core.convert;
import java.util.Collections;
import java.util.Optional;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.ConversionService;
@@ -100,7 +101,8 @@ public abstract class AbstractCouchbaseConverter implements CouchbaseConverter,
return this.conversions.getCustomWriteTarget(value.getClass()) //
.map(it -> (Object) this.conversionService.convert(value, it)) //
.orElse(value);
.orElseGet(() -> Enum.class.isAssignableFrom(value.getClass()) ? ((Enum<?>) value).name() : value);
}
@Override

View File

@@ -774,15 +774,8 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implem
target.put(key, getPotentiallyConvertedSimpleWrite(source));
}
private Object getPotentiallyConvertedSimpleWrite(final Object value) {
if (value == null) {
return null;
}
Optional<Class<?>> customTarget = conversions.getCustomWriteTarget(value.getClass());
return customTarget.map(it -> (Object) conversionService.convert(value, it))
.orElseGet(() -> Enum.class.isAssignableFrom(value.getClass()) ? ((Enum<?>) value).name() : value);
public Object getPotentiallyConvertedSimpleWrite(final Object value) {
return convertForWriteIfNeeded(value);
}
/**

View File

@@ -50,6 +50,9 @@ public interface AirportRepository extends PagingAndSortingRepository<Airport, S
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Airport findByIata(String iata);
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Airport findByIata(Iata iata);
@Query("#{#n1ql.selectEntity} where iata = $1")
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
List<Airport> getAllByIata(String iata);

View File

@@ -0,0 +1,6 @@
package org.springframework.data.couchbase.domain;
public enum Iata {
vie,
xxx
}

View File

@@ -47,6 +47,7 @@ import org.springframework.data.couchbase.core.query.QueryCriteria;
import org.springframework.data.couchbase.domain.Address;
import org.springframework.data.couchbase.domain.Airport;
import org.springframework.data.couchbase.domain.AirportRepository;
import org.springframework.data.couchbase.domain.Iata;
import org.springframework.data.couchbase.domain.Person;
import org.springframework.data.couchbase.domain.PersonRepository;
import org.springframework.data.couchbase.domain.User;
@@ -167,6 +168,18 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
}
}
@Test
void findByEnum() {
Airport vie = null;
try {
vie = new Airport("airports::vie", "vie", "loww");
vie = airportRepository.save(vie);
Airport airport2 = airportRepository.findByIata(Iata.vie);
assertEquals(airport2.getId(), vie.getId());
} finally {
airportRepository.delete(vie);
}
}
@Test
public void testCas() {
User user = new User("1", "Dave", "Wilson");