Skip to content

Commit

Permalink
Global config for Hearth clients
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp committed Jun 29, 2024
1 parent 1a725f8 commit 6156469
Show file tree
Hide file tree
Showing 17 changed files with 290 additions and 14 deletions.
6 changes: 5 additions & 1 deletion codegen/projections/rails_json/lib/rails_json/client.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion codegen/projections/rpcv2_cbor/lib/rpcv2_cbor/client.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions codegen/projections/white_label/lib/white_label/client.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean includeFor(ServiceShape service, Model model) {
}

@Override
public List<RubyRuntimePlugin> getRuntimePlugins(GenerationContext context) {
public List<RubyRuntimePlugin> getAdditionalRuntimePlugins(GenerationContext context) {
return List.of(RubyRuntimePlugin.builder()
.rubySource("plugins/test_plugin.rb")
.pluginClass("Plugins::TestPlugin")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import software.amazon.smithy.ruby.codegen.generators.ConfigGenerator;
import software.amazon.smithy.ruby.codegen.generators.EndpointGenerator;
import software.amazon.smithy.ruby.codegen.generators.GemspecGenerator;
import software.amazon.smithy.ruby.codegen.generators.GlobalConfigPluginGenerator;
import software.amazon.smithy.ruby.codegen.generators.HttpProtocolTestGenerator;
import software.amazon.smithy.ruby.codegen.generators.MiddlewareGenerator;
import software.amazon.smithy.ruby.codegen.generators.ModuleGenerator;
Expand Down Expand Up @@ -166,6 +167,9 @@ public void generateService(GenerateServiceDirective<GenerationContext, RubySett
configGenerator.render();
configGenerator.renderRbs();

GlobalConfigPluginGenerator globalConfigPluginGenerator = new GlobalConfigPluginGenerator(directive);
globalConfigPluginGenerator.render();

MiddlewareGenerator middlewareGenerator = new MiddlewareGenerator(directive, middlewareBuilder);
middlewareGenerator.render();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -298,13 +299,17 @@ public Set<RubyDependency> getRubyDependencies() {
* @return Set of all RubyRuntimePlugins from all integrations
*/
public Set<RubyRuntimePlugin> getRuntimePlugins() {
return integrations.stream()
.map((i) -> i.getRuntimePlugins(this))
.flatMap(List::stream)
.collect(Collectors.toUnmodifiableSet());
Set<RubyRuntimePlugin> runtimePlugins = new LinkedHashSet<>();
runtimePlugins.add(RubyRuntimePlugin.builder()
.pluginClass("Plugins::GlobalConfig")
.writeAdditionalFiles(context -> Collections.singletonList("plugins/global_config.rb"))
.build());
integrations.forEach((i) -> {
runtimePlugins.addAll(i.getAdditionalRuntimePlugins(this));
});
return Collections.unmodifiableSet(runtimePlugins);
}


/**
* @return Set of AuthSchemes that apply to this service.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
*/
public final class Hearth {

public static final Symbol HEARTH = Symbol.builder()
.name("Hearth")
.build();

public static final Symbol CLIENT = Symbol.builder()
.namespace("Hearth", "::")
.name("Client")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public RubyCodeWriter preamble() {
}

/**
* Require statments for symbols/dependenices used
* Require statements for symbols/dependencies used
* will be included in the generated code.
* This should be called for writers that are used to generate full files.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ default List<ClientConfig> getAdditionalClientConfig(GenerationContext context)
* @param context - Generation context to process within
* @return List of RubyRuntimePlugins
*/
default List<RubyRuntimePlugin> getRuntimePlugins(GenerationContext context) {
default List<RubyRuntimePlugin> getAdditionalRuntimePlugins(GenerationContext context) {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.generators;

import java.nio.file.Paths;
import java.util.logging.Logger;
import software.amazon.smithy.codegen.core.directed.ContextualDirective;
import software.amazon.smithy.ruby.codegen.GenerationContext;
import software.amazon.smithy.ruby.codegen.Hearth;
import software.amazon.smithy.ruby.codegen.RubyCodeWriter;
import software.amazon.smithy.ruby.codegen.RubyFormatter;
import software.amazon.smithy.ruby.codegen.RubySettings;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Generate Config class for a Client.
*/
@SmithyInternalApi
public class GlobalConfigPluginGenerator extends RubyGeneratorBase {
private static final Logger LOGGER =
Logger.getLogger(ConfigGenerator.class.getName());

public GlobalConfigPluginGenerator(
ContextualDirective<GenerationContext, RubySettings> directive) {
super(directive);
}

@Override
protected String getModule() {
return "Plugins";
}

@Override
public String rbFile() {
return Paths.get(settings.getGemName(), "lib", settings.getGemName(),
RubyFormatter.toSnakeCase(getModule()), "global_config.rb").toString();
}

public void render() {
write(writer -> {
writer
.preamble()
.includeRequires()
.addModule(settings.getModule())
.addModule("Plugins")
.openBlock("class GlobalConfig")
.call(() -> renderCallMethod(writer))
.closeBlock("end")
.closeAllModules();
});
LOGGER.fine("Wrote config defaults plugin to " + rbFile());
}

private void renderCallMethod(RubyCodeWriter writer) {
writer
.openBlock("def call(config)")
.write("options = config.options")
.openBlock("$T.config.each do |key, value|", Hearth.HEARTH)
.write("config[key] = value unless options.key?(key)")
.closeBlock("end")
.closeBlock("end");
}
}
19 changes: 19 additions & 0 deletions hearth/lib/hearth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@
require_relative 'hearth/waiters/waiter'
require_relative 'hearth/xml'

# Hearth is a low-level Ruby component library for code generated clients using
# the Smithy modeling language.
module Hearth
VERSION = File.read(File.expand_path('../VERSION', __dir__)).strip

@config = {}

class << self
# @return [Hash] Returns a hash of default configuration options shared
# by all constructed clients.
attr_reader :config

# @param [Hash] config
def config=(config)
unless config.is_a?(Hash)
raise ArgumentError, 'configuration must be a hash'
end

@config = config
end
end
end
Loading

0 comments on commit 6156469

Please sign in to comment.