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 toBuilder to PluginContext #181

Merged
merged 1 commit into from
Oct 7, 2019
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 @@ -34,11 +34,12 @@
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.utils.SetUtils;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.ToSmithyBuilder;

/**
* Context object used in plugin execution.
*/
public final class PluginContext {
public final class PluginContext implements ToSmithyBuilder<PluginContext> {
private final ProjectionConfig projection;
private final String projectionName;
private final Model model;
Expand Down Expand Up @@ -239,6 +240,19 @@ private int findOffsetFromStart(String location) {
return position == -1 ? 0 : position + "file:".length();
}

@Override
public Builder toBuilder() {
return builder()
.projection(projectionName, projection)
.model(model)
.originalModel(originalModel)
.events(events)
.settings(settings)
.fileManifest(fileManifest)
.pluginClassLoader(pluginClassLoader)
.sources(sources);
}

/**
* Builds a {@link PluginContext}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.build.model.ProjectionConfig;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.shapes.ShapeIndex;
import software.amazon.smithy.model.transform.ModelTransformer;
import software.amazon.smithy.utils.ListUtils;
Expand Down Expand Up @@ -61,4 +63,24 @@ public void createsNonTraitShapeIndex() {
assertThat(context.getNonTraitShapes(), equalTo(scrubbed));
assertThat(context.getNonTraitShapes(), equalTo(scrubbed)); // trigger loading from cache
}

@Test
public void convertsToBuilder() {
PluginContext context = PluginContext.builder()
.projection("foo", ProjectionConfig.builder().build())
.fileManifest(new MockManifest())
.model(Model.builder().build())
.originalModel(Model.builder().build())
.settings(Node.objectNode().withMember("foo", "bar"))
.build();
PluginContext context2 = context.toBuilder().build();

assertThat(context.getProjectionName(), equalTo(context2.getProjectionName()));
assertThat(context.getProjection(), equalTo(context2.getProjection()));
assertThat(context.getModel(), equalTo(context2.getModel()));
assertThat(context.getOriginalModel(), equalTo(context2.getOriginalModel()));
assertThat(context.getFileManifest(), is(context2.getFileManifest()));
assertThat(context.getSources(), equalTo(context2.getSources()));
assertThat(context.getEvents(), equalTo(context2.getEvents()));
}
}