Skip to content

Commit

Permalink
Add option to enable prelude serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
srchase authored and Michael Dowling committed Jun 15, 2022
1 parent 5733242 commit 7c07be0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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"));
}
}

0 comments on commit 7c07be0

Please sign in to comment.