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 java.util.List;
19 import software.amazon.smithy.codegen.core.Symbol;
20 import software.amazon.smithy.codegen.core.SymbolProvider;
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.go.codegen.integration.MiddlewareRegistrar;
27 import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin;
28 import software.amazon.smithy.model.Model;
29 import software.amazon.smithy.utils.ListUtils;
30 
31 public class RequestResponseLogging implements GoIntegration {
32     private final static String MIDDLEWARE_HELPER = "addRequestResponseLogging";
33 
34     @Override
getOrder()35     public byte getOrder() {
36         return 127;
37     }
38 
39     @Override
writeAdditionalFiles( GoSettings settings, Model model, SymbolProvider symbolProvider, GoDelegator goDelegator )40     public void writeAdditionalFiles(
41             GoSettings settings,
42             Model model,
43             SymbolProvider symbolProvider,
44             GoDelegator goDelegator
45     ) {
46         goDelegator.useShapeWriter(settings.getService(model), writer -> {
47             Symbol stackSymbol = SymbolUtils.createPointableSymbolBuilder("Stack", SmithyGoDependency.SMITHY_MIDDLEWARE)
48                     .build();
49             Symbol middlewareSymbol = SymbolUtils.createValueSymbolBuilder("RequestResponseLogger",
50                     SmithyGoDependency.SMITHY_HTTP_TRANSPORT).build();
51 
52             writer.openBlock("func $L(stack $P, o Options) error {", "}", MIDDLEWARE_HELPER, stackSymbol, () -> {
53                 writer.openBlock("return stack.Deserialize.Add(&$T{", "}, middleware.After)", middlewareSymbol, () -> {
54                     writer.write("LogRequest: o.$L.IsRequest(),", AddAwsConfigFields.LOG_MODE_CONFIG_NAME);
55                     writer.write("LogRequestWithBody: o.$L.IsRequestWithBody(),",
56                             AddAwsConfigFields.LOG_MODE_CONFIG_NAME);
57                     writer.write("LogResponse: o.$L.IsResponse(),", AddAwsConfigFields.LOG_MODE_CONFIG_NAME);
58                     writer.write("LogResponseWithBody: o.$L.IsResponseWithBody(),",
59                             AddAwsConfigFields.LOG_MODE_CONFIG_NAME);
60                 });
61             });
62         });
63     }
64 
65     @Override
getClientPlugins()66     public List<RuntimeClientPlugin> getClientPlugins() {
67         return ListUtils.of(RuntimeClientPlugin.builder()
68                 .registerMiddleware(MiddlewareRegistrar.builder()
69                         .resolvedFunction(SymbolUtils.createValueSymbolBuilder(MIDDLEWARE_HELPER).build())
70                         .useClientOptions()
71                         .build())
72                 .build());
73     }
74 }
75