|
15 | 15 |
|
16 | 16 | package software.amazon.smithy.aws.typescript.codegen;
|
17 | 17 |
|
| 18 | +import java.util.ArrayList; |
18 | 19 | import java.util.Collections;
|
19 | 20 | import java.util.HashMap;
|
20 | 21 | import java.util.List;
|
21 | 22 | import java.util.Map;
|
| 23 | +import java.util.Optional; |
22 | 24 | import java.util.function.Consumer;
|
| 25 | +import java.util.stream.Collectors; |
23 | 26 | import software.amazon.smithy.aws.traits.clientendpointdiscovery.ClientDiscoveredEndpointTrait;
|
| 27 | +import software.amazon.smithy.aws.traits.clientendpointdiscovery.ClientEndpointDiscoveryIdTrait; |
24 | 28 | import software.amazon.smithy.aws.traits.clientendpointdiscovery.ClientEndpointDiscoveryTrait;
|
25 | 29 | import software.amazon.smithy.codegen.core.CodegenException;
|
26 | 30 | import software.amazon.smithy.codegen.core.Symbol;
|
27 | 31 | import software.amazon.smithy.codegen.core.SymbolProvider;
|
28 | 32 | import software.amazon.smithy.model.Model;
|
| 33 | +import software.amazon.smithy.model.knowledge.OperationIndex; |
| 34 | +import software.amazon.smithy.model.shapes.MemberShape; |
29 | 35 | import software.amazon.smithy.model.shapes.OperationShape;
|
30 | 36 | import software.amazon.smithy.model.shapes.ServiceShape;
|
31 | 37 | import software.amazon.smithy.model.shapes.ShapeId;
|
| 38 | +import software.amazon.smithy.model.shapes.StructureShape; |
32 | 39 | import software.amazon.smithy.typescript.codegen.LanguageTarget;
|
33 | 40 | import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
|
34 | 41 | import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
|
@@ -79,11 +86,7 @@ public List<RuntimeClientPlugin> getClientPlugins() {
|
79 | 86 | RuntimeClientPlugin.builder()
|
80 | 87 | .withConventions(AwsDependency.MIDDLEWARE_ENDPOINT_DISCOVERY.dependency,
|
81 | 88 | "EndpointDiscovery", RuntimeClientPlugin.Convention.HAS_MIDDLEWARE)
|
82 |
| - .additionalPluginFunctionParamsSupplier((m, s, o) -> new HashMap<String, Object>() {{ |
83 |
| - put("clientStack", Symbol.builder().name("clientStack").build()); |
84 |
| - put("options", Symbol.builder().name("options").build()); |
85 |
| - put("isDiscoveredEndpointRequired", isClientDiscoveredEndpointRequired(s, o)); |
86 |
| - }}) |
| 89 | + .additionalPluginFunctionParamsSupplier((m, s, o) -> getPluginFunctionParams(m, s, o)) |
87 | 90 | .operationPredicate((m, s, o) ->
|
88 | 91 | isClientDiscoveredEndpointRequired(s, o) || isClientDiscoveredEndpointOptional(s, o)
|
89 | 92 | ).build()
|
@@ -169,4 +172,49 @@ private static String getClientDiscoveryCommand(ServiceShape service) {
|
169 | 172 | }
|
170 | 173 | return service.getTrait(ClientEndpointDiscoveryTrait.class).orElse(null).getOperation().getName() + "Command";
|
171 | 174 | }
|
| 175 | + |
| 176 | + private static Map<String, Object> getPluginFunctionParams( |
| 177 | + Model model, |
| 178 | + ServiceShape serviceShape, |
| 179 | + OperationShape operationShape |
| 180 | + ) { |
| 181 | + Map<String, Object> params = new HashMap<String, Object>(); |
| 182 | + params.put("clientStack", Symbol.builder().name("clientStack").build()); |
| 183 | + params.put("options", Symbol.builder().name("options").build()); |
| 184 | + params.put("isDiscoveredEndpointRequired", isClientDiscoveredEndpointRequired( |
| 185 | + serviceShape, operationShape)); |
| 186 | + |
| 187 | + OperationIndex operationIndex = OperationIndex.of(model); |
| 188 | + List membersWithClientEndpointDiscoveryId = getMembersWithClientEndpointDiscoveryId( |
| 189 | + operationIndex.getInput(operationShape) |
| 190 | + ); |
| 191 | + |
| 192 | + if (!membersWithClientEndpointDiscoveryId.isEmpty()) { |
| 193 | + params.put("identifiers", getClientEndpointDiscoveryIdentifiers(membersWithClientEndpointDiscoveryId)); |
| 194 | + } |
| 195 | + return params; |
| 196 | + } |
| 197 | + |
| 198 | + private static String getClientEndpointDiscoveryIdentifiers( |
| 199 | + List<MemberShape> membersWithClientEndpointDiscoveryId |
| 200 | + ) { |
| 201 | + return membersWithClientEndpointDiscoveryId.stream() |
| 202 | + .map(member -> member.getMemberName() + ": input." + member.getMemberName()) |
| 203 | + .collect(Collectors.joining(", ", "{", "}")); |
| 204 | + } |
| 205 | + |
| 206 | + private static List<MemberShape> getMembersWithClientEndpointDiscoveryId( |
| 207 | + Optional<StructureShape> optionalShape |
| 208 | + ) { |
| 209 | + List<MemberShape> membersWithClientEndpointDiscoveryId = new ArrayList<>(); |
| 210 | + if (optionalShape.isPresent()) { |
| 211 | + StructureShape structureShape = optionalShape.get(); |
| 212 | + for (MemberShape member : structureShape.getAllMembers().values()) { |
| 213 | + if (member.getTrait(ClientEndpointDiscoveryIdTrait.class).isPresent()) { |
| 214 | + membersWithClientEndpointDiscoveryId.add(member); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + return membersWithClientEndpointDiscoveryId; |
| 219 | + } |
172 | 220 | }
|
0 commit comments