Skip to content

Commit

Permalink
Use field name specified in annotation if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
malteseduck committed Jun 27, 2018
1 parent 6036af6 commit 5ec7239
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ open class QueryCriteriaMarkLogicExecutor<T, ID : Serializable>(
is Word -> words(annotation, property, value)
is Range -> range(annotation, property, value)
is Value -> value(annotation, property, value)
else -> value(property, value)
else -> value(property.name, value)
}
}
}
Expand Down Expand Up @@ -174,12 +174,11 @@ open class QueryCriteriaMarkLogicExecutor<T, ID : Serializable>(
}

private fun <Q : QueryCriteria<T>> value(annotation: Value, property: KProperty1<Q, *>, value: Any): StructuredQueryDefinition {
val fieldName = if (annotation.field.isNotBlank()) annotation.field else property.name
return value(property, value, annotation.options, annotation.weight)
val fieldName = if (annotation.field.isNotBlank()) annotation.field else property.name
return value(fieldName, value, annotation.options, annotation.weight)
}

private fun <Q : QueryCriteria<T>> value(property: KProperty1<Q, *>, value: Any, options: Array<String>? = arrayOf("exact"), weight: Double = 1.0): StructuredQueryDefinition {
val fieldName = property.name
private fun value(fieldName: String, value: Any, options: Array<String>? = arrayOf("exact"), weight: Double = 1.0): StructuredQueryDefinition {

return wrapScope(fieldName) {
when (value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ data class PersonCriteria(
@Word
var name: String? = null,

@Value(field = "name")
var hasExactNameOf: String? = null,

var age: Int? = null,

@Range(field = "age", operator = GT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Expand Down Expand Up @@ -79,18 +80,28 @@ private void cleanDb() {
@Test
public void testFindsPersonsByName() {
PersonCriteria criteria = new PersonCriteria();
criteria.setName("Jane");
criteria.setName("Jan");

List<Person> people = repository.findAll(criteria);
assertThat(people).containsExactly(jane);
}

@Test
public void testFindsPersonsByExactName() {
PersonCriteria criteria = new PersonCriteria();
criteria.setHasExactNameOf("Jane");

List<Person> people = repository.findAll(criteria);
assertThat(people).containsExactly(jane);
}

@Test
public void testFindsPersonsByGender() {
PersonCriteria criteria = new PersonCriteria();
criteria.setGender("female");

List<Person> people = repository.findAll(criteria);
assertThat(people).containsExactly(andrea, jenny, jane);
List<Person> people = repository.findAll(criteria, Sort.by("name"));
assertThat(people).containsExactly(andrea, jane, jenny);
}

@Test
Expand Down

0 comments on commit 5ec7239

Please sign in to comment.