Skip to content

Commit fd6b1d0

Browse files
author
Martin Fekete
committed
JsonbExtractor -> JsonbConfiguration
1 parent 40c4ce9 commit fd6b1d0

File tree

9 files changed

+31
-39
lines changed

9 files changed

+31
-39
lines changed

rsql-jpa/src/main/java/io/github/perplexhub/rsql/QuerySupport.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.perplexhub.rsql;
22

3-
import io.github.perplexhub.rsql.jsonb.JsonbExtractor;
3+
import io.github.perplexhub.rsql.jsonb.JsonbConfiguration;
44
import lombok.Builder;
55
import lombok.Data;
66

@@ -29,13 +29,13 @@ public class QuerySupport {
2929
private Collection<String> procedureWhiteList;
3030
private Collection<String> procedureBlackList;
3131
@Builder.Default
32-
private JsonbExtractor jsonbExtractor = JsonbExtractor.DEFAULT;
32+
private JsonbConfiguration jsonbConfiguration = JsonbConfiguration.DEFAULT;
3333

3434
public static class QuerySupportBuilder {}
3535

3636
@Override
3737
public String toString() {
38-
return String.format("%s,distinct:%b,propertyPathMapper:%s,customPredicates:%d,joinHints:%s,propertyWhitelist:%s,propertyBlacklist:%s,jsonbExtractor:%s",
39-
rsqlQuery, distinct, propertyPathMapper, customPredicates == null ? 0 : customPredicates.size(), joinHints, propertyWhitelist, propertyBlacklist, jsonbExtractor);
38+
return String.format("%s,distinct:%b,propertyPathMapper:%s,customPredicates:%d,joinHints:%s,propertyWhitelist:%s,propertyBlacklist:%s,jsonbConfiguration:%s",
39+
rsqlQuery, distinct, propertyPathMapper, customPredicates == null ? 0 : customPredicates.size(), joinHints, propertyWhitelist, propertyBlacklist, jsonbConfiguration);
4040
}
4141
}

rsql-jpa/src/main/java/io/github/perplexhub/rsql/RSQLJPAPredicateConverter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.function.Function;
99
import java.util.stream.Collectors;
1010

11-
import io.github.perplexhub.rsql.jsonb.JsonbExtractor;
11+
import io.github.perplexhub.rsql.jsonb.JsonbConfiguration;
1212
import io.github.perplexhub.rsql.jsonb.JsonbSupport;
1313
import jakarta.persistence.criteria.*;
1414
import jakarta.persistence.metamodel.Attribute;
@@ -38,7 +38,7 @@ public class RSQLJPAPredicateConverter extends RSQLVisitorBase<Predicate, From>
3838
private final Collection<String> procedureBlackList;
3939
private final boolean strictEquality;
4040
private final Character likeEscapeCharacter;
41-
private final JsonbExtractor jsonbExtractor;
41+
private final JsonbConfiguration jsonbConfiguration;
4242

4343
public RSQLJPAPredicateConverter(CriteriaBuilder builder, Map<String, String> propertyPathMapper) {
4444
this(builder, propertyPathMapper, null, null);
@@ -64,7 +64,7 @@ public RSQLJPAPredicateConverter(CriteriaBuilder builder,
6464
Collection<String> proceduresBlackList,
6565
boolean strictEquality,
6666
Character likeEscapeCharacter) {
67-
this(builder, propertyPathMapper, customPredicates, joinHints, proceduresWhiteList, proceduresBlackList, strictEquality, likeEscapeCharacter, JsonbExtractor.DEFAULT);
67+
this(builder, propertyPathMapper, customPredicates, joinHints, proceduresWhiteList, proceduresBlackList, strictEquality, likeEscapeCharacter, JsonbConfiguration.DEFAULT);
6868
}
6969

7070
public RSQLJPAPredicateConverter(CriteriaBuilder builder,
@@ -75,7 +75,7 @@ public RSQLJPAPredicateConverter(CriteriaBuilder builder,
7575
Collection<String> proceduresBlackList,
7676
boolean strictEquality,
7777
Character likeEscapeCharacter,
78-
JsonbExtractor jsonbExtractor) {
78+
JsonbConfiguration jsonbConfiguration) {
7979
this.builder = builder;
8080
this.propertyPathMapper = propertyPathMapper != null ? propertyPathMapper : Collections.emptyMap();
8181
this.customPredicates = customPredicates != null ? customPredicates.stream().collect(Collectors.toMap(RSQLCustomPredicate::getOperator, Function.identity(), (a, b) -> a)) : Collections.emptyMap();
@@ -84,7 +84,7 @@ public RSQLJPAPredicateConverter(CriteriaBuilder builder,
8484
this.procedureBlackList = proceduresBlackList != null ? proceduresBlackList : Collections.emptyList();
8585
this.strictEquality = strictEquality;
8686
this.likeEscapeCharacter = likeEscapeCharacter;
87-
this.jsonbExtractor = jsonbExtractor;
87+
this.jsonbConfiguration = jsonbConfiguration;
8888
}
8989

9090
RSQLJPAContext findPropertyPath(String propertyPath, Path startRoot) {
@@ -259,7 +259,7 @@ private ResolvedExpression resolveExpression(ComparisonNode node, From root, Sel
259259
String jsonbPath = JsonbSupport.jsonPathOfSelector(attribute, jsonSelector);
260260
if(jsonbPath.contains(".")) {
261261
ComparisonNode jsonbNode = node.withSelector(jsonbPath);
262-
return JsonbSupport.jsonbPathExistsExpression(builder, jsonbNode, path, jsonbExtractor);
262+
return JsonbSupport.jsonbPathExistsExpression(builder, jsonbNode, path, jsonbConfiguration);
263263
} else {
264264
final Expression expression;
265265
if (path instanceof JpaExpression jpaExpression) {

rsql-jpa/src/main/java/io/github/perplexhub/rsql/RSQLJPASupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static <T> Specification<T> toSpecification(final QuerySupport querySuppo
128128
querySupport.getCustomPredicates(), querySupport.getJoinHints(),
129129
querySupport.getProcedureWhiteList(), querySupport.getProcedureBlackList(),
130130
querySupport.isStrictEquality(), querySupport.getLikeEscapeCharacter(),
131-
querySupport.getJsonbExtractor());
131+
querySupport.getJsonbConfiguration());
132132

133133
visitor.setPropertyWhitelist(querySupport.getPropertyWhitelist());
134134
visitor.setPropertyBlacklist(querySupport.getPropertyBlacklist());

rsql-jpa/src/main/java/io/github/perplexhub/rsql/jsonb/JsonbExtractor.java renamed to rsql-jpa/src/main/java/io/github/perplexhub/rsql/jsonb/JsonbConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
/**
44
* jsonb expression configuration
55
*/
6-
public interface JsonbExtractor {
6+
public interface JsonbConfiguration {
77

8-
JsonbExtractor DEFAULT = new JsonbExtractor() {
8+
JsonbConfiguration DEFAULT = new JsonbConfiguration() {
99

1010
@Override
1111
public String pathExists() {

rsql-jpa/src/main/java/io/github/perplexhub/rsql/jsonb/JsonbExtractorSupport.java renamed to rsql-jpa/src/main/java/io/github/perplexhub/rsql/jsonb/JsonbConfigurationSupport.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
@Builder
1111
@Accessors(fluent = true)
1212
@Getter
13-
public class JsonbExtractorSupport implements JsonbExtractor {
13+
public final class JsonbConfigurationSupport implements JsonbConfiguration {
1414

1515
@Builder.Default
16-
private final String pathExists = JsonbExtractor.DEFAULT.pathExists();
16+
private final String pathExists = JsonbConfiguration.DEFAULT.pathExists();
1717
@Builder.Default
18-
private final String pathExistsTz = JsonbExtractor.DEFAULT.pathExistsTz();
18+
private final String pathExistsTz = JsonbConfiguration.DEFAULT.pathExistsTz();
1919
@Builder.Default
20-
private final boolean useDateTime = JsonbExtractor.DEFAULT.useDateTime();
20+
private final boolean useDateTime = JsonbConfiguration.DEFAULT.useDateTime();
2121

2222
@Override
2323
public String toString() {

rsql-jpa/src/main/java/io/github/perplexhub/rsql/jsonb/JsonbExpressionBuilder.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
public class JsonbExpressionBuilder {
1717

18-
private final String jsonbPathExistsTz;
19-
private final String jsonbPathExists;
20-
private final boolean useDateTime;
18+
private final JsonbConfiguration configuration;
2119

2220
/**
2321
* The base json type.
@@ -165,19 +163,15 @@ public ArgValue convert(String s) {
165163
Map.entry(BETWEEN, "(%1$s >= %2$s && %1$s <= %3$s)")
166164
);
167165

168-
private static final String JSONB_PATH_EXISTS = "jsonb_path_exists";
169-
170-
private static final String JSONB_PATH_EXISTS_TZ = "jsonb_path_exists_tz";
171-
172166
private final ComparisonOperator operator;
173167
private final String keyPath;
174168
private final List<ArgValue> values;
175169

176170
JsonbExpressionBuilder(ComparisonOperator operator, String keyPath, List<String> args) {
177-
this(operator, keyPath, args, JsonbExtractor.DEFAULT);
171+
this(operator, keyPath, args, JsonbConfiguration.DEFAULT);
178172
}
179173

180-
JsonbExpressionBuilder(ComparisonOperator operator, String keyPath, List<String> args, JsonbExtractor extractor) {
174+
JsonbExpressionBuilder(ComparisonOperator operator, String keyPath, List<String> args, JsonbConfiguration configuration) {
181175
this.operator = Objects.requireNonNull(operator);
182176
this.keyPath = Objects.requireNonNull(keyPath);
183177
if(FORBIDDEN_NEGATION.contains(operator)) {
@@ -196,9 +190,7 @@ public ArgValue convert(String s) {
196190
if(REQUIRE_AT_LEAST_ONE_ARGUMENT.contains(operator) && candidateValues.isEmpty()) {
197191
throw new IllegalArgumentException("Operator " + operator + " requires at least one value");
198192
}
199-
this.useDateTime = extractor.useDateTime();
200-
this.jsonbPathExistsTz = extractor.pathExistsTz();
201-
this.jsonbPathExists = extractor.pathExists();
193+
this.configuration = configuration;
202194
this.values = findMoreTypes(operator, candidateValues);
203195
}
204196

@@ -221,7 +213,7 @@ public JsonbPathExpression getJsonPathExpression() {
221213
List<String> templateArguments = new ArrayList<>();
222214
templateArguments.add(valueReference);
223215
templateArguments.addAll(valuesToCompare);
224-
var function = isDateTimeTz ? jsonbPathExistsTz : jsonbPathExists;
216+
var function = isDateTimeTz ? configuration.pathExistsTz() : configuration.pathExists();
225217
var expression = String.format("%s ? %s", targetPath, String.format(comparisonTemplate, templateArguments.toArray()));
226218
return new JsonbPathExpression(function, expression);
227219
}
@@ -245,7 +237,7 @@ private List<ArgValue> findMoreTypes(ComparisonOperator operator, List<String> v
245237
return values.stream().map(s -> new ArgValue(s, BaseJsonType.STRING)).toList();
246238
}
247239

248-
List<ArgConverter> argConverters = useDateTime ?
240+
List<ArgConverter> argConverters = configuration.useDateTime() ?
249241
List.of(DATE_TIME_CONVERTER, DATE_TIME_CONVERTER_TZ, NUMBER_CONVERTER, BOOLEAN_ARG_CONVERTER)
250242
: List.of(NUMBER_CONVERTER, BOOLEAN_ARG_CONVERTER);
251243
Optional<ArgConverter> candidateConverter = argConverters.stream()

rsql-jpa/src/main/java/io/github/perplexhub/rsql/jsonb/JsonbSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ record JsonbPathExpression(String jsonbFunction, String jsonbPath) {
5959
}
6060

6161

62-
public static ResolvedExpression jsonbPathExistsExpression(CriteriaBuilder builder, ComparisonNode node, Path<?> attrPath, JsonbExtractor extractor) {
62+
public static ResolvedExpression jsonbPathExistsExpression(CriteriaBuilder builder, ComparisonNode node, Path<?> attrPath, JsonbConfiguration configuration) {
6363
var mayBeInvertedOperator = Optional.ofNullable(NEGATE_OPERATORS.get(node.getOperator()));
64-
var jsb = new JsonbExpressionBuilder(mayBeInvertedOperator.orElse(node.getOperator()), node.getSelector(), node.getArguments(), extractor);
64+
var jsb = new JsonbExpressionBuilder(mayBeInvertedOperator.orElse(node.getOperator()), node.getSelector(), node.getArguments(), configuration);
6565
var expression = jsb.getJsonPathExpression();
6666
return ResolvedExpression.ofJson(builder.function(expression.jsonbFunction, Boolean.class, attrPath,
6767
builder.literal(expression.jsonbPath)), mayBeInvertedOperator.isPresent());

rsql-jpa/src/test/java/io/github/perplexhub/rsql/RSQLJPASupportPostgresJsonTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.perplexhub.rsql;
22

3-
import io.github.perplexhub.rsql.jsonb.JsonbExtractorSupport;
3+
import io.github.perplexhub.rsql.jsonb.JsonbConfigurationSupport;
44
import io.github.perplexhub.rsql.model.EntityWithJsonb;
55
import io.github.perplexhub.rsql.model.JsonbEntity;
66
import io.github.perplexhub.rsql.model.PostgresJsonEntity;
@@ -87,7 +87,7 @@ void testJsonSearchOfTemporal(List<PostgresJsonEntity> entities, String rsql, Li
8787
repository.saveAllAndFlush(entities);
8888

8989
//when
90-
List<PostgresJsonEntity> result = repository.findAll(toSpecification(QuerySupport.builder().rsqlQuery(rsql).jsonbExtractor(JsonbExtractorSupport.builder().useDateTime(true).build()).build()));
90+
List<PostgresJsonEntity> result = repository.findAll(toSpecification(QuerySupport.builder().rsqlQuery(rsql).jsonbConfiguration(JsonbConfigurationSupport.builder().useDateTime(true).build()).build()));
9191

9292
//then
9393
assertThat(result)
@@ -728,7 +728,7 @@ void testJsonSearchCustomFunction(List<PostgresJsonEntity> entities, String rsql
728728
repository.saveAllAndFlush(entities);
729729

730730
//when
731-
List<PostgresJsonEntity> result = repository.findAll(toSpecification(QuerySupport.builder().rsqlQuery(rsql).jsonbExtractor(JsonbExtractorSupport.builder().pathExists("my_jsonb_path_exists").build()).build()));
731+
List<PostgresJsonEntity> result = repository.findAll(toSpecification(QuerySupport.builder().rsqlQuery(rsql).jsonbConfiguration(JsonbConfigurationSupport.builder().pathExists("my_jsonb_path_exists").build()).build()));
732732

733733
//then
734734
assertThat(result)

rsql-jpa/src/test/java/io/github/perplexhub/rsql/jsonb/JsonbExpressionBuilderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class JsonbExpressionBuilderTest {
2020
@ParameterizedTest
2121
@MethodSource("data")
2222
void testJsonbPathExpression(ComparisonOperator operator, String keyPath, List<String> arguments, String expectedJsonbFunction, String expectedJsonbPath) {
23-
JsonbExpressionBuilder builder = new JsonbExpressionBuilder(operator, keyPath, arguments, JsonbExtractor.DEFAULT);
23+
JsonbExpressionBuilder builder = new JsonbExpressionBuilder(operator, keyPath, arguments, JsonbConfiguration.DEFAULT);
2424
var expression = builder.getJsonPathExpression();
2525
assertEquals(expectedJsonbFunction, expression.jsonbFunction());
2626
assertEquals(expectedJsonbPath, expression.jsonbPath());
@@ -29,7 +29,7 @@ void testJsonbPathExpression(ComparisonOperator operator, String keyPath, List<S
2929
@ParameterizedTest
3030
@MethodSource("temporal")
3131
void testJsonbPathExpressionWithTemporal(ComparisonOperator operator, String keyPath, List<String> arguments, String expectedJsonbFunction, String expectedJsonbPath) {
32-
JsonbExpressionBuilder builder = new JsonbExpressionBuilder(operator, keyPath, arguments, JsonbExtractorSupport.builder().useDateTime(true).build());
32+
JsonbExpressionBuilder builder = new JsonbExpressionBuilder(operator, keyPath, arguments, JsonbConfigurationSupport.builder().useDateTime(true).build());
3333
var expression = builder.getJsonPathExpression();
3434
assertEquals(expectedJsonbFunction, expression.jsonbFunction());
3535
assertEquals(expectedJsonbPath, expression.jsonbPath());
@@ -38,7 +38,7 @@ void testJsonbPathExpressionWithTemporal(ComparisonOperator operator, String key
3838
@ParameterizedTest
3939
@MethodSource("customized")
4040
void testJsonbPathExpressionCustomized(ComparisonOperator operator, String keyPath, List<String> arguments, String expectedJsonbFunction, String expectedJsonbPath) {
41-
JsonbExpressionBuilder builder = new JsonbExpressionBuilder(operator, keyPath, arguments, JsonbExtractorSupport.builder().pathExists("my_jsonb_path_exists").pathExistsTz("my_jsonb_path_exists_tz").useDateTime(true).build());
41+
JsonbExpressionBuilder builder = new JsonbExpressionBuilder(operator, keyPath, arguments, JsonbConfigurationSupport.builder().pathExists("my_jsonb_path_exists").pathExistsTz("my_jsonb_path_exists_tz").useDateTime(true).build());
4242
var expression = builder.getJsonPathExpression();
4343
assertEquals(expectedJsonbFunction, expression.jsonbFunction());
4444
assertEquals(expectedJsonbPath, expression.jsonbPath());

0 commit comments

Comments
 (0)