|
| 1 | +/* |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.smithy.go.codegen; |
| 17 | + |
| 18 | +import static software.amazon.smithy.go.codegen.GoWriter.goDocTemplate; |
| 19 | +import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; |
| 20 | + |
| 21 | +import java.util.Comparator; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.function.Predicate; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import software.amazon.smithy.go.codegen.auth.AuthSchemeResolverGenerator; |
| 27 | +import software.amazon.smithy.go.codegen.integration.AuthSchemeDefinition; |
| 28 | +import software.amazon.smithy.go.codegen.integration.ConfigField; |
| 29 | +import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; |
| 30 | +import software.amazon.smithy.go.codegen.integration.auth.AnonymousDefinition; |
| 31 | +import software.amazon.smithy.model.knowledge.ServiceIndex; |
| 32 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 33 | +import software.amazon.smithy.model.traits.synthetic.NoAuthTrait; |
| 34 | +import software.amazon.smithy.utils.MapUtils; |
| 35 | + |
| 36 | +/** |
| 37 | + * Implements codegen for service client config. |
| 38 | + */ |
| 39 | +public class ClientOptions implements GoWriter.Writable { |
| 40 | + public static final String NAME = "Options"; |
| 41 | + |
| 42 | + private final ProtocolGenerator.GenerationContext context; |
| 43 | + private final ApplicationProtocol protocol; |
| 44 | + |
| 45 | + private final List<ConfigField> fields; |
| 46 | + private final Map<ShapeId, AuthSchemeDefinition> authSchemes; |
| 47 | + |
| 48 | + public ClientOptions(ProtocolGenerator.GenerationContext context, ApplicationProtocol protocol) { |
| 49 | + this.context = context; |
| 50 | + this.protocol = protocol; |
| 51 | + |
| 52 | + this.fields = context.getIntegrations().stream() |
| 53 | + .flatMap(it -> it.getClientPlugins(context.getModel(), context.getService()).stream()) |
| 54 | + .flatMap(it -> it.getConfigFields().stream()) |
| 55 | + .distinct() |
| 56 | + .sorted(Comparator.comparing(ConfigField::getName)) |
| 57 | + .toList(); |
| 58 | + this.authSchemes = context.getIntegrations().stream() |
| 59 | + .flatMap(it -> it.getClientPlugins(context.getModel(), context.getService()).stream()) |
| 60 | + .flatMap(it -> it.getAuthSchemeDefinitions().entrySet().stream()) |
| 61 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void accept(GoWriter writer) { |
| 66 | + writer.write(generate()); |
| 67 | + } |
| 68 | + |
| 69 | + private GoWriter.Writable generate() { |
| 70 | + var apiOptionsDocs = goDocTemplate( |
| 71 | + "Set of options to modify how an operation is invoked. These apply to all operations " |
| 72 | + + "invoked for this client. Use functional options on operation call to modify this " |
| 73 | + + "list for per operation behavior." |
| 74 | + ); |
| 75 | + return goTemplate(""" |
| 76 | + $protocolTypes:W |
| 77 | +
|
| 78 | + type $options:L struct { |
| 79 | + $apiOptionsDocs:W |
| 80 | + APIOptions []func($stack:P) error |
| 81 | +
|
| 82 | + $fields:W |
| 83 | +
|
| 84 | + $protocolFields:W |
| 85 | + } |
| 86 | +
|
| 87 | + $copy:W |
| 88 | +
|
| 89 | + $getIdentityResolver:W |
| 90 | +
|
| 91 | + $helpers:W |
| 92 | + """, |
| 93 | + MapUtils.of( |
| 94 | + "protocolTypes", generateProtocolTypes(), |
| 95 | + "apiOptionsDocs", apiOptionsDocs, |
| 96 | + "options", NAME, |
| 97 | + "stack", SmithyGoTypes.Middleware.Stack, |
| 98 | + "fields", GoWriter.ChainWritable.of(fields.stream().map(this::writeField).toList()).compose(), |
| 99 | + "protocolFields", generateProtocolFields(), |
| 100 | + "copy", generateCopy(), |
| 101 | + "getIdentityResolver", generateGetIdentityResolver(), |
| 102 | + "helpers", generateHelpers() |
| 103 | + )); |
| 104 | + } |
| 105 | + |
| 106 | + private GoWriter.Writable generateProtocolTypes() { |
| 107 | + ensureSupportedProtocol(); |
| 108 | + return goTemplate(""" |
| 109 | + type HTTPClient interface { |
| 110 | + Do($P) ($P, error) |
| 111 | + } |
| 112 | + """, GoStdlibTypes.Net.Http.Request, GoStdlibTypes.Net.Http.Response); |
| 113 | + } |
| 114 | + |
| 115 | + private GoWriter.Writable writeField(ConfigField field) { |
| 116 | + GoWriter.Writable docs = writer -> { |
| 117 | + field.getDocumentation().ifPresent(writer::writeDocs); |
| 118 | + field.getDeprecated().ifPresent(s -> { |
| 119 | + if (field.getDocumentation().isPresent()) { |
| 120 | + writer.writeDocs(""); |
| 121 | + } |
| 122 | + writer.writeDocs(String.format("Deprecated: %s", s)); |
| 123 | + }); |
| 124 | + }; |
| 125 | + return goTemplate(""" |
| 126 | + $W |
| 127 | + $L $P |
| 128 | + """, docs, field.getName(), field.getType()); |
| 129 | + } |
| 130 | + |
| 131 | + private GoWriter.Writable generateProtocolFields() { |
| 132 | + ensureSupportedProtocol(); |
| 133 | + return goTemplate(""" |
| 134 | + $1W |
| 135 | + HTTPClient HTTPClient |
| 136 | +
|
| 137 | + $2W |
| 138 | + AuthSchemeResolver $4L |
| 139 | +
|
| 140 | + $3W |
| 141 | + AuthSchemes []$5T |
| 142 | + """, |
| 143 | + goDocTemplate("The HTTP client to invoke API calls with. " |
| 144 | + + "Defaults to client's default HTTP implementation if nil."), |
| 145 | + goDocTemplate("The auth scheme resolver which determines how to authenticate for each operation."), |
| 146 | + goDocTemplate("The list of auth schemes supported by the client."), |
| 147 | + AuthSchemeResolverGenerator.INTERFACE_NAME, |
| 148 | + SmithyGoTypes.Transport.Http.AuthScheme); |
| 149 | + } |
| 150 | + |
| 151 | + private GoWriter.Writable generateCopy() { |
| 152 | + return goTemplate(""" |
| 153 | + // Copy creates a clone where the APIOptions list is deep copied. |
| 154 | + func (o $1L) Copy() $1L { |
| 155 | + to := o |
| 156 | + to.APIOptions = make([]func($2P) error, len(o.APIOptions)) |
| 157 | + copy(to.APIOptions, o.APIOptions) |
| 158 | +
|
| 159 | + return to |
| 160 | + } |
| 161 | + """, NAME, SmithyGoTypes.Middleware.Stack); |
| 162 | + } |
| 163 | + |
| 164 | + private GoWriter.Writable generateGetIdentityResolver() { |
| 165 | + return goTemplate(""" |
| 166 | + func (o $L) GetIdentityResolver(schemeID string) $T { |
| 167 | + $W |
| 168 | + $W |
| 169 | + return nil |
| 170 | + } |
| 171 | + """, |
| 172 | + NAME, |
| 173 | + SmithyGoTypes.Auth.IdentityResolver, |
| 174 | + GoWriter.ChainWritable.of( |
| 175 | + ServiceIndex.of(context.getModel()) |
| 176 | + .getEffectiveAuthSchemes(context.getService()).keySet().stream() |
| 177 | + .filter(authSchemes::containsKey) |
| 178 | + .map(trait -> generateGetIdentityResolverMapping(trait, authSchemes.get(trait))) |
| 179 | + .toList() |
| 180 | + ).compose(false), |
| 181 | + generateGetIdentityResolverMapping(NoAuthTrait.ID, new AnonymousDefinition())); |
| 182 | + } |
| 183 | + |
| 184 | + private GoWriter.Writable generateGetIdentityResolverMapping(ShapeId schemeId, AuthSchemeDefinition scheme) { |
| 185 | + return goTemplate(""" |
| 186 | + if schemeID == $S { |
| 187 | + return $W |
| 188 | + }""", schemeId.toString(), scheme.generateOptionsIdentityResolver()); |
| 189 | + } |
| 190 | + |
| 191 | + private GoWriter.Writable generateHelpers() { |
| 192 | + return writer -> { |
| 193 | + writer.write(""" |
| 194 | + $W |
| 195 | + func WithAPIOptions(optFns ...func($P) error) func(*Options) { |
| 196 | + return func (o *Options) { |
| 197 | + o.APIOptions = append(o.APIOptions, optFns...) |
| 198 | + } |
| 199 | + } |
| 200 | + """, |
| 201 | + goDocTemplate( |
| 202 | + "WithAPIOptions returns a functional option for setting the Client's APIOptions option." |
| 203 | + ), |
| 204 | + SmithyGoTypes.Middleware.Stack); |
| 205 | + |
| 206 | + fields.stream().filter(ConfigField::getWithHelper).filter(ConfigField::isDeprecated) |
| 207 | + .forEach(configField -> { |
| 208 | + writer.writeDocs(configField.getDeprecated().get()); |
| 209 | + writeHelper(writer, configField); |
| 210 | + }); |
| 211 | + |
| 212 | + fields.stream().filter(ConfigField::getWithHelper).filter(Predicate.not(ConfigField::isDeprecated)) |
| 213 | + .forEach(configField -> { |
| 214 | + writer.writeDocs( |
| 215 | + String.format( |
| 216 | + "With%s returns a functional option for setting the Client's %s option.", |
| 217 | + configField.getName(), configField.getName())); |
| 218 | + writeHelper(writer, configField); |
| 219 | + }); |
| 220 | + }; |
| 221 | + } |
| 222 | + |
| 223 | + private void writeHelper(GoWriter writer, ConfigField configField) { |
| 224 | + writer.write(""" |
| 225 | + func With$1L(v $2P) func(*Options) { |
| 226 | + return func(o *Options) { |
| 227 | + o.$1L = v |
| 228 | + } |
| 229 | + } |
| 230 | + """, configField.getName(), configField.getType()); |
| 231 | + } |
| 232 | + |
| 233 | + private void ensureSupportedProtocol() { |
| 234 | + if (!protocol.isHttpProtocol()) { |
| 235 | + throw new UnsupportedOperationException("Protocols other than HTTP are not yet implemented: " + protocol); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments