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

Add option to enable prelude serialization #1275

Merged
merged 1 commit into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -64,7 +64,13 @@ public final class SmithyIdlModelSerializer {

private SmithyIdlModelSerializer(Builder builder) {
metadataFilter = builder.metadataFilter;
shapeFilter = builder.shapeFilter.and(FunctionalUtils.not(Prelude::isPreludeShape));
// If prelude serializing has been enabled, only use the given shape filter.
if (builder.serializePrelude) {
shapeFilter = builder.shapeFilter;
// Default to using the given shape filter and filtering prelude shapes.
} else {
shapeFilter = builder.shapeFilter.and(FunctionalUtils.not(Prelude::isPreludeShape));
}
// Never serialize synthetic traits.
traitFilter = builder.traitFilter.and(FunctionalUtils.not(Trait::isSynthetic));
basePath = builder.basePath;
Expand Down Expand Up @@ -236,6 +242,7 @@ public static final class Builder implements SmithyBuilder<SmithyIdlModelSeriali
private Predicate<Trait> traitFilter = FunctionalUtils.alwaysTrue();
private Function<Shape, Path> shapePlacer = SmithyIdlModelSerializer::placeShapesByNamespace;
private Path basePath = null;
private boolean serializePrelude = false;

public Builder() {}

Expand Down Expand Up @@ -302,6 +309,16 @@ public Builder basePath(Path basePath) {
return this;
}

/**
* Enables serializing shapes in the Smithy prelude.
* Defaults to false.
* @return Returns the builder.
*/
public Builder serializePrelude() {
this.serializePrelude = true;
return this;
}

@Override
public SmithyIdlModelSerializer build() {
return new SmithyIdlModelSerializer(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void filtersShapes() {
assertThat(serialized, hasKey(Paths.get("ns.structures.smithy")));
assertThat(serialized.get(Paths.get("ns.structures.smithy")),
containsString("namespace ns.structures"));
assertThat(serialized, not(hasKey(Paths.get("smithy.api.smithy"))));
}

@Test
Expand Down Expand Up @@ -168,4 +169,17 @@ public void transientTraitsAreNotSerialized() {
assertThat(results.get(Paths.get("com.foo.smithy")),
not(containsString(OriginalShapeIdTrait.ID.toString())));
}

@Test
public void canEnableSerializingPrelude() {
Model model = Model.assembler()
.addImport(getClass().getResource("idl-serialization/test-model.json"))
.assemble()
.unwrap();
SmithyIdlModelSerializer serializer = SmithyIdlModelSerializer.builder()
.serializePrelude()
.build();
Map<Path, String> serialized = serializer.serialize(model);
assertThat(serialized.get(Paths.get("smithy.api.smithy")), containsString("namespace smithy.api"));
}
}