Skip to content

Commit e4d16e5

Browse files
committed
chore(reducers): reduce object copying in iterators
1 parent 92ddfc1 commit e4d16e5

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructuredMemberWriter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,13 @@ private void writeMapFilterSensitiveLog(TypeScriptWriter writer, MemberShape map
220220
String valueParam = "value"; // value of the Object.entries() key-value pair
221221

222222
// Reducer is common to all shapes.
223-
writer.openBlock("Object.entries($L).reduce(($L: any, [$L, $L]: [string, $T]) => ({", "}), {})",
223+
writer.openBlock("Object.entries($L).reduce(($L: any, [$L, $L]: [string, $T]) => (", "), {})",
224224
mapParam, accParam, keyParam, valueParam, symbolProvider.toSymbol(mapMember), () -> {
225-
writer.write("...$L,", accParam);
226-
writer.openBlock("[$L]: ", ",", keyParam, () -> {
225+
writer.openBlock("$L[$L] =", "", accParam, keyParam, () -> {
227226
writeMemberFilterSensitiveLog(writer, mapMember, valueParam);
227+
writer.writeInline(",");
228228
});
229+
writer.write(accParam);
229230
}
230231
);
231232
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -981,15 +981,15 @@ private void writePrefixHeaders(GenerationContext context, HttpBinding binding)
981981
// Iterate through each entry in the member.
982982
writer.openBlock("...($1L !== undefined) && Object.keys($1L).reduce(", "),", memberLocation,
983983
() -> {
984-
writer.openBlock("(acc: any, suffix: string) => ({", "}), {}",
984+
writer.openBlock("(acc: any, suffix: string) => {", "}, {}",
985985
() -> {
986986
// Use a ! since we already validated the input member is defined above.
987987
String headerValue = getInputValue(context, binding.getLocation(),
988988
memberLocation + "![suffix]", binding.getMember(), target);
989-
writer.write("...acc,");
990989
// Append the prefix to key.
991-
writer.write("[`$L$${suffix.toLowerCase()}`]: $L,",
990+
writer.write("acc[`$L$${suffix.toLowerCase()}`] = $L;",
992991
binding.getLocationName().toLowerCase(Locale.US), headerValue);
992+
writer.write("return acc;");
993993
});
994994
}
995995
);
@@ -1348,10 +1348,10 @@ private String getMapInputParam(
13481348
String valueString = getInputValue(context, bindingType, "value", mapMember,
13491349
model.expectShape(mapMember.getTarget()));
13501350
return "Object.entries(" + dataSource + " || {}).reduce("
1351-
+ "(acc: any, [key, value]: [string, " + symbolProvider.toSymbol(mapMember) + "]) => ({"
1352-
+ "...acc,"
1353-
+ "[key]: " + valueString + ","
1354-
+ "}), {})";
1351+
+ "(acc: any, [key, value]: [string, " + symbolProvider.toSymbol(mapMember) + "]) => {"
1352+
+ "acc[key] = " + valueString + ";"
1353+
+ "return acc;"
1354+
+ "}, {})";
13551355
}
13561356

13571357
/**

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,12 @@ public void callsFilterForMapWithStructureWithSensitiveData() {
418418
"export const GetFooInputFilterSensitiveLog = (obj: GetFooInput): any => ({\n"
419419
+ " ...obj,\n"
420420
+ " ...(obj.foo && { foo:\n"
421-
+ " Object.entries(obj.foo).reduce((acc: any, [key, value]: [string, User]) => ({\n"
422-
+ " ...acc,\n"
423-
+ " [key]:\n"
421+
+ " Object.entries(obj.foo).reduce((acc: any, [key, value]: [string, User]) => (\n"
422+
+ " acc[key] =\n"
424423
+ " UserFilterSensitiveLog(value)\n"
425-
+ " ,\n"
426-
+ " }), {})\n"
424+
+ " ,\n"
425+
+ " acc\n"
426+
+ " ), {})\n"
427427
+ " }),\n"
428428
+ "})");
429429
}
@@ -445,12 +445,12 @@ public void callsFilterForMapWithUnionWithSensitiveData() {
445445
"export const GetFooInputFilterSensitiveLog = (obj: GetFooInput): any => ({\n"
446446
+ " ...obj,\n"
447447
+ " ...(obj.foo && { foo:\n"
448-
+ " Object.entries(obj.foo).reduce((acc: any, [key, value]: [string, TestUnion]) => ({\n"
449-
+ " ...acc,\n"
450-
+ " [key]:\n"
448+
+ " Object.entries(obj.foo).reduce((acc: any, [key, value]: [string, TestUnion]) => (\n"
449+
+ " acc[key] =\n"
451450
+ " TestUnionFilterSensitiveLog(value)\n"
452-
+ " ,\n"
453-
+ " }), {})\n"
451+
+ " ,\n"
452+
+ " acc\n"
453+
+ " ), {})\n"
454454
+ " }),\n"
455455
+ "})");
456456
}

0 commit comments

Comments
 (0)