From 883dfe191bda6c4f12c95b191824390f9f7b09db Mon Sep 17 00:00:00 2001 From: Luc Talatinian Date: Tue, 28 Nov 2023 14:47:25 -0500 Subject: [PATCH] codegen: add Options() method to read copied client config --- .../966e9ee62f384f2aa660c8bac175addb.json | 8 ++++++++ .../amazon/smithy/go/codegen/GoWriter.java | 17 +++++++++++++++++ .../smithy/go/codegen/ServiceGenerator.java | 16 ++++++++++++++++ endpoints/private/rulesfn/strings.go | 1 - 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 .changelog/966e9ee62f384f2aa660c8bac175addb.json diff --git a/.changelog/966e9ee62f384f2aa660c8bac175addb.json b/.changelog/966e9ee62f384f2aa660c8bac175addb.json new file mode 100644 index 000000000..ad441a2f6 --- /dev/null +++ b/.changelog/966e9ee62f384f2aa660c8bac175addb.json @@ -0,0 +1,8 @@ +{ + "id": "966e9ee6-2f38-4f2a-a660-c8bac175addb", + "type": "feature", + "description": "Expose Options() method on generated service clients.", + "modules": [ + "." + ] +} \ No newline at end of file diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoWriter.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoWriter.java index 195113c2b..e5cd6b83f 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoWriter.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoWriter.java @@ -152,6 +152,23 @@ public static Writable goDocTemplate(String contents) { return goDocTemplate(contents, new HashMap<>()); } + /** + * Auto-formats a multi-paragraph string as a doc writable (including line wrapping). + * @param contents The docs. + * @return writer for formatted docs. + */ + public static Writable autoDocTemplate(String contents) { + return GoWriter.ChainWritable.of( + Arrays.stream(contents.split("\n\n")) + .map(it -> docParagraphWriter(it.replace("\n", " "))) + .toList() + ).compose(false); + } + + private static GoWriter.Writable docParagraphWriter(String paragraph) { + return writer -> writer.writeDocs(paragraph).writeDocs(""); + } + @SafeVarargs public static Writable goDocTemplate(String contents, Map... args) { validateTemplateArgsNotNull(args); diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ServiceGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ServiceGenerator.java index 055329971..da823214d 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ServiceGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ServiceGenerator.java @@ -15,6 +15,7 @@ package software.amazon.smithy.go.codegen; +import static software.amazon.smithy.go.codegen.GoWriter.autoDocTemplate; import static software.amazon.smithy.go.codegen.GoWriter.emptyGoTemplate; import static software.amazon.smithy.go.codegen.GoWriter.goDocTemplate; import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; @@ -95,6 +96,7 @@ private GoWriter.Writable generate() { generateMetadata(), generateClient(), generateNew(), + generateGetOptions(), generateInvokeOperation(), generateInputContextFuncs(), generateAddProtocolFinalizerMiddleware() @@ -223,6 +225,20 @@ func New(options $options:L, optFns ...func(*$options:L)) *$client:L { )); } + private GoWriter.Writable generateGetOptions() { + var docs = autoDocTemplate(""" + Options returns a copy of the client configuration. + + Callers SHOULD NOT perform mutations on any inner structures within client config. Config overrides + should instead be made on a per-operation basis through functional options."""); + return goTemplate(""" + $W + func (c $P) Options() $L { + return c.options.Copy() + } + """, docs, symbolProvider.toSymbol(service), ClientOptions.NAME); + } + private GoWriter.Writable generateConfigFieldResolver(ConfigFieldResolver resolver) { return writer -> { writer.writeInline("$T(&options", resolver.getResolver()); diff --git a/endpoints/private/rulesfn/strings.go b/endpoints/private/rulesfn/strings.go index 8e230783e..5cf4a7b02 100644 --- a/endpoints/private/rulesfn/strings.go +++ b/endpoints/private/rulesfn/strings.go @@ -1,6 +1,5 @@ package rulesfn - // Substring returns the substring of the input provided. If the start or stop // indexes are not valid for the input nil will be returned. If errors occur // they will be added to the provided [ErrorCollector].