Skip to content

Commit 4d085af

Browse files
authored
chore(codegen): populate identifiers for endpoint discovery (#2539)
1 parent 275876a commit 4d085af

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddEndpointDiscoveryPlugin.java

+53-5
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,27 @@
1515

1616
package software.amazon.smithy.aws.typescript.codegen;
1717

18+
import java.util.ArrayList;
1819
import java.util.Collections;
1920
import java.util.HashMap;
2021
import java.util.List;
2122
import java.util.Map;
23+
import java.util.Optional;
2224
import java.util.function.Consumer;
25+
import java.util.stream.Collectors;
2326
import software.amazon.smithy.aws.traits.clientendpointdiscovery.ClientDiscoveredEndpointTrait;
27+
import software.amazon.smithy.aws.traits.clientendpointdiscovery.ClientEndpointDiscoveryIdTrait;
2428
import software.amazon.smithy.aws.traits.clientendpointdiscovery.ClientEndpointDiscoveryTrait;
2529
import software.amazon.smithy.codegen.core.CodegenException;
2630
import software.amazon.smithy.codegen.core.Symbol;
2731
import software.amazon.smithy.codegen.core.SymbolProvider;
2832
import software.amazon.smithy.model.Model;
33+
import software.amazon.smithy.model.knowledge.OperationIndex;
34+
import software.amazon.smithy.model.shapes.MemberShape;
2935
import software.amazon.smithy.model.shapes.OperationShape;
3036
import software.amazon.smithy.model.shapes.ServiceShape;
3137
import software.amazon.smithy.model.shapes.ShapeId;
38+
import software.amazon.smithy.model.shapes.StructureShape;
3239
import software.amazon.smithy.typescript.codegen.LanguageTarget;
3340
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
3441
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
@@ -79,11 +86,7 @@ public List<RuntimeClientPlugin> getClientPlugins() {
7986
RuntimeClientPlugin.builder()
8087
.withConventions(AwsDependency.MIDDLEWARE_ENDPOINT_DISCOVERY.dependency,
8188
"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))
8790
.operationPredicate((m, s, o) ->
8891
isClientDiscoveredEndpointRequired(s, o) || isClientDiscoveredEndpointOptional(s, o)
8992
).build()
@@ -169,4 +172,49 @@ private static String getClientDiscoveryCommand(ServiceShape service) {
169172
}
170173
return service.getTrait(ClientEndpointDiscoveryTrait.class).orElse(null).getOperation().getName() + "Command";
171174
}
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+
}
172220
}

0 commit comments

Comments
 (0)