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

smithy-rules-engine cleanups #1681

Merged
merged 5 commits into from
Apr 6, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public Value visitLibraryFunction(FunctionDefinition definition, List<Expression
}

private Value handleRule(Rule rule) {
RuleEvaluator self = this;
return scope.inScope(() -> {
for (Condition condition : rule.getConditions()) {
Value value = evaluateCondition(condition);
Expand All @@ -143,28 +142,37 @@ public Value visitTreeRule(List<Rule> rules) {

@Override
public Value visitErrorRule(Expression error) {
return error.accept(self);
return RuleEvaluator.this.visitErrorRule(error);
}

@Override
public Value visitEndpointRule(Endpoint endpoint) {
Value.Endpoint.Builder builder = Value.Endpoint.builder()
.sourceLocation(endpoint)
.url(endpoint.getUrl()
.accept(RuleEvaluator.this)
.expectString());
endpoint.getProperties()
.forEach((key, value) -> builder.addProperty(key.toString(),
value.accept(RuleEvaluator.this)));
endpoint.getHeaders()
.forEach((name, expressions) -> expressions.forEach(expr -> builder.addHeader(name,
expr.accept(RuleEvaluator.this).expectString())));
return builder.build();
return RuleEvaluator.this.visitEndpointRule(endpoint);
}
});
});
}

public Value visitErrorRule(Expression error) {
return error.accept(this);
}

public Value visitEndpointRule(Endpoint endpoint) {
Value.Endpoint.Builder builder = Value.Endpoint.builder()
.sourceLocation(endpoint)
.url(endpoint.getUrl()
.accept(RuleEvaluator.this)
.expectString());
endpoint.getProperties()
.forEach((key, value) -> builder.addProperty(key.toString(),
value.accept(RuleEvaluator.this)));
endpoint.getHeaders()
.forEach((name, expressions) -> expressions.forEach(expr -> builder.addHeader(name,
expr.accept(RuleEvaluator.this).expectString())));
return builder.build();

}

public Value evaluateCondition(Condition condition) {
Value value = condition.getFn().accept(this);
if (!value.isNone()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public BooleanEquals(FunctionNode functionNode) {
* @param right the right hand-side expression.
* @return the function defining the BooleanEquals of the left and right expressions.
*/
public static Function ofExpressions(Expression left, Expression right) {
return LibraryFunction.ofExpressions(DEFINITION, left, right);
public static BooleanEquals ofExpressions(Expression left, Expression right) {
return new BooleanEquals(FunctionNode.ofExpressions(DEFINITION.getId(), left, right));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public StringEquals(FunctionNode functionNode) {
* @param right the right expression.
* @return a function instance representing the StringEquals comparison.
*/
public static Function ofExpressions(Expression left, Expression right) {
return LibraryFunction.ofExpressions(DEFINITION, left, right);
public static StringEquals ofExpressions(Expression left, Expression right) {
return new StringEquals(FunctionNode.ofExpressions(DEFINITION.getId(), left, right));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.smithy.rulesengine.traits;

import java.lang.ref.WeakReference;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -25,7 +26,6 @@
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.utils.MapUtils;
import software.amazon.smithy.utils.Pair;
import software.amazon.smithy.utils.SmithyUnstableApi;

Expand Down Expand Up @@ -77,11 +77,14 @@ public Map<MemberShape, ContextParamTrait> getContextParams(Shape operation) {
.orElseThrow(() -> new IllegalArgumentException(operation.toShapeId()
+ " is not an operation shape"));

return getModel().expectShape(operationShape.getInputShape())
LinkedHashMap<MemberShape, ContextParamTrait> out = new LinkedHashMap<>();

getModel().expectShape(operationShape.getInputShape())
.members().stream()
.map(memberShape -> Pair.of(memberShape, memberShape.getTrait(ContextParamTrait.class)))
.filter(pair -> pair.right.isPresent())
.collect(MapUtils.toUnmodifiableMap(pair -> pair.left, pair -> pair.right.get()));
.forEach(pair -> out.put(pair.left, pair.right.get()));
return out;
}

private Model getModel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ public String toString() {
}
if (!properties.isEmpty()) {
sb.append("properties:\n");
properties.forEach((k, v) -> sb.append(StringUtils.indent(String.format("%s: %s", k, v), 2)));
properties.forEach((k, v) -> sb
.append(
StringUtils.indent(
String.format("%s: %s", k, Node.prettyPrintJson(v)), 2)
));
}
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package software.amazon.smithy.rulesengine.language.fn;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.rulesengine.language.syntax.expr.Expression;
import software.amazon.smithy.rulesengine.language.syntax.fn.FunctionDefinition;
import software.amazon.smithy.rulesengine.language.visit.ExpressionVisitor;

public class FunctionOfExprsTest {

@Test
void boolEquals() {
Expression boolEquals = Expression.of(true).equal(true);
assertEquals(boolEquals.accept(new ExpectLibFunction()), false);
assertEquals(boolEquals.accept(new ExpectBuiltInFunction()), "bool");
}


@Test
void stringEquals() {
Expression stringEquals = Expression.of("asdf").equal("asdf");
assertEquals(stringEquals.accept(new ExpectLibFunction()), false);
assertEquals(stringEquals.accept(new ExpectBuiltInFunction()), "string");
}

@Test
void not() {
Expression not = Expression.of("asdf").equal("asdf").not();
assertEquals(not.accept(new ExpectLibFunction()), false);
assertEquals(not.accept(new ExpectBuiltInFunction()), "not");
}

static class ExpectLibFunction extends ExpressionVisitor.Default<Boolean> {
@Override
public Boolean visitLibraryFunction(FunctionDefinition fn, List<Expression> args) {
return true;
}

@Override
public Boolean getDefault() {
return false;
}
}

static class ExpectBuiltInFunction extends ExpressionVisitor.Default<String> {
@Override
public String getDefault() {
return "other";
}

@Override
public String visitBoolEquals(Expression left, Expression right) {
return "bool";
}


@Override
public String visitStringEquals(Expression left, Expression right) {
return "string";
}

@Override
public String visitNot(Expression not) {
return "not";
}
}
}