Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic to handle number acceptors #5813

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.stree.JacksonJrsTreeCodec;
import com.fasterxml.jackson.jr.stree.JrSimpleTreeExtension;
import com.fasterxml.jackson.jr.stree.JrsValue;
import java.io.File;
import java.io.IOException;
Expand All @@ -36,7 +36,10 @@ public final class Jackson {
.disable(JSON.Feature.FAIL_ON_UNKNOWN_BEAN_PROPERTY)
.enable(JSON.Feature.PRETTY_PRINT_OUTPUT)
.enable(JSON.Feature.READ_JSON_ARRAYS_AS_JAVA_ARRAYS)
.treeCodec(new JacksonJrsTreeCodec());
// enable BigDecimal Support
.enable(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS)
.register(new JrSimpleTreeExtension());
RanVaknin marked this conversation as resolved.
Show resolved Hide resolved


mapperBuilder.streamFactory().enable(JsonParser.Feature.ALLOW_COMMENTS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import com.squareup.javapoet.WildcardTypeName;
import java.math.BigDecimal;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Comparator;
Expand Down Expand Up @@ -557,15 +558,30 @@ private String waiterState(Acceptor acceptor) {

private CodeBlock pathAcceptorBody(Acceptor acceptor) {
String expected = acceptor.getExpected().asText();
String expectedType = acceptor.getExpected() instanceof JrsString ? "$S" : "$L";
return CodeBlock.builder()
boolean isString = acceptor.getExpected() instanceof JrsString;
boolean isNumber = acceptor.getExpected().isNumber();

CodeBlock.Builder block = CodeBlock.builder();
if (isNumber) {
block.add("new $T($S)", BigDecimal.class, expected);
} else if (isString) {
block.add("$S", expected);
} else {
block.add("$L", expected);
}
CodeBlock rhs = block.build();

CodeBlock.Builder builder = CodeBlock.builder()
.add("response -> {")
.add("$1T input = new $1T(response);", poetExtensions.jmesPathRuntimeClass().nestedClass("Value"))
.add("return $T.equals(", Objects.class)
.add(jmesPathAcceptorGenerator.interpret(acceptor.getArgument(), "input"))
.add(".value(), " + expectedType + ");", expected)
.add("}")
.build();
.add(".value(), ")
.add(rhs)
.add(");")
.add("}");

return builder.build();
}

private CodeBlock pathAllAcceptorBody(Acceptor acceptor) {
Expand All @@ -586,18 +602,31 @@ private CodeBlock pathAllAcceptorBody(Acceptor acceptor) {

private CodeBlock pathAnyAcceptorBody(Acceptor acceptor) {
String expected = acceptor.getExpected().asText();
String expectedType = acceptor.getExpected() instanceof JrsString ? "$S" : "$L";
return CodeBlock.builder()
.add("response -> {")
.add("$1T input = new $1T(response);", poetExtensions.jmesPathRuntimeClass().nestedClass("Value"))
.add("$T<$T> resultValues = ", List.class, Object.class)
.add(jmesPathAcceptorGenerator.interpret(acceptor.getArgument(), "input"))
.add(".values();")
.add("return !resultValues.isEmpty() && "
+ "resultValues.stream().anyMatch(v -> $T.equals(v, " + expectedType + "));",
Objects.class, expected)
.add("}")
.build();
boolean isString = acceptor.getExpected() instanceof JrsString;
boolean isNumber = acceptor.getExpected().isNumber();

CodeBlock.Builder builder = CodeBlock.builder()
.add("response -> {")
.add("$1T input = new $1T(response);", poetExtensions.jmesPathRuntimeClass().nestedClass("Value"))
.add("$T<$T> resultValues = ", List.class, Object.class)
.add(jmesPathAcceptorGenerator.interpret(acceptor.getArgument(), "input"))
.add(".values();")
.add("return !resultValues.isEmpty() &&"
+ " resultValues.stream().anyMatch(v " + "-> $T.equals"
+ "(v, ", Objects.class);

if (isNumber) {
builder.add("new $T($S)", BigDecimal.class, expected);
} else if (isString) {
builder.add("$S", expected);
} else {
builder.add("$L", expected);
}

builder.add("));")
.add("}");

return builder.build();
}

private CodeBlock trueForAllResponse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.jr.stree.JrsValue;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import java.math.BigDecimal;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;
Expand Down Expand Up @@ -57,6 +58,7 @@
* this interpreter make heavy use of the {@code JmesPathRuntime}.
*/
public class JmesPathAcceptorGenerator {
private static final ClassName BIG_DECIMAL = ClassName.get(BigDecimal.class);
private final ClassName runtimeClass;

public JmesPathAcceptorGenerator(ClassName runtimeClass) {
Expand Down Expand Up @@ -273,7 +275,7 @@ public void visitRawString(String input) {
public void visitLiteral(Literal input) {
JrsValue jsonValue = input.jsonValue();
if (jsonValue.isNumber()) {
codeBlock.add(".constant($L)", Integer.parseInt(jsonValue.asText()));
codeBlock.add(".constant(new $T($S))", BIG_DECIMAL, jsonValue.asText());
} else if (jsonValue instanceof JrsBoolean) {
codeBlock.add(".constant($L)", ((JrsBoolean) jsonValue).booleanValue());
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -47,9 +48,9 @@ public final class JmesPathRuntime {
private SdkPojo pojoValue;

/**
* The value if this is an {@link Type#INTEGER} (or null otherwise).
* The value if this is an {@link Type#NUMBER} (or null otherwise).
*/
private Integer integerValue;
private BigDecimal numberValue;

/**
* The value if this is an {@link Type#STRING} (or null otherwise).
Expand Down Expand Up @@ -105,9 +106,25 @@ public final class JmesPathRuntime {
} else if (value instanceof String) {
this.type = Type.STRING;
this.stringValue = (String) value;
} else if (value instanceof Integer) {
this.type = Type.INTEGER;
this.integerValue = (Integer) value;
} else if (value instanceof Number) {
this.type = Type.NUMBER;
if (value instanceof BigDecimal) {
this.numberValue = (BigDecimal) value;
} else if (value instanceof Double) {
this.numberValue = BigDecimal.valueOf((Double) value);
} else if (value instanceof Float) {
this.numberValue = BigDecimal.valueOf((Float) value);
} else if (value instanceof Long) {
this.numberValue = BigDecimal.valueOf((Long) value);
} else if (value instanceof Integer) {
this.numberValue = BigDecimal.valueOf((Integer) value);
} else if (value instanceof Short) {
this.numberValue = BigDecimal.valueOf((Short) value);
} else if (value instanceof Byte) {
this.numberValue = BigDecimal.valueOf((Byte) value);
} else {
this.numberValue = new BigDecimal(value.toString());
RanVaknin marked this conversation as resolved.
Show resolved Hide resolved
}
} else if (value instanceof Collection) {
this.type = Type.LIST;
this.listValue = new ArrayList<>(((Collection<?>) value));
Expand Down Expand Up @@ -143,7 +160,7 @@ public final class JmesPathRuntime {
switch (type) {
case NULL: return null;
case POJO: return pojoValue;
case INTEGER: return integerValue;
case NUMBER: return numberValue;
case STRING: return stringValue;
case BOOLEAN: return booleanValue;
case LIST: return listValue;
Expand Down Expand Up @@ -207,8 +224,8 @@ public final class JmesPathRuntime {
switch (type) {
case NULL:
return null;
case INTEGER:
return integerValue.toString();
case NUMBER:
return numberValue.toString();
case STRING:
return stringValue;
case BOOLEAN:
Expand Down Expand Up @@ -490,14 +507,14 @@ public final class JmesPathRuntime {
return new Value(false);
}

if (type == Type.INTEGER) {
if (type == Type.NUMBER) {
switch (comparison) {
case "<": return new Value(integerValue < rhs.integerValue);
case "<=": return new Value(integerValue <= rhs.integerValue);
case ">": return new Value(integerValue > rhs.integerValue);
case ">=": return new Value(integerValue >= rhs.integerValue);
case "==": return new Value(Objects.equals(integerValue, rhs.integerValue));
case "!=": return new Value(!Objects.equals(integerValue, rhs.integerValue));
case "<": return new Value(numberValue.compareTo(rhs.numberValue) < 0);
case "<=": return new Value(numberValue.compareTo(rhs.numberValue) <= 0);
case ">": return new Value(numberValue.compareTo(rhs.numberValue) > 0);
case ">=": return new Value(numberValue.compareTo(rhs.numberValue) >= 0);
case "==": return new Value(numberValue.compareTo(rhs.numberValue) == 0);
case "!=": return new Value(numberValue.compareTo(rhs.numberValue) != 0);
default: throw new IllegalArgumentException("Unsupported comparison: " + comparison);
}
}
Expand Down Expand Up @@ -635,7 +652,7 @@ public final class JmesPathRuntime {
MAP,
BOOLEAN,
STRING,
INTEGER,
NUMBER,
NULL
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.squareup.javapoet.ClassName;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class JmesPathAcceptorGeneratorTest {
private JmesPathAcceptorGenerator acceptorGenerator;
Expand All @@ -42,9 +44,10 @@ void testAutoScalingComplexExpression() {
@Test
void testEcsComplexExpression() {
testConversion("length(services[?!(length(deployments) == `1` && runningCount == desiredCount)]) == `0`",
"input.field(\"services\").filter(x0 -> x0.constant(x0.field(\"deployments\").length().compare(\"==\", "
+ "x0.constant(1)).and(x0.field(\"runningCount\").compare(\"==\", x0.field(\"desiredCount\"))).not()))"
+ ".length().compare(\"==\", input.constant(0))");
"input.field(\"services\").filter(x0 -> x0.constant(x0.field(\"deployments\")"
+ ".length().compare(\"==\", x0.constant(new java.math.BigDecimal(\"1\")))"
+ ".and(x0.field(\"runningCount\").compare(\"==\", x0.field(\"desiredCount\")))"
+ ".not())).length().compare(\"==\", input.constant(new java.math.BigDecimal(\"0\")))");
}

@Test
Expand Down Expand Up @@ -326,6 +329,24 @@ void testNegativeNumber() {
testConversion("foo[-10]", "input.field(\"foo\").index(-10)");
}

@ParameterizedTest
@CsvSource({
"123132.81289319837183771465876127837183719837123, 123132.81289319837183771465876127837183719837123",
"42, 42",
"127, 127",
"32767, 32767",
"9223372036854775807, 9223372036854775807",
"42.5, 42.5",
"-42, -42",
"0, 0",
"1e-10, 1E-10"
})
void testNumericValues(String input, String expectedNumber) {
String jmesPath = String.format("value == `%s`", input);
String expected = String.format("input.field(\"value\").compare(\"==\", input.constant(new java.math.BigDecimal(\"%s\")))", expectedNumber);
testConversion(jmesPath, expected);
}

private void testConversion(String jmesPathString, String expectedCode) {
assertThat(acceptorGenerator.interpret(jmesPathString, "input").toString()).isEqualTo((expectedCode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,58 @@
"state": "success"
}
]
},
"FloatValueTest": {
"delay": 15,
"operation": "APostOperation",
"maxAttempts": 40,
"acceptors": [
{
"matcher": "path",
"expected": 42.5,
"argument": "FloatValue",
"state": "success"
}
]
},
"BigDecimalValueTest": {
"delay": 15,
"operation": "APostOperation",
"maxAttempts": 40,
"acceptors": [
{
"matcher": "path",
"expected": 123132.81289319837183771465876127837183719837123,
"argument": "BigDecimalValue",
"state": "success"
}
]
},
"LongValueTest": {
"delay": 15,
"operation": "APostOperation",
"maxAttempts": 40,
"acceptors": [
{
"matcher": "path",
"expected": 9223372036854775807,
"argument": "LongValue",
"state": "success"
}
]
},
"DoubleValueTest": {
"delay": 15,
"operation": "APostOperation",
"maxAttempts": 40,
"acceptors": [
{
"matcher": "path",
"expected": 1.7976931348623157E308,
"argument": "DoubleValue",
"state": "success"
}
]
}
}
}
Loading
Loading