-
Notifications
You must be signed in to change notification settings - Fork 213
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
feat: add operators validation #3809
Merged
ndr-brt
merged 2 commits into
eclipse-edc:main
from
Think-iT-Labs:3713-add-operators-validator
Feb 6, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...or-core/src/main/java/org/eclipse/edc/connector/core/store/ContainsOperatorPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.connector.core.store; | ||
|
||
import org.eclipse.edc.spi.query.OperatorPredicate; | ||
|
||
import java.util.Collection; | ||
|
||
public class ContainsOperatorPredicate implements OperatorPredicate { | ||
|
||
@Override | ||
public boolean test(Object property, Object operandRight) { | ||
if (property instanceof Collection<?> collection) { | ||
return collection.contains(operandRight); | ||
} | ||
|
||
return false; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...ore/src/main/java/org/eclipse/edc/connector/core/store/CriterionOperatorRegistryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.connector.core.store; | ||
|
||
import org.eclipse.edc.spi.query.Criterion; | ||
import org.eclipse.edc.spi.query.CriterionOperatorRegistry; | ||
import org.eclipse.edc.spi.query.OperatorPredicate; | ||
import org.eclipse.edc.spi.query.PropertyLookup; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
|
||
import static java.lang.String.format; | ||
|
||
/** | ||
* Implementation for {@link CriterionOperatorRegistry} | ||
*/ | ||
public class CriterionOperatorRegistryImpl implements CriterionOperatorRegistry { | ||
|
||
private final Map<String, OperatorPredicate> operatorPredicates = new HashMap<>(); | ||
private final List<PropertyLookup> propertyLookups = new ArrayList<>(); | ||
|
||
public static CriterionOperatorRegistry ofDefaults() { | ||
var registry = new CriterionOperatorRegistryImpl(); | ||
registry.registerPropertyLookup(new ReflectionPropertyLookup()); | ||
registry.registerPropertyLookup(new AssetPropertyLookup()); | ||
registry.registerOperatorPredicate(EQUAL, new EqualOperatorPredicate()); | ||
registry.registerOperatorPredicate(IN, new InOperatorPredicate()); | ||
registry.registerOperatorPredicate(LIKE, new LikeOperatorPredicate()); | ||
registry.registerOperatorPredicate(CONTAINS, new ContainsOperatorPredicate()); | ||
return registry; | ||
} | ||
|
||
@Override | ||
public void registerOperatorPredicate(String operator, OperatorPredicate converter) { | ||
operatorPredicates.put(operator.toLowerCase(), converter); | ||
} | ||
|
||
@Override | ||
public void registerPropertyLookup(PropertyLookup propertyLookup) { | ||
propertyLookups.add(0, propertyLookup); | ||
} | ||
|
||
@Override | ||
public void unregister(String operator) { | ||
operatorPredicates.remove(operator.toLowerCase()); | ||
} | ||
|
||
@Override | ||
public boolean isSupported(String operator) { | ||
return operatorPredicates.containsKey(operator.toLowerCase()); | ||
} | ||
|
||
@Override | ||
public <T> Predicate<T> toPredicate(Criterion criterion) { | ||
var predicate = operatorPredicates.get(criterion.getOperator().toLowerCase()); | ||
if (predicate == null) { | ||
throw new IllegalArgumentException(format("Operator [%s] is not supported.", criterion.getOperator())); | ||
} | ||
|
||
return t -> { | ||
|
||
var operandLeft = (String) criterion.getOperandLeft(); | ||
|
||
var property = propertyLookups.stream() | ||
.map(it -> it.getProperty(operandLeft, t)) | ||
.filter(Objects::nonNull) | ||
.findFirst() | ||
.orElse(null); | ||
|
||
if (property == null) { | ||
return false; | ||
} | ||
|
||
return predicate.test(property, criterion.getOperandRight()); | ||
}; | ||
|
||
} | ||
|
||
} |
153 changes: 0 additions & 153 deletions
153
...src/main/java/org/eclipse/edc/connector/core/store/CriterionToPredicateConverterImpl.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
theoretically
operandRight
could be aList
itself, IDK what would happen then...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the (postgre)SQL context it can be only a single object, because eventually I guess another operator/syntax would need to be used.
I think if we want to being able to pass a list as
operandRight
we'll need another operator (containsAll
?)