1 /*
2  * Copyright 2020 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.aws.go.codegen;
17 
18 import software.amazon.smithy.aws.traits.ServiceTrait;
19 import software.amazon.smithy.codegen.core.SymbolProvider;
20 import software.amazon.smithy.go.codegen.CodegenUtils;
21 import software.amazon.smithy.go.codegen.GoDelegator;
22 import software.amazon.smithy.go.codegen.GoSettings;
23 import software.amazon.smithy.go.codegen.SmithyGoDependency;
24 import software.amazon.smithy.go.codegen.SymbolUtils;
25 import software.amazon.smithy.go.codegen.integration.GoIntegration;
26 import software.amazon.smithy.model.Model;
27 
28 public class AwsClientUserAgent implements GoIntegration {
29     public static final String MIDDLEWARE_RESOLVER = "addClientUserAgent";
30 
31     @Override
getOrder()32     public byte getOrder() {
33         return -49;
34     }
35 
36     @Override
writeAdditionalFiles( GoSettings settings, Model model, SymbolProvider symbolProvider, GoDelegator goDelegator )37     public void writeAdditionalFiles(
38             GoSettings settings,
39             Model model,
40             SymbolProvider symbolProvider,
41             GoDelegator goDelegator
42     ) {
43         ServiceTrait serviceTrait = settings.getService(model).expectTrait(ServiceTrait.class);
44 
45         goDelegator.useShapeWriter(settings.getService(model), writer -> {
46             writer.openBlock("func $L(stack $P) error {", "}", MIDDLEWARE_RESOLVER, SymbolUtils.createPointableSymbolBuilder("Stack",
47                     SmithyGoDependency.SMITHY_MIDDLEWARE).build(), () -> {
48                 // TODO: This should include the module version
49                 writer.write("return $T(stack)", SymbolUtils.createValueSymbolBuilder("AddRequestUserAgentMiddleware",
50                         AwsGoDependency.AWS_MIDDLEWARE).build());
51             });
52             writer.write("");
53         });
54     }
55 }
56