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

codegen: add Options() method to read copied client config #482

Merged
merged 1 commit into from
Nov 29, 2023
Merged
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
8 changes: 8 additions & 0 deletions .changelog/966e9ee62f384f2aa660c8bac175addb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "966e9ee6-2f38-4f2a-a660-c8bac175addb",
"type": "feature",
"description": "Expose Options() method on generated service clients.",
"modules": [
"."
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object>... args) {
validateTemplateArgsNotNull(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,6 +96,7 @@ private GoWriter.Writable generate() {
generateMetadata(),
generateClient(),
generateNew(),
generateGetOptions(),
generateInvokeOperation(),
generateInputContextFuncs(),
generateAddProtocolFinalizerMiddleware()
Expand Down Expand Up @@ -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());
Expand Down
1 change: 0 additions & 1 deletion endpoints/private/rulesfn/strings.go
Original file line number Diff line number Diff line change
@@ -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].
Expand Down
Loading