Skip to content
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

Add scoped selectors #391

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
478 changes: 381 additions & 97 deletions docs/source/spec/core/selectors.rst

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.smithy.model.selector;

import java.math.BigDecimal;
import java.util.Locale;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
* Compares two selector attribute values.
*/
@FunctionalInterface
interface AttributeComparator {

AttributeComparator EQUALS = stringComparator(String::equals);
AttributeComparator NOT_EQUALS = stringComparator((a, b) -> !a.equals(b));
AttributeComparator STARTS_WITH = stringComparator(String::startsWith);
AttributeComparator ENDS_WITH = stringComparator(String::endsWith);
AttributeComparator CONTAINS = stringComparator(String::contains);
AttributeComparator GT = stringComparator((a, b) -> numericComparison(a, b, result -> result == 1));
AttributeComparator GTE = stringComparator((a, b) -> numericComparison(a, b, result -> result >= 0));
AttributeComparator LT = stringComparator((a, b) -> numericComparison(a, b, result -> result <= -1));
AttributeComparator LTE = stringComparator((a, b) -> numericComparison(a, b, result -> result <= 0));
AttributeComparator EXISTS = AttributeComparator::existsCheck;

/**
* Compares the left hand side value against the right using a comparator.
*
* @param lhs Left value of the comparison.
* @param rhs Right value of the comparison.
* @param caseInsensitive Whether or not the comparison is case-insensitive.
* @return Returns true if the values match the comparator.
*/
boolean compare(AttributeValue lhs, AttributeValue rhs, boolean caseInsensitive);

/**
* Compares the given attribute values by flattening each side of the
* comparison, and comparing each value.
*
* <p>This method is necessary in order to support matching on projections.
*
* @param lhs The left hand side of the comparison.
* @param rhs The right hand side of the comparison.
* @param insensitive Whether or not to use a case-insensitive comparison.
* @return Returns true if the attributes match the comparator.
*/
default boolean flattenedCompare(AttributeValue lhs, AttributeValue rhs, boolean insensitive) {
for (AttributeValue l : lhs.getFlattenedValues()) {
for (AttributeValue r : rhs.getFlattenedValues()) {
if (compare(l, r, insensitive)) {
return true;
}
}
}

return false;
}

// String comparators simplify how comparisons are made on attribute
// values that MUST resolve to strings.
static AttributeComparator stringComparator(BiFunction<String, String, Boolean> compare) {
return (lhs, rhs, caseInsensitive) -> {
// Both values MUST be present to compare.
if (!lhs.isPresent() || !rhs.isPresent()) {
return false;
}

String lhsString = lhs.toString();
String rhsString = rhs.toString();

// Convert both sides of the comparison to lowercase when case insensitive.
if (caseInsensitive) {
lhsString = lhsString.toLowerCase(Locale.ENGLISH);
rhsString = rhsString.toLowerCase(Locale.ENGLISH);
}

return compare.apply(lhsString, rhsString);
};
}

// Try to parse both numbers, ignore numeric failures since that's acceptable,
// then pass the result of calling compareTo on the numbers to the given
// evaluator. The evaluator then determines if the comparison is what was expected.
static boolean numericComparison(String lhs, String rhs, Function<Integer, Boolean> evaluator) {
BigDecimal lhsNumber = parseNumber(lhs);
if (lhsNumber == null) {
return false;
}

BigDecimal rhsNumber = parseNumber(rhs);
if (rhsNumber == null) {
return false;
}

return evaluator.apply(lhsNumber.compareTo(rhsNumber));
}

// Invalid numbers do not fail the parser or evaluation of a selector.
static BigDecimal parseNumber(String token) {
try {
return new BigDecimal(token);
} catch (NumberFormatException e) {
return null;
}
}

// Checks if a value "exists" and if the expected boolean string matches
// the resolved existence boolean.
static boolean existsCheck(AttributeValue a, AttributeValue b, boolean caseInsensitive) {
String bString = b.toString();
return (a.isPresent() && bString.equals("true"))
|| (!a.isPresent() && b.toString().equals("false"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,138 +15,70 @@

package software.amazon.smithy.model.selector;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.neighbor.NeighborProvider;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.utils.ListUtils;

/**
* Matches shapes with a specific attribute.
* Matches shapes with a specific attribute or that matches an attribute comparator.
*/
final class AttributeSelector implements Selector {

static final Comparator EQUALS = String::equals;
static final Comparator NOT_EQUALS = (a, b) -> !a.equals(b);
static final Comparator STARTS_WITH = String::startsWith;
static final Comparator ENDS_WITH = String::endsWith;
static final Comparator CONTAINS = String::contains;

static final Comparator GT = (a, b) -> numericComparison(a, b, i -> i == 1);
static final Comparator GTE = (a, b) -> numericComparison(a, b, i -> i >= 0);
static final Comparator LT = (a, b) -> numericComparison(a, b, i -> i <= -1);
static final Comparator LTE = (a, b) -> numericComparison(a, b, i -> i <= 0);

static final KeyGetter KEY_ID = (shape) -> ListUtils.of(shape.getId().toString());
static final KeyGetter KEY_ID_NAMESPACE = (shape) -> ListUtils.of(shape.getId().getNamespace());
static final KeyGetter KEY_ID_NAME = (shape) -> ListUtils.of(shape.getId().getName());
static final KeyGetter KEY_ID_MEMBER = (shape) -> shape.getId().getMember()
.map(Collections::singletonList)
.orElseGet(Collections::emptyList);
static final KeyGetter KEY_SERVICE_VERSION = (shape) -> shape.asServiceShape()
.map(ServiceShape::getVersion)
.map(Collections::singletonList)
.orElseGet(Collections::emptyList);

private final KeyGetter key;
private final List<String> expected;
private final Comparator comparator;
private final AttributeValue.Factory key;
private final List<AttributeValue> expected;
private final AttributeComparator comparator;
private final boolean caseInsensitive;

interface KeyGetter extends Function<Shape, List<String>> {}

interface Comparator extends BiFunction<String, String, Boolean> {}

AttributeSelector(KeyGetter key) {
this.key = key;
expected = null;
comparator = null;
caseInsensitive = false;
}

AttributeSelector(
KeyGetter key,
Comparator comparator,
AttributeValue.Factory key,
List<String> expected,
AttributeComparator comparator,
boolean caseInsensitive
) {
this.expected = expected;
this.key = key;
this.caseInsensitive = caseInsensitive;
this.comparator = comparator;

// Case insensitive comparisons are made by converting both
// side of the comparison to lowercase.
if (caseInsensitive) {
for (int i = 0; i < expected.size(); i++) {
expected.set(i, expected.get(i).toLowerCase(Locale.ENGLISH));
// Create the valid values of the expected selector.
if (expected == null) {
this.expected = Collections.emptyList();
} else {
this.expected = new ArrayList<>(expected.size());
for (String validValue : expected) {
this.expected.add(new AttributeValue.Literal(validValue));
}
}
}

static AttributeSelector existence(AttributeValue.Factory key) {
return new AttributeSelector(key, null, null, false);
}

@Override
public Set<Shape> select(Model model, NeighborProvider neighborProvider, Set<Shape> shapes) {
return shapes.stream()
.filter(shape -> matchesAttribute(key.apply(shape)))
.filter(this::matchesAttribute)
.collect(Collectors.toSet());
}

private boolean matchesAttribute(List<String> result) {
if (comparator == null) {
return !result.isEmpty();
}

for (String attribute : result) {
// The returned attribute value might be null if
// the value exists, but isn't comparable.
if (attribute == null) {
continue;
}
private boolean matchesAttribute(Shape shape) {
AttributeValue lhs = key.create(shape);

if (caseInsensitive) {
attribute = attribute.toLowerCase(Locale.ENGLISH);
}
if (expected.isEmpty()) {
return lhs.isPresent();
}

for (String value : expected) {
if (comparator.apply(attribute, value)) {
return true;
}
for (AttributeValue rhs : expected) {
if (comparator.flattenedCompare(lhs, rhs, caseInsensitive)) {
return true;
}
}

return false;
}

// Try to parse both numbers, ignore numeric failures since that's acceptable,
// then pass the result of calling compareTo on the numbers to the given
// evaluator. The evaluator then determines if the comparison is what was expected.
private static boolean numericComparison(String lhs, String rhs, Function<Integer, Boolean> evaluator) {
BigDecimal lhsNumber = parseNumber(lhs);
if (lhsNumber == null) {
return false;
}

BigDecimal rhsNumber = parseNumber(rhs);
if (rhsNumber == null) {
return false;
}

return evaluator.apply(lhsNumber.compareTo(rhsNumber));
}

private static BigDecimal parseNumber(String token) {
try {
return new BigDecimal(token);
} catch (NumberFormatException e) {
return null;
}
}
}
Loading