From e2b1ae300cb1b29a494061e2bee0ecdf7edf2db2 Mon Sep 17 00:00:00 2001 From: Matt Muller Date: Fri, 4 Aug 2023 16:55:18 -0400 Subject: [PATCH] Revert "Refactor ClientConfig defaults (#153)" This reverts commit 8a86cebdd180ceb0fe3bb0f4baae1e319dc9d211. --- .../ruby/codegen/ApplicationTransport.java | 37 ++++--- .../smithy/ruby/codegen/ClientFragment.java | 5 +- .../ruby/codegen/config/ClientConfig.java | 103 +++++++++--------- .../ruby/codegen/config/ConfigDefaults.java | 51 --------- .../ruby/codegen/config/ConfigProvider.java | 3 - .../codegen/config/ConfigProviderChain.java | 40 ++----- .../ruby/codegen/config/DefaultLiteral.java | 50 --------- .../codegen/generators/ConfigGenerator.java | 12 +- .../integrations/RequestCompression.java | 17 ++- .../ruby/codegen/middleware/Middleware.java | 24 ++-- .../codegen/middleware/MiddlewareBuilder.java | 32 +++--- .../integrations/RailsIntegration.java | 2 +- 12 files changed, 130 insertions(+), 246 deletions(-) delete mode 100644 codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ConfigDefaults.java delete mode 100644 codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/DefaultLiteral.java diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/ApplicationTransport.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/ApplicationTransport.java index 04903c039..2b04b4e2d 100644 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/ApplicationTransport.java +++ b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/ApplicationTransport.java @@ -28,6 +28,7 @@ import software.amazon.smithy.model.traits.HttpChecksumRequiredTrait; import software.amazon.smithy.model.traits.HttpTrait; import software.amazon.smithy.ruby.codegen.config.ClientConfig; +import software.amazon.smithy.ruby.codegen.config.ConfigProviderChain; import software.amazon.smithy.ruby.codegen.middleware.Middleware; import software.amazon.smithy.ruby.codegen.middleware.MiddlewareStackStep; import software.amazon.smithy.ruby.codegen.util.Streaming; @@ -79,34 +80,40 @@ public ApplicationTransport( */ public static ApplicationTransport createDefaultHttpApplicationTransport() { - ClientConfig endpoint = ClientConfig.builder() + ClientConfig endpoint = (new ClientConfig.Builder()) .name("endpoint") .type("String") .documentation("Endpoint of the service") .allowOperationOverride() - .defaultDynamicValue("proc { |cfg| cfg[:stub_responses] ? 'http://localhost' : nil }") + .defaults(new ConfigProviderChain.Builder() + .dynamicProvider("proc { |cfg| cfg[:stub_responses] ? 'http://localhost' : nil }") + .build() + ) .build(); - ClientFragment request = ClientFragment.builder() + ClientFragment request = (new ClientFragment.Builder()) .addConfig(endpoint) // TODO: Replace URI with Endpoint middleware - should be a blank request .render((self, ctx) -> "Hearth::HTTP::Request.new(uri: URI(" + endpoint.renderGetConfigValue() + "))") .build(); - ClientFragment response = ClientFragment.builder() + ClientFragment response = (new ClientFragment.Builder()) .render((self, ctx) -> "Hearth::HTTP::Response.new(body: response_body)") .build(); - ClientConfig httpClient = ClientConfig.builder() + ClientConfig httpClient = (new ClientConfig.Builder()) .name("http_client") .type("Hearth::HTTP::Client") .documentation("The HTTP Client to use for request transport.") .documentationDefaultValue("Hearth::HTTP::Client.new") .allowOperationOverride() - .defaultDynamicValue("proc { |cfg| Hearth::HTTP::Client.new(logger: cfg[:logger]) }") + .defaults(new ConfigProviderChain.Builder() + .dynamicProvider("proc { |cfg| Hearth::HTTP::Client.new(logger: cfg[:logger]) }") + .build() + ) .build(); - ClientFragment client = ClientFragment.builder() + ClientFragment client = (new ClientFragment.Builder()) .addConfig(httpClient) .render((self, ctx) -> httpClient.renderGetConfigValue()) .build(); @@ -114,7 +121,7 @@ public static ApplicationTransport createDefaultHttpApplicationTransport() { MiddlewareList defaultMiddleware = (transport, context) -> { List middleware = new ArrayList<>(); - middleware.add(Middleware.builder() + middleware.add(new Middleware.Builder() .klass(Hearth.BUILD_MIDDLEWARE) .step(MiddlewareStackStep.BUILD) .operationParams((ctx, operation) -> { @@ -126,7 +133,7 @@ public static ApplicationTransport createDefaultHttpApplicationTransport() { .build() ); - middleware.add(Middleware.builder() + middleware.add((new Middleware.Builder()) .klass("Hearth::HTTP::Middleware::ContentLength") .operationPredicate( (model, service, operation) -> @@ -137,7 +144,7 @@ public static ApplicationTransport createDefaultHttpApplicationTransport() { .build() ); - middleware.add(Middleware.builder() + middleware.add((new Middleware.Builder()) .klass("Hearth::HTTP::Middleware::ContentMD5") .step(MiddlewareStackStep.AFTER_BUILD) .operationPredicate( @@ -145,7 +152,7 @@ public static ApplicationTransport createDefaultHttpApplicationTransport() { .build() ); - middleware.add(Middleware.builder() + middleware.add((new Middleware.Builder()) .klass(Hearth.PARSE_MIDDLEWARE) .step(MiddlewareStackStep.PARSE) .operationParams((ctx, operation) -> { @@ -158,10 +165,10 @@ public static ApplicationTransport createDefaultHttpApplicationTransport() { successCode = "" + httpTrait.get().getCode(); } String errors = operation.getErrors() - .stream() - .map((error) -> "Errors::" - + ctx.symbolProvider().toSymbol(ctx.model().expectShape(error)).getName()) - .collect(Collectors.joining(", ")); + .stream() + .map((error) -> "Errors::" + + ctx.symbolProvider().toSymbol(ctx.model().expectShape(error)).getName()) + .collect(Collectors.joining(", ")); params.put("error_parser", "Hearth::HTTP::ErrorParser.new(" + "error_module: Errors, success_status: " + successCode diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/ClientFragment.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/ClientFragment.java index e52419b8e..7289e15cf 100644 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/ClientFragment.java +++ b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/ClientFragment.java @@ -40,10 +40,6 @@ public ClientFragment(Builder builder) { this.render = builder.render; } - public static Builder builder() { - return new Builder(); - } - /** * @return set of client config to apply to support this fragment. */ @@ -84,6 +80,7 @@ public static class Builder implements SmithyBuilder { }; /** + * * @param config config to be added to support this fragment. * @return this builder. */ diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ClientConfig.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ClientConfig.java index ee8654aa8..1192cb7f5 100644 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ClientConfig.java +++ b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ClientConfig.java @@ -17,8 +17,6 @@ import java.util.Objects; import java.util.Set; -import software.amazon.smithy.ruby.codegen.ClientFragment; -import software.amazon.smithy.ruby.codegen.GenerationContext; import software.amazon.smithy.utils.SmithyBuilder; import software.amazon.smithy.utils.SmithyUnstableApi; @@ -31,8 +29,11 @@ public class ClientConfig { private final String name; private final String type; private final String documentation; + private final String documentationDefaultValue; private final String documentationType; - private final ConfigDefaults defaults; + private final ConfigProviderChain defaults; + + private final String defaultLiteral; private final boolean allowOperationOverride; /** @@ -42,22 +43,13 @@ public ClientConfig(Builder builder) { this.name = builder.name; this.type = builder.type; this.documentation = builder.documentation; + this.documentationDefaultValue = builder.documentationDefaultValue; this.documentationType = builder.documentationType; - if (builder.defaults != null) { - this.defaults = builder.defaults; - } else { - this.defaults = new DefaultLiteral("[]"); - } - if (builder.documentationDefaultValue != null) { - this.defaults.setDocumentationDefault(builder.documentationDefaultValue); - } + this.defaults = builder.defaults; + this.defaultLiteral = builder.defaultLiteral; this.allowOperationOverride = builder.allowOperationOverride; } - public static Builder builder() { - return new Builder(); - } - /** * @return The name of the config - this will be the initialization parameter name as well * as the member variable name. @@ -87,7 +79,13 @@ public String getDocumentation() { * @return Documented default value. */ public String getDocumentationDefaultValue() { - return defaults.getDocumentationDefault().orElse(""); + if (documentationDefaultValue != null) { + return documentationDefaultValue; + } + if (defaults != null) { + return defaults.getDocumentationDefault().orElse(""); + } + return ""; } /** @@ -101,12 +99,17 @@ public String getDocumentationType() { } /** - * Render the defaults for this config. - * - * @param context generation context + * @return chain of defaults to use. + */ + public ConfigProviderChain getDefaults() { + return defaults; + } + + /** + * @return a literal default value to be rendered. Must result in an array of defaults in Ruby. */ - public String renderDefaults(GenerationContext context) { - return defaults.renderDefault(context); + public String getDefaultLiteral() { + return defaultLiteral; } /** @@ -133,7 +136,12 @@ public String renderGetConfigValue() { public void addToConfigCollection(Set configCollection) { if (!configCollection.contains(this)) { configCollection.add(this); - defaults.addToConfigCollection(configCollection); + if (defaults != null) { + defaults.getProviders().forEach((p) -> { + p.providerFragment().getClientConfig() + .forEach((c) -> c.addToConfigCollection(configCollection)); + }); + } } } @@ -155,6 +163,10 @@ public int hashCode() { return Objects.hash(getName(), getType()); } + public static Builder builder() { + return new Builder(); + } + /** * Builder for ClientConfig. */ @@ -164,13 +176,10 @@ public static class Builder implements SmithyBuilder { private String documentation; private String documentationDefaultValue; private String documentationType; - private ConfigDefaults defaults; + private ConfigProviderChain defaults; + private String defaultLiteral; private boolean allowOperationOverride = false; - protected Builder() { - - } - /** * @param name name of the config option. * @return this builder. @@ -199,7 +208,7 @@ public Builder documentation(String documentation) { } /** - * @param defaultValue an optional default value to be use in documentation. + * @param defaultValue an optional default value * @return this builder */ public Builder documentationDefaultValue(String defaultValue) { @@ -233,20 +242,7 @@ public Builder allowOperationOverride() { * @return this builder */ public Builder defaultPrimitiveValue(String value) { - validateDefaultNotSet(); - this.defaults = ConfigProviderChain.builder().staticProvider(value).build(); - return this; - } - - public Builder defaultDynamicValue(String rubyDefaultBlock) { - validateDefaultNotSet(); - this.defaults = ConfigProviderChain.builder().dynamicProvider(rubyDefaultBlock).build(); - return this; - } - - public Builder defaultDynamicValue(ClientFragment rubyDefaultBlock) { - validateDefaultNotSet(); - this.defaults = ConfigProviderChain.builder().dynamicProvider(rubyDefaultBlock).build(); + this.defaults = new ConfigProviderChain.Builder().staticProvider(value).build(); return this; } @@ -255,8 +251,7 @@ public Builder defaultDynamicValue(ClientFragment rubyDefaultBlock) { * @return this builder */ public Builder defaultValue(String value) { - validateDefaultNotSet(); - this.defaults = ConfigProviderChain.builder() + this.defaults = new ConfigProviderChain.Builder() .dynamicProvider("proc { " + value + " }") .build(); return this; @@ -266,8 +261,11 @@ public Builder defaultValue(String value) { * @param defaults chain of default providers to use. * @return this builder. */ - public Builder defaults(ConfigDefaults defaults) { - validateDefaultNotSet(); + public Builder defaults(ConfigProviderChain defaults) { + if (defaultLiteral != null) { + throw new IllegalArgumentException("ConfigProviderChain defaults are incompatible " + + "with defaultLiteral. You must provide only one."); + } this.defaults = defaults; return this; } @@ -277,8 +275,11 @@ public Builder defaults(ConfigDefaults defaults) { * @return this builder */ public Builder defaultLiteral(String defaultLiteral) { - validateDefaultNotSet(); - this.defaults = new DefaultLiteral(defaultLiteral); + if (defaults != null) { + throw new IllegalArgumentException("ConfigProviderChain defaults are incompatible " + + "with defaultLiteral. You must provide only one."); + } + this.defaultLiteral = defaultLiteral; return this; } @@ -286,11 +287,5 @@ public Builder defaultLiteral(String defaultLiteral) { public ClientConfig build() { return new ClientConfig(this); } - - private void validateDefaultNotSet() { - if (defaults != null) { - throw new IllegalArgumentException("ConfigDefaults have already been set."); - } - } } } diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ConfigDefaults.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ConfigDefaults.java deleted file mode 100644 index ee8ce0d22..000000000 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ConfigDefaults.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.config; - -import java.util.Optional; -import java.util.Set; -import software.amazon.smithy.ruby.codegen.GenerationContext; - -/** - * Represents the default values for a ClientConfig. - */ -public interface ConfigDefaults { - /** - * Render the defaults for this config value. - * - * @param context generation context - * @return rendered default. Must be an array of defaults (callable/literals) - */ - String renderDefault(GenerationContext context); - - /** - * @return a string if there should be documentation for the default. - */ - Optional getDocumentationDefault(); - - /** - * @param documentationDefault default value to add to documentation. - */ - void setDocumentationDefault(String documentationDefault); - - /** - * Add any dependent ClientConfig to the collection. - * ConfigDefaults may depend on other ClientConfig (eg, creating a logger may use a log_level config). - * - * @param configCollection collection to add dependent config to. - */ - void addToConfigCollection(Set configCollection); -} diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ConfigProvider.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ConfigProvider.java index 49294941a..74f164900 100644 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ConfigProvider.java +++ b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ConfigProvider.java @@ -21,9 +21,6 @@ public interface ConfigProvider { /** - * Config defaults may depend on other config (eg a logger that uses log_level). - * The ClientFragment can be used to express these dependencies. - * * @return the provider rendered into Ruby code */ ClientFragment providerFragment(); diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ConfigProviderChain.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ConfigProviderChain.java index f2acae5cd..90004ee58 100644 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ConfigProviderChain.java +++ b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/ConfigProviderChain.java @@ -18,10 +18,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; import software.amazon.smithy.ruby.codegen.ClientFragment; -import software.amazon.smithy.ruby.codegen.GenerationContext; import software.amazon.smithy.utils.SmithyBuilder; import software.amazon.smithy.utils.SmithyUnstableApi; @@ -29,64 +26,41 @@ * Describes config values to be added to generated Client classes. */ @SmithyUnstableApi -public class ConfigProviderChain implements ConfigDefaults { +public class ConfigProviderChain { private final List providers; - private Optional documentationDefaultOverride; /** * @param builder builder to construct the chain from. */ public ConfigProviderChain(Builder builder) { this.providers = builder.providers; - documentationDefaultOverride = Optional.empty(); } /** - * @return a new builder + * @return an ordered list of all of the providers. */ - public static Builder builder() { - return new Builder(); - } - - @Override - public String renderDefault(GenerationContext context) { - return "[" + providers.stream() - .map((p) -> p.providerFragment().render(context)) - .collect(Collectors.joining(",")) + "]"; + public List getProviders() { + return List.copyOf(this.providers); } /** * @return a string if there should be documentation for the default. */ public Optional getDocumentationDefault() { - if (documentationDefaultOverride.isPresent()) { - return documentationDefaultOverride; - } - return providers.stream().map((p) -> p.getDocumentationDefault()) .filter((d) -> d.isPresent()) .map((d) -> d.get()) .findFirst(); } - @Override - public void setDocumentationDefault(String documentationDefault) { - this.documentationDefaultOverride = Optional.of(documentationDefault); - } - - @Override - public void addToConfigCollection(Set configCollection) { - providers.forEach(p -> configCollection.addAll(p.providerFragment().getClientConfig())); - } - /** * Builder for generating an ordered list of config providers. */ public static class Builder implements SmithyBuilder { private final List providers; - protected Builder() { + public Builder() { providers = new ArrayList<>(); } @@ -124,7 +98,7 @@ public Builder dynamicProvider(ClientFragment providerFragment) { /** * @param environmentKey ENV key to get value from - * @param type ruby type to attempt to coerce the value to + * @param type ruby type to attempt to coerce the value to * @return this builder */ public Builder envProvider(String environmentKey, String type) { @@ -134,7 +108,7 @@ public Builder envProvider(String environmentKey, String type) { /** * @param configKey the key in the shared config files - * @param type ruby type to attempt to coerce the value to + * @param type ruby type to attempt to coerce the value to * @return this builder */ public Builder sharedConfigProvider(String configKey, String type) { diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/DefaultLiteral.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/DefaultLiteral.java deleted file mode 100644 index 62896a1bd..000000000 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/config/DefaultLiteral.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.config; - -import java.util.Optional; -import java.util.Set; -import software.amazon.smithy.ruby.codegen.GenerationContext; - -public class DefaultLiteral implements ConfigDefaults { - - private String documentationDefault; - private final String defaultLiteral; - - public DefaultLiteral(String defaultLiteral) { - this.defaultLiteral = defaultLiteral; - } - - @Override - public String renderDefault(GenerationContext context) { - return defaultLiteral; - } - - @Override - public Optional getDocumentationDefault() { - return Optional.ofNullable(documentationDefault); - } - - @Override - public void setDocumentationDefault(String documentationDefault) { - this.documentationDefault = documentationDefault; - } - - @Override - public void addToConfigCollection(Set configCollection) { - - } -} diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/generators/ConfigGenerator.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/generators/ConfigGenerator.java index 95c685451..29de2e9ba 100644 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/generators/ConfigGenerator.java +++ b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/generators/ConfigGenerator.java @@ -135,8 +135,16 @@ private void renderDefaultsMethod(RubyCodeWriter writer) { .openBlock("@defaults ||= {"); clientConfigList.forEach(clientConfig -> { - writer.write("$L: $L,", clientConfig.getName(), clientConfig.renderDefaults(context)); - + if (clientConfig.getDefaults() == null) { + if (clientConfig.getDefaultLiteral() != null) { + writer.write("$L: $L,", clientConfig.getName(), clientConfig.getDefaultLiteral()); + } + } else { + String defaults = clientConfig.getDefaults().getProviders().stream() + .map((p) -> p.providerFragment().render(context)) + .collect(Collectors.joining(",")); + writer.write("$L: [$L],", clientConfig.getName(), defaults); + } }); writer diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/integrations/RequestCompression.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/integrations/RequestCompression.java index 2fc54bca2..890744502 100644 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/integrations/RequestCompression.java +++ b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/integrations/RequestCompression.java @@ -28,6 +28,7 @@ import software.amazon.smithy.ruby.codegen.GenerationContext; import software.amazon.smithy.ruby.codegen.RubyIntegration; import software.amazon.smithy.ruby.codegen.config.ClientConfig; +import software.amazon.smithy.ruby.codegen.config.ConfigProviderChain; import software.amazon.smithy.ruby.codegen.middleware.Middleware; import software.amazon.smithy.ruby.codegen.middleware.MiddlewareBuilder; import software.amazon.smithy.ruby.codegen.middleware.MiddlewareStackStep; @@ -43,12 +44,15 @@ public boolean includeFor(ServiceShape service, Model model) { @Override public void modifyClientMiddleware(MiddlewareBuilder middlewareBuilder, GenerationContext context) { - ClientConfig disableRequestCompression = ClientConfig.builder() + ClientConfig disableRequestCompression = (new ClientConfig.Builder()) .name("disable_request_compression") .type("Boolean") - .defaultPrimitiveValue("false") + .defaultValue("false") .documentation("When set to 'true' the request body will not be compressed for supported operations.") .allowOperationOverride() + .defaults(new ConfigProviderChain.Builder() + .staticProvider("false") + .build()) .build(); String minCompressionDocumentation = """ @@ -56,15 +60,18 @@ public void modifyClientMiddleware(MiddlewareBuilder middlewareBuilder, Generati The value must be non-negative integer value between 0 and 10485780 bytes inclusive. """; - ClientConfig requestMinCompressionSizeBytes = ClientConfig.builder() + ClientConfig requestMinCompressionSizeBytes = (new ClientConfig.Builder()) .name("request_min_compression_size_bytes") .type("Integer") + .defaultValue("10240") .documentation(minCompressionDocumentation) .allowOperationOverride() - .defaultPrimitiveValue("10240") + .defaults(new ConfigProviderChain.Builder() + .staticProvider("10240") + .build()) .build(); - Middleware compression = Middleware.builder() + Middleware compression = (new Middleware.Builder()) .operationPredicate(((model, service, operation) -> operation.hasTrait(RequestCompressionTrait.class))) .operationParams((ctx, operation) -> { Map params = new HashMap<>(); diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/middleware/Middleware.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/middleware/Middleware.java index 6b8f369f5..133a1c154 100644 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/middleware/Middleware.java +++ b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/middleware/Middleware.java @@ -82,10 +82,6 @@ private Middleware(Builder builder) { this.writeAdditionalFiles = builder.writeAdditionalFiles; } - public static Builder builder() { - return new Builder(); - } - /** * @return the Ruby class to use for the middleware. */ @@ -129,7 +125,7 @@ public Map getAdditionalParams() { } /** - * @param model model + * @param model model * @param service service to test for * @return true if this middleware should be included for the service */ @@ -138,8 +134,9 @@ public boolean includeFor(Model model, ServiceShape service) { } /** - * @param model model - * @param service service to test for + * + * @param model model + * @param service service to test for * @param operation operation in the service to test for * @return true if this midldeware should be included for this operation/service */ @@ -150,9 +147,8 @@ public boolean includeFor(Model model, ServiceShape service, /** * Generate code to add this middleware to an operation method. - * - * @param writer writer - * @param context generation context + * @param writer writer + * @param context generation context * @param operation operation to add to */ public void renderAdd(RubyCodeWriter writer, GenerationContext context, @@ -162,7 +158,6 @@ public void renderAdd(RubyCodeWriter writer, GenerationContext context, /** * Write additional files required by this middleware. - * * @param context generation context * @return List of additional files written out */ @@ -170,6 +165,10 @@ public List writeAdditionalFiles(GenerationContext context) { return writeAdditionalFiles.writeAdditionalFiles(context); } + public static Builder builder() { + return new Builder(); + } + @FunctionalInterface /** * Called to Render the addition of this middleware to the stack. @@ -267,9 +266,6 @@ public static class Builder implements SmithyBuilder { private WriteAdditionalFiles writeAdditionalFiles = (context) -> Collections.emptyList(); - protected Builder() { - } - /** * @param klass the Ruby class of the Middleware * @return Returns the builder diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/middleware/MiddlewareBuilder.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/middleware/MiddlewareBuilder.java index ab7a72817..80302273c 100644 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/middleware/MiddlewareBuilder.java +++ b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/middleware/MiddlewareBuilder.java @@ -40,6 +40,7 @@ import software.amazon.smithy.ruby.codegen.RubyCodeWriter; import software.amazon.smithy.ruby.codegen.RubySymbolProvider; import software.amazon.smithy.ruby.codegen.config.ClientConfig; +import software.amazon.smithy.ruby.codegen.config.ConfigProviderChain; import software.amazon.smithy.utils.SmithyInternalApi; @SmithyInternalApi @@ -172,13 +173,13 @@ public void addDefaultMiddleware(GenerationContext context) { ApplicationTransport transport = context.applicationTransport(); SymbolProvider symbolProvider = context.symbolProvider(); - Middleware initialize = Middleware.builder() + Middleware initialize = (new Middleware.Builder()) .klass(Hearth.INITIALIZE_MIDDLEWARE) .step(MiddlewareStackStep.INITIALIZE) .order(Byte.MIN_VALUE) .build(); - ClientConfig validateInput = ClientConfig.builder() + ClientConfig validateInput = (new ClientConfig.Builder()) .name("validate_input") .type("Boolean") .defaultPrimitiveValue("true") @@ -186,7 +187,7 @@ public void addDefaultMiddleware(GenerationContext context) { "When `true`, request parameters are validated using the modeled shapes.") .build(); - Middleware validate = Middleware.builder() + Middleware validate = (new Middleware.Builder()) .klass(Hearth.VALIDATE_MIDDLEWARE) .step(MiddlewareStackStep.VALIDATE) .operationParams((ctx, operation) -> { @@ -200,7 +201,7 @@ public void addDefaultMiddleware(GenerationContext context) { .addConfig(validateInput) .build(); - ClientConfig disableHostPrefix = ClientConfig.builder() + ClientConfig disableHostPrefix = (new ClientConfig.Builder()) .name("disable_host_prefix") .type("Boolean") .defaultPrimitiveValue("false") @@ -208,7 +209,7 @@ public void addDefaultMiddleware(GenerationContext context) { "When `true`, does not perform host prefix injection using @endpoint's hostPrefix property.") .build(); - Middleware hostPrefix = Middleware.builder() + Middleware hostPrefix = (new Middleware.Builder()) .klass("Hearth::Middleware::HostPrefix") .step(MiddlewareStackStep.BUILD) .relative(new Middleware.Relative( @@ -235,7 +236,7 @@ public void addDefaultMiddleware(GenerationContext context) { }) .build(); - ClientConfig stubResponses = ClientConfig.builder() + ClientConfig stubResponses = (new ClientConfig.Builder()) .name("stub_responses") .type("Boolean") .defaultPrimitiveValue("false") @@ -243,7 +244,7 @@ public void addDefaultMiddleware(GenerationContext context) { "Enable response stubbing for testing. See {Hearth::ClientStubs#stub_responses}.") .build(); - ClientConfig retryStrategy = ClientConfig.builder() + ClientConfig retryStrategy = (new ClientConfig.Builder()) .name("retry_strategy") .type("Hearth::Retry::Strategy") .documentationDefaultValue("Hearth::Retry::Standard.new") @@ -262,14 +263,14 @@ public void addDefaultMiddleware(GenerationContext context) { ) .build(); - Middleware retry = Middleware.builder() + Middleware retry = (new Middleware.Builder()) .klass(Hearth.RETRY_MIDDLEWARE) .step(MiddlewareStackStep.RETRY) .addConfig(retryStrategy) .addParam("error_inspector_class", transport.getErrorInspector()) .build(); - Middleware send = Middleware.builder() + Middleware send = (new Middleware.Builder()) .klass(Hearth.SEND_MIDDLEWARE) .step(MiddlewareStackStep.SEND) .addParam("client", @@ -299,22 +300,25 @@ public void addDefaultMiddleware(GenerationContext context) { } private Collection getDefaultClientConfig() { - ClientConfig logger = ClientConfig.builder() + ClientConfig logger = (new ClientConfig.Builder()) .name("logger") .type("Logger") .documentationDefaultValue("Logger.new($stdout, level: cfg.log_level)") - .defaultDynamicValue("proc { |cfg| Logger.new($stdout, level: cfg[:log_level]) }") + .defaults(new ConfigProviderChain.Builder() + .dynamicProvider("proc { |cfg| Logger.new($stdout, level: cfg[:log_level]) }") + .build() + ) .documentation("The Logger instance to use for logging.") .build(); - ClientConfig logLevel = ClientConfig.builder() + ClientConfig logLevel = (new ClientConfig.Builder()) .name("log_level") .type("Symbol") .defaultPrimitiveValue(":info") .documentation("The default log level to use with the Logger.") .build(); - ClientConfig plugins = ClientConfig.builder() + ClientConfig plugins = (new ClientConfig.Builder()) .name("plugins") .type("Hearth::PluginList") .defaultValue("Hearth::PluginList.new") @@ -324,7 +328,7 @@ private Collection getDefaultClientConfig() { + "Plugins may modify the provided config.") .build(); - ClientConfig interceptors = ClientConfig.builder() + ClientConfig interceptors = (new ClientConfig.Builder()) .name("interceptors") .type("Hearth::InterceptorList") .defaultValue("Hearth::InterceptorList.new") diff --git a/codegen/smithy-ruby-rails-codegen/src/main/java/software/amazon/smithy/ruby/codegen/integrations/RailsIntegration.java b/codegen/smithy-ruby-rails-codegen/src/main/java/software/amazon/smithy/ruby/codegen/integrations/RailsIntegration.java index 94005bbf3..75d994e8b 100644 --- a/codegen/smithy-ruby-rails-codegen/src/main/java/software/amazon/smithy/ruby/codegen/integrations/RailsIntegration.java +++ b/codegen/smithy-ruby-rails-codegen/src/main/java/software/amazon/smithy/ruby/codegen/integrations/RailsIntegration.java @@ -37,7 +37,7 @@ public List getProtocolGenerators() { @Override public void modifyClientMiddleware(MiddlewareBuilder middlewareBuilder, GenerationContext context) { - Middleware requestId = Middleware.builder() + Middleware requestId = (new Middleware.Builder()) .klass("Middleware::RequestId") .step(MiddlewareStackStep.PARSE) .rubySource("smithy-ruby-rails-codegen/middleware/request_id.rb")