Skip to content

Commit

Permalink
Merge pull request #15 from skmcgrail/rules-refactors
Browse files Browse the repository at this point in the history
Rules refactors
  • Loading branch information
rcoh authored Sep 9, 2022
2 parents 9e66491 + 673ac7f commit 53b0e80
Show file tree
Hide file tree
Showing 92 changed files with 1,306 additions and 1,188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import software.amazon.smithy.rulesengine.language.EndpointRuleset;
import software.amazon.smithy.rulesengine.language.eval.RuleEvaluator;
import software.amazon.smithy.rulesengine.language.eval.Value;
import software.amazon.smithy.rulesengine.language.lang.Identifier;
import software.amazon.smithy.rulesengine.language.lang.rule.Condition;
import software.amazon.smithy.rulesengine.language.lang.rule.Rule;
import software.amazon.smithy.rulesengine.language.syntax.Identifier;
import software.amazon.smithy.rulesengine.language.syntax.rule.Condition;
import software.amazon.smithy.rulesengine.language.syntax.rule.Rule;
import software.amazon.smithy.rulesengine.language.util.PathFinder;
import software.amazon.smithy.rulesengine.language.util.StringUtils;
import software.amazon.smithy.rulesengine.language.visit.TraversingVisitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
import software.amazon.smithy.rulesengine.language.eval.Scope;
import software.amazon.smithy.rulesengine.language.eval.Type;
import software.amazon.smithy.rulesengine.language.eval.Typecheck;
import software.amazon.smithy.rulesengine.language.lang.Identifier;
import software.amazon.smithy.rulesengine.language.lang.expr.Expr;
import software.amazon.smithy.rulesengine.language.lang.expr.Literal;
import software.amazon.smithy.rulesengine.language.syntax.Identifier;
import software.amazon.smithy.rulesengine.language.syntax.expr.Expr;
import software.amazon.smithy.rulesengine.language.syntax.expr.Literal;
import software.amazon.smithy.rulesengine.language.util.MandatorySourceLocation;
import software.amazon.smithy.rulesengine.language.util.SourceLocationTrackingBuilder;
import software.amazon.smithy.rulesengine.language.util.StringUtils;
import software.amazon.smithy.utils.BuilderRef;
import software.amazon.smithy.utils.MapUtils;
Expand All @@ -48,7 +50,7 @@
* An Endpoint as returned by EndpointRules.
*/
@SmithyUnstableApi
public final class Endpoint implements ToSmithyBuilder<Endpoint>, FromSourceLocation, Typecheck, ToNode {
public final class Endpoint extends MandatorySourceLocation implements ToSmithyBuilder<Endpoint>, Typecheck, ToNode {
private static final String URL = "url";
private static final String PROPERTIES = "properties";
private static final String HEADERS = "headers";
Expand All @@ -59,9 +61,9 @@ public final class Endpoint implements ToSmithyBuilder<Endpoint>, FromSourceLoca
private final Expr url;
private final Map<String, List<Expr>> headers;
private final Map<Identifier, Literal> properties;
private final SourceLocation sourceLocation;

private Endpoint(Builder builder) {
super(builder.getSourceLocation());
this.url = SmithyBuilder.requiredState("url", builder.url);
Map<Identifier, Literal> properties = new LinkedHashMap<>(builder.properties.copy());
List<Literal> authSchemes =
Expand All @@ -80,7 +82,6 @@ private Endpoint(Builder builder) {

this.properties = properties;
this.headers = builder.headers.copy();
this.sourceLocation = SmithyBuilder.requiredState("sourceLocation", builder.getSourceLocation());
}

public static Endpoint fromNode(Node node) {
Expand Down Expand Up @@ -125,18 +126,13 @@ public static Builder builder() {
return new Builder(SourceLocation.none());
}

@Override
public SourceLocation getSourceLocation() {
return sourceLocation;
}

public Expr getUrl() {
return url;
}

@Override
public Builder toBuilder() {
return builder(this.sourceLocation).url(url).properties(properties);
return builder(this.getSourceLocation()).url(url).properties(properties);
}

@Override
Expand Down Expand Up @@ -233,7 +229,7 @@ public Map<String, List<Expr>> getHeaders() {
/**
* Builder for {@link Endpoint}.
*/
public static class Builder extends SourceAwareBuilder<Builder, Endpoint> {
public static class Builder extends SourceLocationTrackingBuilder<Builder, Endpoint> {
private static final String SIGNING_NAME = "signingName";
private static final String SIGNING_REGION_SET = "signingRegionSet";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
import software.amazon.smithy.rulesengine.language.eval.Scope;
import software.amazon.smithy.rulesengine.language.eval.Type;
import software.amazon.smithy.rulesengine.language.eval.Typecheck;
import software.amazon.smithy.rulesengine.language.lang.parameters.Parameters;
import software.amazon.smithy.rulesengine.language.lang.rule.EndpointRule;
import software.amazon.smithy.rulesengine.language.lang.rule.Rule;
import software.amazon.smithy.rulesengine.language.syntax.parameters.Parameters;
import software.amazon.smithy.rulesengine.language.syntax.rule.EndpointRule;
import software.amazon.smithy.rulesengine.language.syntax.rule.Rule;
import software.amazon.smithy.rulesengine.language.util.MandatorySourceLocation;
import software.amazon.smithy.rulesengine.language.util.SourceLocationTrackingBuilder;
import software.amazon.smithy.utils.BuilderRef;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.SmithyUnstableApi;
Expand All @@ -43,7 +45,7 @@
* A set of EndpointRules. Endpoint Rules describe the endpoint resolution behavior for a service.
*/
@SmithyUnstableApi
public final class EndpointRuleset implements FromSourceLocation, Typecheck, ToNode {
public final class EndpointRuleset extends MandatorySourceLocation implements Typecheck, ToNode {
private static final String LATEST_VERSION = "1.3";
private static final String VERSION = "version";
private static final String PARAMETERS = "parameters";
Expand All @@ -53,13 +55,11 @@ public final class EndpointRuleset implements FromSourceLocation, Typecheck, ToN
private final Parameters parameters;
private final String version;

private final SourceLocation sourceLocation;

private EndpointRuleset(Builder builder) {
super(builder.getSourceLocation());
rules = builder.rules.copy();
parameters = SmithyBuilder.requiredState("parameters", builder.parameters);
version = SmithyBuilder.requiredState("version", builder.version);
sourceLocation = builder.getSourceLocation();
}

public static EndpointRuleset fromNode(Node node) throws RuleError {
Expand Down Expand Up @@ -91,11 +91,6 @@ public List<Rule> getRules() {
return rules;
}

@Override
public SourceLocation getSourceLocation() {
return sourceLocation;
}

@Override
public Type typecheck(Scope<Type> scope) {
return scope.inScope(() -> {
Expand All @@ -122,7 +117,7 @@ public Node toNode() {

public Builder toBuilder() {
return builder()
.sourceLocation(sourceLocation)
.sourceLocation(getSourceLocation())
.parameters(parameters)
.rules(getRules());
}
Expand Down Expand Up @@ -160,7 +155,7 @@ public String toString() {
return builder.toString();
}

public static class Builder extends SourceAwareBuilder<Builder, EndpointRuleset> {
public static class Builder extends SourceLocationTrackingBuilder<Builder, EndpointRuleset> {
private final BuilderRef<List<Rule>> rules = BuilderRef.forList();
private Parameters parameters;
// default the version to the latest.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2022 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.rulesengine.language.error;

import software.amazon.smithy.model.FromSourceLocation;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.rulesengine.language.util.SourceLocationHelpers;
import software.amazon.smithy.utils.SmithyUnstableApi;

/**
* Exception thrown when the ruleset is invalid.
*/
@SmithyUnstableApi
public final class InvalidRulesException extends RuntimeException implements FromSourceLocation {
private final transient SourceLocation sourceLocation;

public InvalidRulesException(String message, FromSourceLocation location) {
super(createMessage(message, location.getSourceLocation()));
sourceLocation = location.getSourceLocation();
}

@Override
public SourceLocation getSourceLocation() {
return sourceLocation;
}

private static String createMessage(String message, SourceLocation sourceLocation) {
if (sourceLocation == SourceLocation.NONE) {
return message;
} else {
String prettyLocation = SourceLocationHelpers.stackTraceForm(sourceLocation);
return message.contains(prettyLocation) ? message : message + " (" + prettyLocation + ")";
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import java.util.Map;
import software.amazon.smithy.rulesengine.language.EndpointRuleset;
import software.amazon.smithy.rulesengine.language.lang.Identifier;
import software.amazon.smithy.rulesengine.language.syntax.Identifier;
import software.amazon.smithy.utils.SmithyUnstableApi;

@SmithyUnstableApi
Expand Down
Loading

0 comments on commit 53b0e80

Please sign in to comment.