Skip to content

Commit c2eb23f

Browse files
committed
feat(clients): allow protocol selection by class constructor
# Conflicts: # codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddProtocolConfig.java
1 parent 7e8a34e commit c2eb23f

File tree

1 file changed

+53
-78
lines changed
  • codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen

1 file changed

+53
-78
lines changed

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

Lines changed: 53 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,34 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
109109
.orElse("");
110110
String awsQueryCompat = settings.getService(model).hasTrait(AwsQueryCompatibleTrait.class) ? "true" : "false";
111111

112+
Consumer<TypeScriptWriter> typeScriptWriterConsumer = (TypeScriptWriter writer) -> {
113+
writer.openBlock(
114+
"""
115+
{
116+
defaultNamespace: $S,""",
117+
"""
118+
}""",
119+
namespace,
120+
() -> {
121+
if (!xmlns.isEmpty()) {
122+
writer.write("xmlNamespace: $S,", xmlns);
123+
}
124+
String version = settings.getService(model).getVersion();
125+
if (!version.isEmpty()) {
126+
writer.write("version: $S,", version);
127+
}
128+
writer.write("serviceTarget: $S,", rpcTarget);
129+
if (awsQueryCompat.equals("true")) {
130+
writer.write("awsQueryCompat: $L,", awsQueryCompat);
131+
}
132+
133+
if (CUSTOMIZATIONS.containsKey(settings.getService())) {
134+
CUSTOMIZATIONS.get(settings.getService()).accept(writer);
135+
}
136+
}
137+
);
138+
};
139+
112140
switch (target) {
113141
case SHARED:
114142
if (Objects.equals(settings.getProtocol(), RestXmlTrait.ID)) {
@@ -121,16 +149,10 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
121149
AwsDependency.AWS_SDK_CORE,
122150
"/protocols"
123151
);
124-
writer.write(
125-
"""
126-
new AwsRestXmlProtocol({
127-
defaultNamespace: $S,
128-
xmlNamespace: $S,
129-
})""",
130-
namespace,
131-
xmlns
132-
);
133-
}
152+
writer.write("AwsRestXmlProtocol");
153+
},
154+
"protocolSettings",
155+
typeScriptWriterConsumer
134156
);
135157
} else if (Objects.equals(settings.getProtocol(), AwsQueryTrait.ID)) {
136158
return MapUtils.of(
@@ -142,18 +164,10 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
142164
AwsDependency.AWS_SDK_CORE,
143165
"/protocols"
144166
);
145-
writer.write(
146-
"""
147-
new AwsQueryProtocol({
148-
defaultNamespace: $S,
149-
xmlNamespace: $S,
150-
version: $S,
151-
})""",
152-
namespace,
153-
xmlns,
154-
settings.getService(model).getVersion()
155-
);
156-
}
167+
writer.write("AwsQueryProtocol");
168+
},
169+
"protocolSettings",
170+
typeScriptWriterConsumer
157171
);
158172
} else if (Objects.equals(settings.getProtocol(), Ec2QueryTrait.ID)) {
159173
return MapUtils.of(
@@ -165,18 +179,10 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
165179
AwsDependency.AWS_SDK_CORE,
166180
"/protocols"
167181
);
168-
writer.write(
169-
"""
170-
new AwsEc2QueryProtocol({
171-
defaultNamespace: $S,
172-
xmlNamespace: $S,
173-
version: $S,
174-
})""",
175-
namespace,
176-
xmlns,
177-
settings.getService(model).getVersion()
178-
);
179-
}
182+
writer.write("AwsEc2QueryProtocol");
183+
},
184+
"protocolSettings",
185+
typeScriptWriterConsumer
180186
);
181187
} else if (Objects.equals(settings.getProtocol(), RestJson1Trait.ID)) {
182188
return MapUtils.of(
@@ -188,7 +194,7 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
188194
AwsDependency.AWS_SDK_CORE,
189195
"/protocols"
190196
);
191-
writer.write("new AwsRestJsonProtocol({ defaultNamespace: $S })", namespace);
197+
writer.write("AwsRestJsonProtocol");
192198
}
193199
);
194200
} else if (Objects.equals(settings.getProtocol(), AwsJson1_0Trait.ID)) {
@@ -201,24 +207,10 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
201207
AwsDependency.AWS_SDK_CORE,
202208
"/protocols"
203209
);
204-
writer.openBlock(
205-
"""
206-
new AwsJson1_0Protocol({
207-
defaultNamespace: $S,
208-
serviceTarget: $S,
209-
awsQueryCompatible: $L,""",
210-
"""
211-
})""",
212-
namespace,
213-
rpcTarget,
214-
awsQueryCompat,
215-
() -> {
216-
if (CUSTOMIZATIONS.containsKey(settings.getService())) {
217-
CUSTOMIZATIONS.get(settings.getService()).accept(writer);
218-
}
219-
}
220-
);
221-
}
210+
writer.write("AwsJson1_0Protocol");
211+
},
212+
"protocolSettings",
213+
typeScriptWriterConsumer
222214
);
223215
} else if (Objects.equals(settings.getProtocol(), AwsJson1_1Trait.ID)) {
224216
return MapUtils.of(
@@ -230,24 +222,10 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
230222
AwsDependency.AWS_SDK_CORE,
231223
"/protocols"
232224
);
233-
writer.openBlock(
234-
"""
235-
new AwsJson1_1Protocol({
236-
defaultNamespace: $S,
237-
serviceTarget: $S,
238-
awsQueryCompatible: $L,""",
239-
"""
240-
})""",
241-
namespace,
242-
rpcTarget,
243-
awsQueryCompat,
244-
() -> {
245-
if (CUSTOMIZATIONS.containsKey(settings.getService())) {
246-
CUSTOMIZATIONS.get(settings.getService()).accept(writer);
247-
}
248-
}
249-
);
250-
}
225+
writer.write("AwsJson1_1Protocol");
226+
},
227+
"protocolSettings",
228+
typeScriptWriterConsumer
251229
);
252230
} else if (Objects.equals(settings.getProtocol(), Rpcv2CborTrait.ID)) {
253231
return MapUtils.of(
@@ -261,14 +239,11 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
261239
);
262240
writer.write(
263241
"""
264-
new AwsSmithyRpcV2CborProtocol({
265-
defaultNamespace: $S,
266-
awsQueryCompatible: $L,
267-
})""",
268-
namespace,
269-
awsQueryCompat
242+
AwsSmithyRpcV2CborProtocol"""
270243
);
271-
}
244+
},
245+
"protocolSettings",
246+
typeScriptWriterConsumer
272247
);
273248
}
274249
case BROWSER:

0 commit comments

Comments
 (0)