Skip to content

Commit

Permalink
Extract classes to top level packages
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Ciocanu <ciocanu@adobe.com>
  • Loading branch information
Artur Ciocanu committed Jul 25, 2024
1 parent a126fe7 commit 9dbdadc
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 328 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import io.dapr.client.DaprClient;
import io.dapr.spring.data.repository.query.DaprPredicateQueryCreator;
import io.dapr.spring.data.repository.query.DaprPredicate;
import io.dapr.utils.TypeRef;
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
import org.springframework.expression.spel.SpelNode;
Expand Down Expand Up @@ -139,20 +139,23 @@ private String createSql(String sqlPattern, String keyspace) {

private String createSql(String sqlPattern, String keyspace, KeyValueQuery<?> query) {
String keyspaceFilter = getKeyspaceFilter(keyspace);
if (query.getCriteria() instanceof DaprPredicateQueryCreator.ValueComparingPredicate) {
String path = ((DaprPredicateQueryCreator.ValueComparingPredicate) query.getCriteria()).getPath().toString();

if (query.getCriteria() instanceof DaprPredicate criteria) {
String path = criteria.getPath().toString();
String pathWithOutType = String.format("'%s'", path.substring(path.indexOf(".") + 1));
String value = String.format("'%s'",
((DaprPredicateQueryCreator.ValueComparingPredicate) query.getCriteria()).getValue().toString());
String value = String.format("'%s'", ((DaprPredicate) query.getCriteria()).getValue().toString());

return String.format(sqlPattern, keyspaceFilter, pathWithOutType, value);
} else if (query.getCriteria() instanceof String) {
SpelExpression expression = PARSER.parseRaw(query.getCriteria().toString());
SpelNode leftNode = expression.getAST().getChild(0);
SpelNode rightNode = expression.getAST().getChild(1);
String left = String.format("'%s'", leftNode.toStringAST());
String right = rightNode.toStringAST();

return String.format(sqlPattern, keyspaceFilter, left, right);
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.dapr.spring.data.repository.query;

import org.springframework.beans.BeanWrapper;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper;
import org.springframework.util.ObjectUtils;

import java.util.function.Function;
import java.util.function.Predicate;

public class DaprPredicate implements Predicate<Object> {

private final PropertyPath path;
private final Function<Object, Boolean> check;
private final Object value;

public DaprPredicate(PropertyPath path, Object expected) {
this(path, expected, (valueToCompare) -> ObjectUtils.nullSafeEquals(valueToCompare, expected));
}


/**
* Creates a new {@link DaprPredicate}.
*
* @param path The path to the property to compare.
* @param value The value to compare.
* @param check The function to check the value.
*/
public DaprPredicate(PropertyPath path, Object value, Function<Object, Boolean> check) {
this.path = path;
this.check = check;
this.value = value;
}

public PropertyPath getPath() {
return path;
}

public Object getValue() {
return value;
}

@Override
public boolean test(Object o) {
Object value = getValueByPath(o, path);
return check.apply(value);
}

private Object getValueByPath(Object root, PropertyPath path) {
Object currentValue = root;

for (PropertyPath currentPath : path) {
currentValue = wrap(currentValue).getPropertyValue(currentPath.getSegment());

if (currentValue == null) {
break;
}
}

return currentValue;
}

private BeanWrapper wrap(Object o) {
return new DirectFieldAccessFallbackBeanWrapper(o);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* Copyright 2024 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
limitations under the License.
*/

package io.dapr.spring.data.repository.query;

import org.springframework.data.repository.query.parser.Part;
import org.springframework.util.ObjectUtils;
import org.springframework.util.comparator.Comparators;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.regex.Pattern;

class DaprPredicateBuilder {

private final Part part;

private DaprPredicateBuilder(Part part) {
this.part = part;
}

static DaprPredicateBuilder propertyValueOf(Part part) {
return new DaprPredicateBuilder(part);
}

Predicate<Object> isTrue() {
return new DaprPredicate(part.getProperty(), true);
}

public Predicate<Object> isFalse() {
return new DaprPredicate(part.getProperty(), false);
}

public Predicate<Object> isEqualTo(Object value) {
return new DaprPredicate(part.getProperty(), value, o -> {
if (!ObjectUtils.nullSafeEquals(Part.IgnoreCaseType.NEVER, part.shouldIgnoreCase())) {
if (o instanceof String s1 && value instanceof String s2) {
return s1.equalsIgnoreCase(s2);
}
}

return ObjectUtils.nullSafeEquals(o, value);
});
}

public Predicate<Object> isNull() {
return new DaprPredicate(part.getProperty(), null, Objects::isNull);
}

public Predicate<Object> isNotNull() {
return isNull().negate();
}

public Predicate<Object> isLessThan(Object value) {
return new DaprPredicate(part.getProperty(), value, o -> Comparators.nullsHigh().compare(o, value) == -1);
}

public Predicate<Object> isLessThanEqual(Object value) {
return new DaprPredicate(part.getProperty(), value, o -> Comparators.nullsHigh().compare(o, value) <= 0);
}

public Predicate<Object> isGreaterThan(Object value) {
return new DaprPredicate(part.getProperty(), value, o -> Comparators.nullsHigh().compare(o, value) == 1);
}

public Predicate<Object> isGreaterThanEqual(Object value) {
return new DaprPredicate(part.getProperty(), value, o -> Comparators.nullsHigh().compare(o, value) >= 0);
}

public Predicate<Object> matches(Pattern pattern) {
return new DaprPredicate(part.getProperty(), null, o -> {
if (o == null) {
return false;
}

return pattern.matcher(o.toString()).find();
});
}

public Predicate<Object> matches(Object value) {
return new DaprPredicate(part.getProperty(), value, o -> {
if (o == null || value == null) {
return ObjectUtils.nullSafeEquals(o, value);
}

if (value instanceof Pattern pattern) {
return pattern.matcher(o.toString()).find();
}

return o.toString().matches(value.toString());
});
}

public Predicate<Object> in(Object value) {
return new DaprPredicate(part.getProperty(), value, o -> {
if (value instanceof Collection<?> collection) {
if (o instanceof Collection<?> subSet) {
return collection.containsAll(subSet);
}

return collection.contains(o);
}

if (ObjectUtils.isArray(value)) {
return ObjectUtils.containsElement(ObjectUtils.toObjectArray(value), value);
}

return false;
});
}

public Predicate<Object> contains(Object value) {
return new DaprPredicate(part.getProperty(), value, o -> {
if (o == null) {
return false;
}

if (o instanceof Collection<?> collection) {
return collection.contains(value);
}

if (ObjectUtils.isArray(o)) {
return ObjectUtils.containsElement(ObjectUtils.toObjectArray(o), value);
}

if (o instanceof Map<?, ?> map) {
return map.containsValue(value);
}

if (value == null) {
return false;
}

String s = o.toString();

if (ObjectUtils.nullSafeEquals(Part.IgnoreCaseType.NEVER, part.shouldIgnoreCase())) {
return s.contains(value.toString());
}

return s.toLowerCase().contains(value.toString().toLowerCase());
});
}

public Predicate<Object> startsWith(Object value) {
return new DaprPredicate(part.getProperty(), value, o -> {
if (!(o instanceof String s)) {
return false;
}

if (ObjectUtils.nullSafeEquals(Part.IgnoreCaseType.NEVER, part.shouldIgnoreCase())) {
return s.startsWith(value.toString());
}

return s.toLowerCase().startsWith(value.toString().toLowerCase());
});
}

public Predicate<Object> endsWith(Object value) {
return new DaprPredicate(part.getProperty(), value, o -> {
if (!(o instanceof String s)) {
return false;
}

if (ObjectUtils.nullSafeEquals(Part.IgnoreCaseType.NEVER, part.shouldIgnoreCase())) {
return s.endsWith(value.toString());
}

return s.toLowerCase().endsWith(value.toString().toLowerCase());
});
}
}
Loading

0 comments on commit 9dbdadc

Please sign in to comment.