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

Adds resolver function for loading S3 client specific config #991

Merged
merged 4 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,23 @@ private void writeAwsConfigConstructor(Model model, ServiceShape service, GoWrit
}
});

for (AwsConfigField field : AWS_CONFIG_FIELDS) {

List<AwsConfigField> configFields = new ArrayList<>(AWS_CONFIG_FIELDS);
// add client specific config fields
for (AwsConfigField cfgField: ResolveClientConfig.AWS_CONFIG_FIELDS) {
configFields.add(cfgField);
}

for (AwsConfigField field : configFields) {
Optional<Symbol> awsResolverFunction = field.getAwsResolverFunction();
if (!awsResolverFunction.isPresent()) {
continue;
}
if (field.getServicePredicate().isPresent()) {
if (!field.getServicePredicate().get().test(model, service)) {
continue;
}
}
writer.write("$L(cfg, &opts)", awsResolverFunction.get());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class AwsGoDependency {
public static final GoDependency AWS_HTTP_TRANSPORT = aws("aws/transport/http", "awshttp");
public static final GoDependency AWSTESTING_UNIT = aws("internal/awstesting/unit");

public static final GoDependency S3_SHARED_CONFIG = aws("service/internal/s3shared/config", "s3sharedconfig");

public static final GoDependency REGEXP = SmithyGoDependency.stdlib("regexp");

public static final String AWS_SOURCE_PATH = "github.com/aws/aws-sdk-go-v2";
Expand Down Expand Up @@ -82,6 +84,6 @@ protected static GoDependency module(
}

private static final class Versions {
private static final String AWS_SDK = "v0.30.1-0.20201217001905-4acf9c65b2d1";
private static final String AWS_SDK = "v0.30.1-0.20201221101722-677dd4a81dad";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package software.amazon.smithy.aws.go.codegen;

import java.util.List;
import java.util.logging.Logger;
import software.amazon.smithy.aws.traits.ServiceTrait;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.go.codegen.GoDelegator;
import software.amazon.smithy.go.codegen.GoSettings;
import software.amazon.smithy.go.codegen.SmithyGoDependency;
import software.amazon.smithy.go.codegen.SymbolUtils;
import software.amazon.smithy.go.codegen.integration.GoIntegration;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.utils.ListUtils;

/**
* Registers additional client specific configuration fields
*/
public class ResolveClientConfig implements GoIntegration {
jasdel marked this conversation as resolved.
Show resolved Hide resolved
private static final Logger LOGGER = Logger.getLogger(AddAwsConfigFields.class.getName());

private static final String USE_ARN_REGION_OPTION = "UseARNRegion";
private static final String CLIENT_CONFIG_RESOLVER_FUNC = "resolveClientConfig";
private static final String CONFIG_SOURCE_CONFIG_NAME = "ConfigSources";
private static final String RESOLVE_USE_ARN_REGION= "ResolveUseARNRegion";

public static final List<AddAwsConfigFields.AwsConfigField> AWS_CONFIG_FIELDS = ListUtils.of(
AddAwsConfigFields.AwsConfigField.builder()
.name(USE_ARN_REGION_OPTION)
.type(getUniversalSymbol("boolean"))
.generatedOnClient(false)
.servicePredicate((model, serviceShape) -> {
return isS3SharedService(model, serviceShape);
})
.awsResolveFunction(SymbolUtils.createValueSymbolBuilder(CLIENT_CONFIG_RESOLVER_FUNC)
.build())
.build()
);

@Override
public void writeAdditionalFiles(
GoSettings settings,
Model model,
SymbolProvider symbolProvider,
GoDelegator goDelegator
) {
LOGGER.info("generating client config resolver");
ServiceShape serviceShape = settings.getService(model);
goDelegator.useShapeWriter(serviceShape, writer -> {
if (!isS3SharedService(model, serviceShape)) {
return;
}

writer.writeDocs("resolves client config");
writer.addUseImports(AwsGoDependency.AWS_CORE);
writer.openBlock("func $L(cfg aws.Config, o *Options) error {", "}",
CLIENT_CONFIG_RESOLVER_FUNC, () -> {
writer.openBlock("if cfg.$L == nil || len(cfg.$L) == 0 {", "}",
CONFIG_SOURCE_CONFIG_NAME, CONFIG_SOURCE_CONFIG_NAME,
() -> writer.write("return nil")
);

writer.addUseImports(SmithyGoDependency.CONTEXT);
Symbol resolverFunc = SymbolUtils.createValueSymbolBuilder(RESOLVE_USE_ARN_REGION,
AwsGoDependency.S3_SHARED_CONFIG).build();
writer.write("value, found, err := $T(context.Background(), cfg.$L)", resolverFunc,
CONFIG_SOURCE_CONFIG_NAME);
writer.write("if err != nil { return err }");
writer.write("if found { o.$L = value }", USE_ARN_REGION_OPTION);

writer.write("return nil");
});

});
}


private static Symbol getUniversalSymbol(String symbolName) {
return SymbolUtils.createValueSymbolBuilder(symbolName)
.putProperty(SymbolUtils.GO_UNIVERSE_TYPE, true).build();
}

private static boolean isS3SharedService(Model model, ServiceShape service) {
return isS3Service(model, service) || isS3ControlService(model, service);
}

private static boolean isS3Service(Model model, ServiceShape service) {
return service.expectTrait(ServiceTrait.class).getSdkId().equalsIgnoreCase("S3");
}

private static boolean isS3ControlService(Model model, ServiceShape service) {
return service.expectTrait(ServiceTrait.class).getSdkId().equalsIgnoreCase("S3 Control");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ software.amazon.smithy.aws.go.codegen.FilterStreamingOperations
software.amazon.smithy.aws.go.codegen.RequestResponseLogging
software.amazon.smithy.aws.go.codegen.customization.S3ControlEndpointResolver
software.amazon.smithy.aws.go.codegen.AwsHttpPresignURLClientGenerator
software.amazon.smithy.aws.go.codegen.ResolveClientConfig
17 changes: 17 additions & 0 deletions service/s3/api_client.go

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

17 changes: 17 additions & 0 deletions service/s3control/api_client.go

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