Skip to content

Commit

Permalink
Enable RuleSet and Test validation by default
Browse files Browse the repository at this point in the history
This commit removes the custom validation system that required code
to validate a ruleset and its tests, integrating the validation with
Smithy's built in ModelValidation system. Several bugs in validators
and test cases were fixed. Test cases were moved to using the standard
errorfiles based setup where applicable.

Some related refactoring was also performed.
  • Loading branch information
kstich committed Jun 14, 2023
1 parent bc01b57 commit d062610
Show file tree
Hide file tree
Showing 124 changed files with 4,643 additions and 2,870 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private Endpoint(Builder builder) {
List<Literal> authSchemes = new ArrayList<>();
for (Pair<Identifier, Map<Identifier, Literal>> authScheme : builder.authSchemes.get()) {
Map<Identifier, Literal> base = new TreeMap<>(Comparator.comparing(Identifier::toString));
base.put(Identifier.of("name"), Literal.of(authScheme.left.asString()));
base.put(Identifier.of("name"), Literal.of(authScheme.left.toString()));
base.putAll(authScheme.right);
authSchemes.add(Literal.recordLiteral(base));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public boolean isEmpty() {
public Node toNode() {
return Node.nullNode();
}

@Override
public String toString() {
return "<empty>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ public String toString() {
return name.getValue();
}

public String asString() {
return name.getValue();
}

@Override
public Node toNode() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public GetAttr getAttr(String path) {
public GetAttr getAttr(Identifier path) {
return GetAttr.builder()
.sourceLocation(this)
.target(this).path(path.asString()).build();
.target(this).path(path.toString()).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ public Identifier getName() {

@Override
public String toString() {
return name.asString();
return name.toString();
}

@Override
public Node toNode() {
return ObjectNode.builder()
.withMember("ref", name.asString())
.withMember("ref", name.toString())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
public final class GetAttr extends Expression {
public static final String ID = "getAttr";
private final Expression target;
private String unparsedPath;
private final String unparsedPath;
private final List<Part> path;

private GetAttr(Builder builder) {
Expand Down Expand Up @@ -146,7 +146,7 @@ public Type typeCheckLocal(Scope<Type> scope) {

@Override
public String getTemplate() {
String target = ((Reference) this.getTarget()).getName().asString();
String target = ((Reference) this.getTarget()).getName().toString();
return "{" + target + "#" + unparsedPath + "}";
}

Expand Down Expand Up @@ -247,7 +247,7 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return key.asString();
return key.toString();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@

package software.amazon.smithy.rulesengine.language.syntax.parameters;

import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;
import software.amazon.smithy.rulesengine.language.eval.value.Value;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.MapUtils;
import software.amazon.smithy.utils.SmithyUnstableApi;

/**
* Built-in parameters for EndpointRules.
*/
@SmithyUnstableApi
public final class Builtins {
public final class BuiltIns {
/**
* Built-in parameter representing Region eg. `us-east-1`.
*/
Expand Down Expand Up @@ -70,10 +71,10 @@ public final class Builtins {
*/
public static final Parameter SDK_ENDPOINT =
Parameter.builder()
.name("EndpointType")
.name("Endpoint")
.type(ParameterType.STRING)
.documentation("Override the endpoint used to send this request")
.builtIn("SDK::EndpointType")
.builtIn("SDK::Endpoint")
.build();

/**
Expand Down Expand Up @@ -171,9 +172,23 @@ public final class Builtins {
+ "the regional endpoint for us-east-1.")
.build();

public static final List<Parameter> ALL_BUILTINS = ListUtils.of(
SDK_ENDPOINT, REGION, FIPS, DUALSTACK, S3_ACCELERATE, S3_FORCE_PATH_STYLE, S3_USE_ARN_REGION,
S3_USE_GLOBAL_ENDPOINT, S3_CONTROL_USE_ARN_REGION, STS_USE_GLOBAL_ENDPOINT, S3_DISABLE_MRAP);
public static final Map<String, Parameter> ALL_BUILTINS;

private Builtins() {}
static {
Map<String, Parameter> tempMap = new LinkedHashMap<>();
tempMap.put(SDK_ENDPOINT.getBuiltIn().get(), SDK_ENDPOINT);
tempMap.put(REGION.getBuiltIn().get(), REGION);
tempMap.put(FIPS.getBuiltIn().get(), FIPS);
tempMap.put(DUALSTACK.getBuiltIn().get(), DUALSTACK);
tempMap.put(S3_ACCELERATE.getBuiltIn().get(), S3_ACCELERATE);
tempMap.put(S3_FORCE_PATH_STYLE.getBuiltIn().get(), S3_FORCE_PATH_STYLE);
tempMap.put(S3_USE_ARN_REGION.getBuiltIn().get(), S3_USE_ARN_REGION);
tempMap.put(S3_USE_GLOBAL_ENDPOINT.getBuiltIn().get(), S3_USE_GLOBAL_ENDPOINT);
tempMap.put(S3_CONTROL_USE_ARN_REGION.getBuiltIn().get(), S3_CONTROL_USE_ARN_REGION);
tempMap.put(STS_USE_GLOBAL_ENDPOINT.getBuiltIn().get(), STS_USE_GLOBAL_ENDPOINT);
tempMap.put(S3_DISABLE_MRAP.getBuiltIn().get(), S3_DISABLE_MRAP);
ALL_BUILTINS = MapUtils.copyOf(tempMap);
}

private BuiltIns() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
package software.amazon.smithy.rulesengine.language.syntax.parameters;

import software.amazon.smithy.model.SourceException;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.StringNode;
import software.amazon.smithy.model.shapes.ShapeType;
import software.amazon.smithy.rulesengine.language.error.RuleError;
import software.amazon.smithy.rulesengine.language.eval.type.BooleanType;
import software.amazon.smithy.rulesengine.language.eval.type.StringType;
import software.amazon.smithy.rulesengine.language.eval.type.Type;
import software.amazon.smithy.utils.SmithyUnstableApi;

@SmithyUnstableApi
Expand All @@ -35,6 +40,36 @@ public static ParameterType fromNode(StringNode node) throws RuleError {
String.format("Unexpected parameter type `%s`. Expected `String` or `Boolean`.", value), node));
}

public static ParameterType fromNode(Node node) throws RuleError {
if (node.isStringNode()) {
return STRING;
} else if (node.isBooleanNode()) {
return BOOLEAN;
}
throw new RuleError(new SourceException(
String.format("Unexpected parameter type `%s`. Expected a string or boolean.", node.getType()), node));
}

public static ParameterType fromType(Type type) {
if (type instanceof StringType) {
return STRING;
} else if (type instanceof BooleanType) {
return BOOLEAN;
}
throw new RuntimeException(
String.format("Unexpected parameter type `%s`. Expected a string or boolean.", type));
}

public static ParameterType fromShapeType(ShapeType type) {
if (type == ShapeType.STRING) {
return STRING;
} else if (type == ShapeType.BOOLEAN) {
return BOOLEAN;
}
throw new RuntimeException(
String.format("Unexpected parameter type `%s`. Expected string or boolean.", type));
}

@Override
public String toString() {
switch (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ private Builder(FromSourceLocation sourceLocation) {
this.sourceLocation = sourceLocation.getSourceLocation();
}

@SafeVarargs
public final Builder conditions(Condition... conditions) {
public Builder conditions(Condition... conditions) {
for (Condition condition : conditions) {
condition(condition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public final class TreeRule extends Rule {
private final List<Rule> rules;

protected TreeRule(Builder builder, List<Rule> rules) {
TreeRule(Builder builder, List<Rule> rules) {
super(builder);
this.rules = rules;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import software.amazon.smithy.model.traits.AbstractTrait;
import software.amazon.smithy.model.traits.AbstractTraitBuilder;
import software.amazon.smithy.model.traits.Trait;
import software.amazon.smithy.rulesengine.language.EndpointRuleSet;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.SmithyUnstableApi;
import software.amazon.smithy.utils.ToSmithyBuilder;
Expand All @@ -32,10 +33,12 @@ public final class EndpointRuleSetTrait extends AbstractTrait implements ToSmith
public static final ShapeId ID = ShapeId.from("smithy.rules#endpointRuleSet");

private final Node ruleSet;
private final EndpointRuleSet endpointRuleSet;

private EndpointRuleSetTrait(Builder builder) {
super(ID, builder.getSourceLocation());
this.ruleSet = SmithyBuilder.requiredState("ruleSet", builder.ruleSet);
this.endpointRuleSet = EndpointRuleSet.fromNode(ruleSet);
}

public static Builder builder() {
Expand All @@ -46,6 +49,10 @@ public Node getRuleSet() {
return ruleSet;
}

public EndpointRuleSet getEndpointRuleSet() {
return endpointRuleSet;
}

@Override
protected Node createNode() {
return ruleSet;
Expand Down
Loading

0 comments on commit d062610

Please sign in to comment.