Skip to content

Commit 5a4e5bb

Browse files
authored
add feature tracking for cbor protocol (#2821)
1 parent 183987c commit 5a4e5bb

17 files changed

+139
-0
lines changed

aws/middleware/user_agent.go

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const (
8585
UserAgentFeatureS3ExpressBucket = "J"
8686
UserAgentFeatureS3AccessGrants = "K" // not yet implemented
8787
UserAgentFeatureGZIPRequestCompression = "L"
88+
UserAgentFeatureProtocolRPCV2CBOR = "M"
8889
)
8990

9091
// RequestUserAgent is a build middleware that set the User-Agent for the request.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package software.amazon.smithy.aws.go.codegen.customization;
2+
3+
import static software.amazon.smithy.go.codegen.GoWriter.goTemplate;
4+
import static software.amazon.smithy.go.codegen.SymbolUtils.buildPackageSymbol;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.function.BiPredicate;
9+
import software.amazon.smithy.aws.go.codegen.AwsGoDependency;
10+
import software.amazon.smithy.codegen.core.Symbol;
11+
import software.amazon.smithy.go.codegen.GoCodegenContext;
12+
import software.amazon.smithy.go.codegen.GoWriter;
13+
import software.amazon.smithy.go.codegen.SmithyGoTypes;
14+
import software.amazon.smithy.go.codegen.integration.GoIntegration;
15+
import software.amazon.smithy.go.codegen.integration.MiddlewareRegistrar;
16+
import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin;
17+
import software.amazon.smithy.model.Model;
18+
import software.amazon.smithy.model.shapes.ServiceShape;
19+
import software.amazon.smithy.protocol.traits.Rpcv2CborTrait;
20+
21+
/**
22+
* Adds user agent tracking for basic features - i.e. simple model-based ones that do not require any additional in-code
23+
* checks, such as a particular protocol.
24+
*/
25+
public class BasicUserAgentFeatures implements GoIntegration {
26+
private static final List<Feature> FEATURES = List.of(
27+
new Feature("ProtocolRPCV2CBOR", (model, service) -> service.hasTrait(Rpcv2CborTrait.class))
28+
);
29+
30+
@Override
31+
public List<RuntimeClientPlugin> getClientPlugins() {
32+
return FEATURES.stream().map(Feature::getPlugin).toList();
33+
}
34+
35+
@Override
36+
public void writeAdditionalFiles(GoCodegenContext ctx) {
37+
var model = ctx.model();
38+
var service = ctx.settings().getService(model);
39+
ctx.writerDelegator().useFileWriter("api_client.go", ctx.settings().getModuleName(),
40+
GoWriter.ChainWritable.of(
41+
FEATURES.stream()
42+
.filter(it -> it.servicePredicate.test(model, service))
43+
.map(Feature::getAddMiddleware)
44+
.toList()
45+
).compose());
46+
}
47+
48+
private static final class Feature {
49+
public final Symbol featureId;
50+
public final BiPredicate<Model, ServiceShape> servicePredicate;
51+
52+
public Feature(String id, BiPredicate<Model, ServiceShape> servicePredicate) {
53+
this.featureId = AwsGoDependency.AWS_MIDDLEWARE.constSymbol("UserAgentFeature" + id);
54+
this.servicePredicate = servicePredicate;
55+
}
56+
57+
public RuntimeClientPlugin getPlugin() {
58+
return RuntimeClientPlugin.builder()
59+
.servicePredicate(servicePredicate)
60+
.registerMiddleware(
61+
MiddlewareRegistrar.builder()
62+
.resolvedFunction(buildPackageSymbol("add" + featureId.getName()))
63+
.useClientOptions()
64+
.build()
65+
)
66+
.build();
67+
}
68+
69+
public GoWriter.Writable getAddMiddleware() {
70+
return goTemplate("""
71+
func add$featureName:L(stack $stack:P, options Options) error {
72+
ua, err := getOrAddRequestUserAgent(stack)
73+
if err != nil {
74+
return err
75+
}
76+
77+
ua.AddUserAgentFeature($featureEnum:T)
78+
return nil
79+
}
80+
""",
81+
Map.of(
82+
"stack", SmithyGoTypes.Middleware.Stack,
83+
"featureName", featureId.getName(),
84+
"featureEnum", featureId
85+
));
86+
}
87+
}
88+
}

codegen/smithy-aws-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration

+1
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,4 @@ software.amazon.smithy.aws.go.codegen.customization.RequestCompressionUserAgent
8686
software.amazon.smithy.aws.go.codegen.customization.s3.ExpressUserAgent
8787
software.amazon.smithy.aws.go.codegen.customization.BackfillRequiredTrait
8888
software.amazon.smithy.aws.go.codegen.customization.DeprecateService
89+
software.amazon.smithy.aws.go.codegen.customization.BasicUserAgentFeatures

internal/protocoltest/smithyrpcv2cbor/api_client.go

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_EmptyInputOutput.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_Float16.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_FractionalSeconds.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_GreetingWithErrors.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_NoInputOutput.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_OperationWithDefaults.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_OptionalInputOutput.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_RecursiveShapes.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_RpcV2CborDenseMaps.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_RpcV2CborLists.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_RpcV2CborSparseMaps.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_SimpleScalarProperties.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/protocoltest/smithyrpcv2cbor/api_op_SparseNullsOperation.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)