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

[0.10] Support Smithy IDL Serialization #284

Merged
merged 16 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add a base path to the idl serializer
  • Loading branch information
JordonPhillips committed Feb 27, 2020
commit 92a4eaa1a1cb59e08b17059e32e5a659715ef95a
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,19 @@ public final class SmithyIdlModelSerializer {
private final Predicate<Shape> shapeFilter;
private final Predicate<Trait> traitFilter;
private final Function<Shape, Path> shapePlacer;
private final Path basePath;

private SmithyIdlModelSerializer(Builder builder) {
JordonPhillips marked this conversation as resolved.
Show resolved Hide resolved
metadataFilter = builder.metadataFilter;
shapeFilter = builder.shapeFilter.and(FunctionalUtils.not(Prelude::isPreludeShape));
traitFilter = builder.traitFilter;
shapePlacer = builder.shapePlacer;
basePath = builder.basePath;
if (basePath != null) {
Function<Shape, Path> shapePlacer = builder.shapePlacer;
this.shapePlacer = shape -> this.basePath.resolve(shapePlacer.apply(shape));
} else {
this.shapePlacer = builder.shapePlacer;
}
}

/**
Expand All @@ -89,7 +96,11 @@ public Map<Path, String> serialize(Model model) {
.collect(Collectors.groupingBy(shapePlacer)).entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> serialize(model, entry.getValue())));
if (result.isEmpty()) {
return Collections.singletonMap(Paths.get("metadata.smithy"), serializeHeader(model, null));
Path path = Paths.get("metadata.smithy");
if (basePath != null) {
path = basePath.resolve(path);
}
return Collections.singletonMap(path, serializeHeader(model, null));
}
return result;
}
Expand Down Expand Up @@ -219,6 +230,7 @@ public static final class Builder implements SmithyBuilder<SmithyIdlModelSeriali
private Predicate<Shape> shapeFilter = shape -> true;
private Predicate<Trait> traitFilter = trait -> true;
private Function<Shape, Path> shapePlacer = SmithyIdlModelSerializer::placeShapesByNamespace;
private Path basePath = null;

public Builder() {}

Expand Down Expand Up @@ -274,6 +286,17 @@ public Builder shapePlacer(Function<Shape, Path> shapePlacer) {
return this;
}

/**
* A base path to use for any created models.
*
* @param basePath The base directory to assign models to.
* @return Returns the builder.
*/
public Builder basePath(Path basePath) {
this.basePath = basePath;
return this;
}

@Override
public SmithyIdlModelSerializer build() {
return new SmithyIdlModelSerializer(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package software.amazon.smithy.model.shapes;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.aMapWithSize;
Expand All @@ -17,6 +18,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.StringNode;
import software.amazon.smithy.model.traits.DocumentationTrait;
import software.amazon.smithy.model.traits.RequiredTrait;
import software.amazon.smithy.utils.IoUtils;
Expand Down Expand Up @@ -48,14 +50,12 @@ public void multipleNamespacesGenerateMultipleFiles() {
.addImport(getClass().getResource("idl-serialization/multiple-namespaces/input.json"))
.assemble()
.unwrap();
SmithyIdlModelSerializer serializer = SmithyIdlModelSerializer.builder().build();
Map<Path, String> serialized = serializer.serialize(model);

Path outputDir = Paths.get(getClass().getResource("idl-serialization/multiple-namespaces/output").getFile());
serialized.forEach((path, generated) -> {
Path expectedPath = outputDir.resolve(path);
assertThat(generated, equalTo(IoUtils.readUtf8File(expectedPath)));
});
SmithyIdlModelSerializer serializer = SmithyIdlModelSerializer.builder()
.basePath(outputDir)
.build();
Map<Path, String> serialized = serializer.serialize(model);
serialized.forEach((path, generated) -> assertThat(generated, equalTo(IoUtils.readUtf8File(path))));
}

@Test
Expand Down Expand Up @@ -119,4 +119,18 @@ public void filtersDocumentationTrait() {
assertThat(output, not(containsString("/// ")));
}
}

@Test
public void basePathAppliesToMetadataOnlyModel() {
Path basePath = Paths.get("/tmp/smithytest");
Model model = Model.assembler()
.putMetadata("foo", StringNode.from("bar"))
.assemble()
.unwrap();
SmithyIdlModelSerializer serializer = SmithyIdlModelSerializer.builder()
.basePath(basePath)
.build();
Map<Path, String> serialized = serializer.serialize(model);
assertThat(serialized.keySet(), contains(basePath.resolve("metadata.smithy")));
}
}