Skip to content

Commit d81f640

Browse files
committed
ONLY static cast when we are comparing to a collection.size()
1 parent 12edc66 commit d81f640

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

tools/code-generation/smithy/cpp-codegen/smithy-cpp-codegen/src/main/java/com/amazonaws/util/awsclientsmithygenerator/generators/waiters/jmespath/ComparatorEmitter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ public String visitComparator(ComparatorExpression expression) {
5454
case NOT_EQUAL: op = " != "; break;
5555
default: throw new UnsupportedOperationException("Unsupported comparator: " + expression.getComparator());
5656
}
57+
left = castCountIfForSizeComparison(left, right);
58+
right = castCountIfForSizeComparison(right, left);
5759
return "(" + left + op + right + ")";
5860
}
61+
62+
static String castCountIfForSizeComparison(String operand, String other) {
63+
if (operand.contains("std::count_if") && other.contains(".size()")) {
64+
return "static_cast<std::size_t>(" + operand + ")";
65+
}
66+
return operand;
67+
}
5968
}

tools/code-generation/smithy/cpp-codegen/smithy-cpp-codegen/src/main/java/com/amazonaws/util/awsclientsmithygenerator/generators/waiters/jmespath/FilterOperandEmitter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ private String emitItemCountIf(FilterProjectionExpression filterExpr) {
9898

9999
String predicate = filterExpr.getComparison().accept(
100100
new FilterPredicateEmitter("inner", model, innerElementShape, smithyServiceName));
101-
return "static_cast<std::size_t>(std::count_if(" + collection + ".begin(), " + collection + ".end(), "
102-
+ "[](const " + innerElementType + "& inner) { return " + predicate + "; }))";
101+
return "std::count_if(" + collection + ".begin(), " + collection + ".end(), "
102+
+ "[](const " + innerElementType + "& inner) { return " + predicate + "; })";
103103
}
104104

105105
private String resolveInnerElementType(JmespathExpression collectionExpr) {

tools/code-generation/smithy/cpp-codegen/smithy-cpp-codegen/src/main/java/com/amazonaws/util/awsclientsmithygenerator/generators/waiters/jmespath/FilterPredicateEmitter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public String visitComparator(ComparatorExpression expression) {
6767
case NOT_EQUAL: op = " != "; break;
6868
default: throw new UnsupportedOperationException("Unsupported comparator: " + expression.getComparator());
6969
}
70+
left = ComparatorEmitter.castCountIfForSizeComparison(left, right);
71+
right = ComparatorEmitter.castCountIfForSizeComparison(right, left);
7072
return "(" + left + op + right + ")";
7173
}
7274
}

tools/code-generation/smithy/cpp-codegen/smithy-cpp-codegen/src/main/java/com/amazonaws/util/awsclientsmithygenerator/generators/waiters/jmespath/ScalarEmitter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public String visitFunction(FunctionExpression expression) {
4343
if ("length".equals(expression.getName())) {
4444
JmespathExpression arg = expression.getArguments().get(0);
4545
if (arg instanceof FilterProjectionExpression) {
46-
return emitCountIf((FilterProjectionExpression) arg);
46+
return emitCountIf((FilterProjectionExpression) arg);
4747
}
4848
String collection = arg.accept(new CollectionGetterEmitter());
4949
return "result" + collection + ".size()";
@@ -65,8 +65,8 @@ private String emitCountIf(FilterProjectionExpression filterExpr) {
6565

6666
String predicate = filterExpr.getComparison().accept(
6767
new FilterPredicateEmitter("item", model, elementShape, smithyServiceName));
68-
return "static_cast<std::size_t>(std::count_if(" + collection + ".begin(), " + collection + ".end(), "
69-
+ "[](const " + elementType + "& item) { return " + predicate + "; }))";
68+
return "std::count_if(" + collection + ".begin(), " + collection + ".end(), "
69+
+ "[](const " + elementType + "& item) { return " + predicate + "; })";
7070
}
7171

7272
private String resolveFilterElementType(FilterProjectionExpression filterExpr) {

tools/code-generation/smithy/cpp-codegen/smithy-cpp-codegen/src/test/java/com/amazonaws/util/awsclientsmithygenerator/generators/waiters/WaiterJmesPathCppCodeGeneratorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ void filterProjection_notWithLengthAndFieldComparison() {
555555
String expr = "length(services[?!(length(deployments) == `1` && runningCount == desiredCount)]) == `0`";
556556
String code = gen(expr, PathComparator.BOOLEAN_EQUALS);
557557
assertContains(code, "std::count_if");
558+
assertFalse(code.contains("static_cast"), "Should not cast when comparing against literal:\n" + code);
558559
assertContains(code, "item.GetDeployments().size() == 1");
559560
assertContains(code, "item.GetRunningCount() == item.GetDesiredCount()");
560561
assertContains(code, "!");

0 commit comments

Comments
 (0)