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

Filter out synthetic traits from build info #1374

Merged
merged 1 commit into from
Aug 29, 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 @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import software.amazon.smithy.build.PluginContext;
import software.amazon.smithy.build.SmithyBuildPlugin;
import software.amazon.smithy.model.Model;
Expand All @@ -43,6 +44,7 @@
* configured on the provided PluginContext.
*/
public final class BuildInfoPlugin implements SmithyBuildPlugin {
public static final String BUILD_INFO_PATH = "smithy-build-info.json";
private static final String NAME = "build-info";

@Override
Expand All @@ -58,7 +60,7 @@ public boolean requiresValidModel() {
@Override
public void execute(PluginContext context) {
if (context.getOriginalModel().isPresent() && context.getProjection().isPresent()) {
context.getFileManifest().writeJson("smithy-build-info.json", serializeBuildInfo(context));
context.getFileManifest().writeJson(BUILD_INFO_PATH, serializeBuildInfo(context));
}
}

Expand All @@ -67,8 +69,9 @@ private static Node serializeBuildInfo(PluginContext context) {
info.setProjectionName(context.getProjectionName());
info.setProjection(context.getProjection().orElse(null));
info.setValidationEvents(context.getEvents());
info.setTraitNames(findTraitNames(context.getModel()));
info.setTraitDefNames(getTraitShapeIds(context.getModel()));
Set<ShapeId> traitIds = getTraitShapeIds(context.getModel());
info.setTraitNames(findTraitNames(context.getModel(), traitIds));
info.setTraitDefNames(new ArrayList<>(traitIds));
info.setServiceShapeIds(findShapeIds(context.getModel(), ServiceShape.class));
info.setOperationShapeIds(findShapeIds(context.getModel(), OperationShape.class));
info.setResourceShapeIds(findShapeIds(context.getModel(), ResourceShape.class));
Expand All @@ -77,20 +80,31 @@ private static Node serializeBuildInfo(PluginContext context) {
return mapper.serialize(info);
}

private static List<ShapeId> getTraitShapeIds(Model model) {
private static Set<ShapeId> getTraitShapeIds(Model model) {
// Will only get traits that are defined in the model (no synthetic traits)
Set<Shape> traits = model.getShapesWithTrait(TraitDefinition.class);
List<ShapeId> result = new ArrayList<>(traits.size());

Set<ShapeId> result = new TreeSet<>();
for (Shape traitShape : traits) {
result.add(traitShape.getId());
}
Collections.sort(result);
syall marked this conversation as resolved.
Show resolved Hide resolved

return result;
}

private static List<ShapeId> findTraitNames(Model model) {
private static List<ShapeId> findTraitNames(Model model, Set<ShapeId> traitIds) {

List<ShapeId> applied = new ArrayList<>(model.getAppliedTraits());
Collections.sort(applied);
return applied;

List<ShapeId> traitNames = new ArrayList<>();
for (ShapeId shapeId : applied) {
if (traitIds.contains(shapeId)) {
traitNames.add(shapeId);
}
}
Collections.sort(traitNames);

return traitNames;
}

private static <T extends Shape> List<ShapeId> findShapeIds(Model model, Class<T> clazz) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package software.amazon.smithy.build.plugins;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;

import java.net.URISyntaxException;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.build.MockManifest;
import software.amazon.smithy.build.PluginContext;
import software.amazon.smithy.build.model.ProjectionConfig;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.utils.ListUtils;

public class BuildInfoPluginTest {

@Test
public void testFiltersSyntheticEnumTrait() throws URISyntaxException {

syall marked this conversation as resolved.
Show resolved Hide resolved
String testPath = "build-info/example.smithy";

Model model = Model.assembler()
.addImport(getClass().getResource(testPath))
.assemble()
.unwrap();
MockManifest manifest = new MockManifest();
PluginContext context = PluginContext.builder()
.fileManifest(manifest)
.model(model)
.originalModel(model)
.projection("TEST", ProjectionConfig.builder().build())
.sources(ListUtils.of(Paths.get(getClass().getResource(testPath).toURI()).getParent()))
.build();
new BuildInfoPlugin().execute(context);

String buildInfo = manifest.getFileString(BuildInfoPlugin.BUILD_INFO_PATH).get();

assertThat(buildInfo, not(containsString("smithy.synthetic#enum")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
$version: "2.0"

namespace smithy.example

service Example {
version: "1.0.0"
operations: [
ChangeCard
]
}

operation ChangeCard {
input: Card
output: Card
}

structure Card {
suit: Suit
}

enum Suit {
DIAMOND
CLUB
HEART
SPADE
}