Skip to content

Commit

Permalink
Remove model inference
Browse files Browse the repository at this point in the history
This removes the ability to infer which service should be generated
for in a model, instead relying on the setting being explicit.
  • Loading branch information
JordonPhillips committed Mar 17, 2020
1 parent f557fdf commit 5c8d7bb
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class CodegenVisitor extends ShapeVisitor.Default<Void> {
private final GoDelegator writers;

CodegenVisitor(PluginContext context) {
settings = GoSettings.from(context.getModel(), context.getSettings());
settings = GoSettings.from(context.getSettings());
model = context.getModel();
modelWithoutTraitShapes = context.getModelWithoutTraitShapes();
service = settings.getService(model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,18 @@
package software.amazon.smithy.go.codegen;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.node.StringNode;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;

/**
* Settings used by {@link GoCodegenPlugin}.
*/
public final class GoSettings {

private static final Logger LOGGER = Logger.getLogger(GoSettings.class.getName());

private static final String SERVICE = "service";
private static final String MODULE_NAME = "module";
private static final String MODULE_DESCRIPTION = "moduleDescription";
Expand All @@ -48,19 +41,14 @@ public final class GoSettings {
/**
* Create a settings object from a configuration object node.
*
* @param model Model to infer the service to generate if not explicitly provided.
* @param config Config object to load.
* @return Returns the extracted settings.
*/
public static GoSettings from(Model model, ObjectNode config) {
public static GoSettings from(ObjectNode config) {
GoSettings settings = new GoSettings();
config.warnIfAdditionalProperties(Arrays.asList(SERVICE, MODULE_NAME, MODULE_DESCRIPTION, MODULE_VERSION));

// Get the service from the settings or infer one from the given model.
settings.setService(config.getStringMember(SERVICE)
.map(StringNode::expectShapeId)
.orElseGet(() -> inferService(model)));

settings.setService(config.expectStringMember(SERVICE).expectShapeId());
settings.setModuleName(config.expectStringMember(MODULE_NAME).getValue());
settings.setModuleVersion(config.expectStringMember(MODULE_VERSION).getValue());
settings.setModuleDescription(config.getStringMemberOrDefault(
Expand All @@ -69,25 +57,6 @@ public static GoSettings from(Model model, ObjectNode config) {
return settings;
}

private static ShapeId inferService(Model model) {
List<ShapeId> services = model
.shapes(ServiceShape.class)
.map(Shape::getId)
.sorted()
.collect(Collectors.toList());

if (services.isEmpty()) {
throw new CodegenException("Cannot infer a service to generate because the model does not "
+ "contain any service shapes");
} else if (services.size() > 1) {
throw new CodegenException("Cannot infer a service to generate because the model contains "
+ "multiple service shapes: " + services);
} else {
LOGGER.info("Inferring service to generate as " + services.get(0));
return services.get(0);
}
}

/**
* Gets the id of the service that is being generated.
*
Expand Down

0 comments on commit 5c8d7bb

Please sign in to comment.