-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve in-code rule-set composition
This commit adds a `SyntaxElement` abstract class that contains several methods intended for use in constructing rule-sets in code. This class implements two new interfaces (`ToCondition` and `ToExpression`) to support converting between types of syntax elements that compose into rule-sets. The `Condition`, `Parameter`, and `Expression` classes extend this, allowing all types of ruleset syntax to be composed this way.
- Loading branch information
Showing
26 changed files
with
501 additions
and
95 deletions.
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
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
124 changes: 124 additions & 0 deletions
124
...ngine/src/main/java/software/amazon/smithy/rulesengine/language/syntax/SyntaxElement.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,124 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rulesengine.language.syntax; | ||
|
||
import software.amazon.smithy.rulesengine.language.syntax.expressions.Expression; | ||
import software.amazon.smithy.rulesengine.language.syntax.expressions.functions.BooleanEquals; | ||
import software.amazon.smithy.rulesengine.language.syntax.expressions.functions.GetAttr; | ||
import software.amazon.smithy.rulesengine.language.syntax.expressions.functions.IsSet; | ||
import software.amazon.smithy.rulesengine.language.syntax.expressions.functions.IsValidHostLabel; | ||
import software.amazon.smithy.rulesengine.language.syntax.expressions.functions.Not; | ||
import software.amazon.smithy.rulesengine.language.syntax.expressions.functions.ParseUrl; | ||
import software.amazon.smithy.rulesengine.language.syntax.expressions.functions.StringEquals; | ||
import software.amazon.smithy.rulesengine.language.syntax.expressions.functions.Substring; | ||
import software.amazon.smithy.rulesengine.language.syntax.rule.Condition; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* A class that is coercible into {@link Condition}s and {@link Expression}s for | ||
* use in composing rule-sets in code. | ||
*/ | ||
@SmithyInternalApi | ||
public abstract class SyntaxElement implements ToCondition, ToExpression { | ||
/** | ||
* Returns a BooleanEquals expression comparing this expression to the provided boolean value. | ||
* | ||
* @param value the value to compare against. | ||
* @return the {@link BooleanEquals} function. | ||
*/ | ||
public final BooleanEquals booleanEqual(boolean value) { | ||
return BooleanEquals.ofExpressions(toExpression(), Expression.of(value)); | ||
} | ||
|
||
/** | ||
* Returns a StringEquals function of this expression and the given string value. | ||
* | ||
* @param value the string value to compare this expression to. | ||
* @return the {@link StringEquals} function. | ||
*/ | ||
public final StringEquals stringEqual(String value) { | ||
return StringEquals.ofExpressions(toExpression(), Expression.of(value)); | ||
} | ||
|
||
/** | ||
* Returns a GetAttr function containing the given path string. | ||
* | ||
* @param path the path. | ||
* @return the {@link GetAttr} function. | ||
*/ | ||
public final GetAttr getAttr(String path) { | ||
return GetAttr.ofExpressions(toExpression(), path); | ||
} | ||
|
||
/** | ||
* Returns a GetAttr function containing the given identifier. | ||
* | ||
* @param path the path. | ||
* @return the {@link GetAttr} function. | ||
*/ | ||
public final GetAttr getAttr(Identifier path) { | ||
return GetAttr.ofExpressions(toExpression(), path.toString()); | ||
} | ||
|
||
/** | ||
* Returns an IsSet expression for this instance. | ||
* | ||
* @return the {@link IsSet} function. | ||
*/ | ||
public final IsSet isSet() { | ||
return IsSet.ofExpressions(toExpression()); | ||
} | ||
|
||
/** | ||
* Returns an isValidHostLabel expression of this expression. | ||
* | ||
* @param allowDots whether the UTF-8 {@code .} is considered valid within a host label. | ||
* @return the {@link IsValidHostLabel} function. | ||
*/ | ||
public final IsValidHostLabel isValidHostLabel(boolean allowDots) { | ||
return IsValidHostLabel.ofExpressions(toExpression(), allowDots); | ||
} | ||
|
||
/** | ||
* Returns a Not expression of this instance. | ||
* | ||
* @return the {@link Not} function. | ||
*/ | ||
public final Not not() { | ||
return Not.ofExpressions(toExpression()); | ||
} | ||
|
||
/** | ||
* Returns a parseUrl expression of this expression. | ||
* | ||
* @return the {@link ParseUrl} function. | ||
*/ | ||
public final ParseUrl parseUrl() { | ||
return ParseUrl.ofExpressions(toExpression()); | ||
} | ||
|
||
/** | ||
* Returns a Substring expression of this expression. | ||
* | ||
* @param startIndex the starting index of the string. | ||
* @param stopIndex the ending index of the string. | ||
* @param reverse whether the indexing is should start from end of the string to start. | ||
* @return the {@link Substring} function. | ||
*/ | ||
public final Substring substring(int startIndex, int stopIndex, boolean reverse) { | ||
return Substring.ofExpressions(toExpression(), startIndex, stopIndex, reverse); | ||
} | ||
|
||
/** | ||
* Converts this expression to a string template. | ||
* By default, this implementation returns a {@link RuntimeException}. | ||
* | ||
* @return the String template. | ||
*/ | ||
public String template() { | ||
throw new RuntimeException(String.format("cannot convert %s to a string template", this)); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...-engine/src/main/java/software/amazon/smithy/rulesengine/language/syntax/ToCondition.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,42 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rulesengine.language.syntax; | ||
|
||
import software.amazon.smithy.rulesengine.language.syntax.rule.Condition; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* Supplies functionality to be coercible into {@link Condition}s for | ||
* use in composing rule-sets in code. | ||
*/ | ||
@SmithyInternalApi | ||
public interface ToCondition { | ||
/** | ||
* Convert this into a condition builder for compositional use. | ||
* | ||
* @return the condition builder. | ||
*/ | ||
Condition.Builder toConditionBuilder(); | ||
|
||
/** | ||
* Convert this into a condition. | ||
* | ||
* @return the condition. | ||
*/ | ||
default Condition toCondition() { | ||
return toConditionBuilder().build(); | ||
} | ||
|
||
/** | ||
* Converts this function into a condition which stores the output in the named result. | ||
* | ||
* @param result the name of the result parameter. | ||
* @return the function as a condition. | ||
*/ | ||
default Condition toCondition(String result) { | ||
return toConditionBuilder().result(result).build(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...engine/src/main/java/software/amazon/smithy/rulesengine/language/syntax/ToExpression.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,23 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rulesengine.language.syntax; | ||
|
||
import software.amazon.smithy.rulesengine.language.syntax.expressions.Expression; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* Supplies functionality to be coercible into {@link Expression}s for | ||
* use in composing rule-sets in code. | ||
*/ | ||
@SmithyInternalApi | ||
public interface ToExpression { | ||
/** | ||
* Convert this into an expression. | ||
* | ||
* @return the expression. | ||
*/ | ||
Expression toExpression(); | ||
} |
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
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
Oops, something went wrong.