Skip to content

Commit

Permalink
add logic to handle number acceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
RanVaknin committed Jan 21, 2025
1 parent 5a6d4b5 commit 065bd5f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ 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)
// .enable(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS)
.treeCodec(new JacksonJrsTreeCodec());

mapperBuilder.streamFactory().enable(JsonParser.Feature.ALLOW_COMMENTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@

package software.amazon.awssdk.codegen.poet.waiters;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.jr.stree.JrsBoolean;
import com.fasterxml.jackson.jr.stree.JrsValue;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;
Expand Down Expand Up @@ -273,7 +276,30 @@ 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()));
JsonParser.NumberType numberType = jsonValue.numberType();
System.out.println("Number type: " + numberType);
switch (numberType) {
case INT:
codeBlock.add(".constant($L)", Integer.parseInt(jsonValue.asText()));
break;
case LONG:
codeBlock.add(".constant($L)", Long.parseLong(jsonValue.asText()));
break;
case FLOAT:
codeBlock.add(".constant($L)", Float.parseFloat(jsonValue.asText()));
break;
case DOUBLE:
codeBlock.add(".constant($L)", Double.parseDouble(jsonValue.asText()));
break;
case BIG_DECIMAL:
codeBlock.add(".constant(new $T($S))", BigDecimal.class, jsonValue.asText());
break;
case BIG_INTEGER:
codeBlock.add(".constant(new $T($S))", BigInteger.class, jsonValue.asText());
break;
default:
throw new IllegalArgumentException("Unsupported number type: " + numberType);
}
} 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
Expand Up @@ -326,6 +326,42 @@ void testNegativeNumber() {
testConversion("foo[-10]", "input.field(\"foo\").index(-10)");
}

@Test
void testNumberLiterals() {
testConversion("`42`", "input.constant(42)");

testConversion("`42.5`", "input.constant(42.5)");

testConversion("`9223372036854775807`", "input.constant(9223372036854775807)");

// testConversion("`123.456789012345678901123123123123`", "input.constant(new BigDecimal(\"123.456789012345678901123123123123\"))");
}

@Test
void testFloatPrecision() {
testConversion("`123.4567`", "input.constant(123.4567f)");
}

@Test
void testDoublePrecision() {
testConversion("`123.456789012345`", "input.constant(123.456789012345)");
}

@Test
void testFloatScientificNotation() {
testConversion("`1.23e-4`", "input.constant(1.23E-4)");
}

@Test
void testFloatMaxValue() {
testConversion("`3.4028235E38`", "input.constant(3.4028235E38)");
}

@Test
void testDoubleMaxValue() {
testConversion("`1.7976931348623157E308`", "input.constant(1.7976931348623157E308)");
}

private void testConversion(String jmesPathString, String expectedCode) {
assertThat(acceptorGenerator.interpret(jmesPathString, "input").toString()).isEqualTo((expectedCode));
}
Expand Down

0 comments on commit 065bd5f

Please sign in to comment.