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

Service Integrations (specify integrations in smithy-build.json) #142

Closed
wants to merge 1 commit into from
Closed
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 @@ -16,24 +16,21 @@
package software.amazon.smithy.ruby.codegen.integrations;

import java.util.List;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.ruby.codegen.GenerationContext;
import software.amazon.smithy.ruby.codegen.RubyIntegration;
import software.amazon.smithy.ruby.codegen.RubyRuntimePlugin;
import software.amazon.smithy.ruby.codegen.RubyServiceIntegration;
import software.amazon.smithy.ruby.codegen.config.ClientConfig;
import software.amazon.smithy.ruby.codegen.middleware.Middleware;
import software.amazon.smithy.ruby.codegen.middleware.MiddlewareBuilder;
import software.amazon.smithy.ruby.codegen.middleware.MiddlewareStackStep;

public class WhiteLabelTestIntegration implements RubyIntegration {
public class PluginTestIntegration extends RubyServiceIntegration {

@Override
public boolean includeFor(ServiceShape service, Model model) {
return service.toShapeId().getName().equals("WhiteLabel");
public String name() {
return "plugin-test";
}


@Override
public List<RubyRuntimePlugin> getRuntimePlugins(GenerationContext context) {
return List.of(RubyRuntimePlugin.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
software.amazon.smithy.ruby.codegen.integrations.WhiteLabelTestIntegration
software.amazon.smithy.ruby.codegen.integrations.PluginTestIntegration
1 change: 1 addition & 0 deletions codegen/smithy-ruby-codegen-test/smithy-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"plugins": {
"ruby-codegen": {
"service": "smithy.ruby.tests#WhiteLabel",
"integrations": ["plugin-test"],
"module": "WhiteLabel",
"gemspec": {
"gemName": "white_label",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public GenerationContext createContext(CreateContextDirective<RubySettings, Ruby
Model model = directive.model();
List<RubyIntegration> integrations = directive.integrations().stream()
.filter((integration) -> integration
.includeFor(service, model))
.includeFor(service, model)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be one or the other. If we still need the other approach then maybe we shouldn't do this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the includeFor is much more generic and necessary for a lot of cases (eg: an integration that provides behavior for a trait where there are related config values - we don't want to add those to services that don't have the trait).

|| directive.settings().getIntegrations().contains(integration.name()))
.collect(Collectors.toList());

Map<ShapeId, ProtocolGenerator> supportedProtocols = ProtocolGenerator
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.ruby.codegen;

import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.utils.SmithyUnstableApi;

/**
* A RubyIntegration that should be applied to services by specifying `integrations`
* in the smithy-build.json for the applicable services.
*/
@SmithyUnstableApi
public abstract class RubyServiceIntegration implements RubyIntegration {
public boolean includeFor(ServiceShape service, Model model) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.ServiceIndex;
import software.amazon.smithy.model.node.ObjectNode;
Expand All @@ -35,13 +36,15 @@ public final class RubySettings {
private static final Logger LOGGER = Logger.getLogger(RubySettings.class.getName());

private static final String SERVICE = "service";
private static final String INTEGRATIONS = "integrations";
private static final String MODULE = "module";
private static final String GEMSPEC = "gemspec";
private static final String GEM_NAME = "gemName";
private static final String GEM_VERSION = "gemVersion";
private static final String GEM_SUMMARY = "gemSummary";

private ShapeId service;
private Set<String> integrations = Set.of();
private String module;
private String gemName;
private String gemVersion;
Expand All @@ -56,9 +59,15 @@ public final class RubySettings {
public static RubySettings from(ObjectNode config) {
RubySettings settings = new RubySettings();
config.warnIfAdditionalProperties(
Arrays.asList(SERVICE, MODULE, GEMSPEC, GEM_NAME, GEM_VERSION, GEM_SUMMARY));
Arrays.asList(SERVICE, INTEGRATIONS, MODULE, GEMSPEC, GEM_NAME, GEM_VERSION, GEM_SUMMARY));

settings.setService(config.expectStringMember(SERVICE).expectShapeId());
config.getArrayMember(INTEGRATIONS).ifPresent((integrations) -> {
settings.setIntegrations(
integrations.getElements().stream().map(
(n) -> n.expectStringNode().getValue()).collect(Collectors.toUnmodifiableSet())
);
});
// module and namespace
settings.setModule(config.expectStringMember(MODULE).getValue());
// required gemspec values
Expand All @@ -80,12 +89,26 @@ public ShapeId getService() {
}

/**
* @param service service to generate for
* @param service service to generate for.
*/
public void setService(ShapeId service) {
this.service = service;
}

/**
* @return list of integrations to apply to this service.
*/
public Set<String> getIntegrations() {
return integrations;
}

/**
* @param integrations integrations to apply to this service.
*/
public void setIntegrations(Set<String> integrations) {
this.integrations = integrations;
}

/**
* @return module (namespace) to generate in.
*/
Expand Down Expand Up @@ -151,9 +174,8 @@ public Set<RubyDependency> getBaseDependencies() {
}

/**
*
* @param service service to generate for
* @param model model used for generation
* @param service service to generate for
* @param model model used for generation
* @param supportedProtocolTraits ordered list of all supported protocols
* @return the resolved service protocol
*/
Expand Down