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

Implement toNode for partition models #1449

Merged
merged 5 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -22,6 +22,7 @@
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.node.ToNode;
import software.amazon.smithy.rulesengine.language.util.SourceLocationTrackingBuilder;
import software.amazon.smithy.utils.BuilderRef;
import software.amazon.smithy.utils.SmithyBuilder;
Expand All @@ -32,7 +33,7 @@
* Describes an AWS partition, it's regions, and the outputs to be provided by the rule-set aws.partition function.
*/
@SmithyUnstableApi
public final class Partition implements ToSmithyBuilder<Partition>, FromSourceLocation {
public final class Partition implements ToSmithyBuilder<Partition>, FromSourceLocation, ToNode {
private static final String ID = "id";
private static final String REGION_REGEX = "regionRegex";
private static final String REGIONS = "regions";
Expand Down Expand Up @@ -123,6 +124,24 @@ public int hashCode() {
return Objects.hash(id, regionRegex, regions, partitionOutputs);
}

@Override
public Node toNode() {
return Node.objectNodeBuilder()
.withMember(ID, Node.from(id))
.withMember(REGION_REGEX, Node.from(regionRegex))
.withMember(REGIONS, regionsNode())
.withMember(OUTPUTS, partitionOutputs.toNode())
.build();
}

private Node regionsNode() {
ObjectNode.Builder on = ObjectNode.builder();
regions.forEach((k, v) ->
on.withMember(k, v.toNode())
);
return on.build();
}

public static class Builder extends SourceLocationTrackingBuilder<Builder, Partition> {
private String id;
private String regionRegex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.node.ToNode;
import software.amazon.smithy.rulesengine.language.util.SourceLocationTrackingBuilder;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.SmithyUnstableApi;
Expand All @@ -30,7 +31,7 @@
* The outputs to be provided by the rule-set aws.partition function.
*/
@SmithyUnstableApi
public final class PartitionOutputs implements ToSmithyBuilder<PartitionOutputs>, FromSourceLocation {
public final class PartitionOutputs implements ToSmithyBuilder<PartitionOutputs>, FromSourceLocation, ToNode {
private static final String DNS_SUFFIX = "dnsSuffix";
private static final String DUAL_STACK_DNS_SUFFIX = "dualStackDnsSuffix";
private static final String SUPPORTS_FIPS = "supportsFIPS";
Expand Down Expand Up @@ -121,6 +122,16 @@ public boolean equals(Object o) {
&& Objects.equals(dualStackDnsSuffix, partitionOutputs.dualStackDnsSuffix);
}

@Override
public Node toNode() {
return Node.objectNodeBuilder()
.withMember(DNS_SUFFIX, dnsSuffix)
.withMember(DUAL_STACK_DNS_SUFFIX, dualStackDnsSuffix)
.withMember(SUPPORTS_FIPS, supportsFips)
.withMember(SUPPORTS_DUAL_STACK, supportsDualStack)
.build();
}

public static class Builder extends SourceLocationTrackingBuilder<Builder, PartitionOutputs> {
private String dnsSuffix;
private String dualStackDnsSuffix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.util.Objects;
import software.amazon.smithy.model.FromSourceLocation;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.ArrayNode;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.node.ToNode;
import software.amazon.smithy.rulesengine.language.util.SourceLocationTrackingBuilder;
import software.amazon.smithy.utils.BuilderRef;
import software.amazon.smithy.utils.SmithyBuilder;
Expand All @@ -32,7 +34,7 @@
* A model for defining the set of partitions that are used by the rule-set aws.partition function.
*/
@SmithyUnstableApi
public final class Partitions implements ToSmithyBuilder<Partitions>, FromSourceLocation {
public final class Partitions implements ToSmithyBuilder<Partitions>, FromSourceLocation, ToNode {
private static final String VERSION = "version";
private static final String PARTITIONS = "partitions";
private final String version;
Expand Down Expand Up @@ -110,6 +112,20 @@ public SmithyBuilder<Partitions> toBuilder() {
.partitions(partitions);
}

@Override
public Node toNode() {
return Node.objectNodeBuilder()
.withMember(VERSION, Node.from(version))
.withMember(PARTITIONS, partitionsNode())
.build();
}

private Node partitionsNode() {
ArrayNode.Builder node = ArrayNode.builder();
partitions.forEach(node::withValue);
return node.build();
}

public static class Builder extends SourceLocationTrackingBuilder<Builder, Partitions> {
private String version;
private final BuilderRef<List<Partition>> partitions = BuilderRef.forList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import software.amazon.smithy.model.FromSourceLocation;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ToNode;
import software.amazon.smithy.rulesengine.language.util.SourceLocationTrackingBuilder;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.SmithyUnstableApi;
Expand All @@ -27,7 +28,7 @@
* Provides a facility for overriding a partition's regions.
*/
@SmithyUnstableApi
public final class RegionOverride implements ToSmithyBuilder<RegionOverride>, FromSourceLocation {
public final class RegionOverride implements ToSmithyBuilder<RegionOverride>, FromSourceLocation, ToNode {
private final SourceLocation sourceLocation;

private RegionOverride(Builder builder) {
Expand Down Expand Up @@ -63,6 +64,11 @@ public boolean equals(Object obj) {
return obj instanceof RegionOverride;
}

@Override
public Node toNode() {
return Node.objectNode();
}

public static class Builder extends SourceLocationTrackingBuilder<Builder, RegionOverride> {

public Builder(FromSourceLocation sourceLocation) {
Expand Down