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

[BT-4418] FaunaParser Long values #20

Merged
merged 11 commits into from
Jan 31, 2024
Merged
14 changes: 13 additions & 1 deletion faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static com.fauna.common.enums.FaunaTokenType.END_OBJECT;
import static com.fauna.common.enums.FaunaTokenType.END_PAGE;
import static com.fauna.common.enums.FaunaTokenType.END_REF;
import static com.fauna.common.enums.FaunaTokenType.LONG;
import static com.fauna.common.enums.FaunaTokenType.NONE;
import static com.fauna.common.enums.FaunaTokenType.TIME;

Expand Down Expand Up @@ -133,8 +134,10 @@ private void handleStartObject() throws IOException {
case DOUBLE_TAG:
handleTaggedString(FaunaTokenType.DOUBLE);
break;
case DOC_TAG:
case LONG_TAG:
handleTaggedString(FaunaTokenType.LONG);
break;
case DOC_TAG:
case MOD_TAG:
case OBJECT_TAG:
case REF_TAG:
Expand Down Expand Up @@ -238,4 +241,13 @@ public Double getValueAsDouble() {
throw new RuntimeException("Error getting the current token as Double", e);
}
}

public Long getValueAsLong() {
validateTaggedType(LONG);
try {
return Long.parseLong(taggedTokenValue);
} catch (NumberFormatException e) {
throw new RuntimeException("Error getting the current token as Long", e);
pchitolina-fauna marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,25 @@ public void testGetValueAsDouble() throws IOException {
assertThrows(RuntimeException.class, invalidReader::getValueAsDouble);
}

@Test
public void testGetValueAsLong() throws IOException {
String s = "{\"@long\": \"123\"}";
InputStream inputStream = new ByteArrayInputStream(s.getBytes());
FaunaParser reader = new FaunaParser(inputStream);

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.LONG, 123L)
);

assertReader(reader, expectedTokens);

String invalidJson = "{\"@long\": \"abc\"}";
InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes());
FaunaParser invalidReader = new FaunaParser(invalidInputStream);

assertThrows(RuntimeException.class, invalidReader::getValueAsLong);
pchitolina-fauna marked this conversation as resolved.
Show resolved Hide resolved
}

private static void assertReader(FaunaParser reader,
List<Map.Entry<FaunaTokenType, Object>> tokens) throws IOException {
for (Map.Entry<FaunaTokenType, Object> entry : tokens) {
Expand Down Expand Up @@ -189,6 +208,9 @@ private static void assertReader(FaunaParser reader,
case DOUBLE:
assertEquals(entry.getValue(), reader.getValueAsDouble());
break;
case LONG:
assertEquals(entry.getValue(), reader.getValueAsLong());
break;
default:
assertNull(entry.getValue() == null);
break;
Expand Down
Loading