Skip to content

Commit

Permalink
Merge branch 'feature/BT-4418-utf8faunareader_read_int' into feature/B…
Browse files Browse the repository at this point in the history
…T-4418-utf8faunareader_read_boolean

# Conflicts:
#	faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java
  • Loading branch information
pchitolina-fauna committed Jan 24, 2024
2 parents ff12243 + a6fa64b commit 9f89cad
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package com.fauna.serialization;

import static com.fauna.common.enums.FaunaTokenType.END_ARRAY;
import static com.fauna.common.enums.FaunaTokenType.END_DOCUMENT;
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.NONE;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fauna.common.enums.FaunaTokenType;
import com.fauna.exception.SerializationException;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import static com.fauna.common.enums.FaunaTokenType.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.Stack;

/**
* Represents a reader that provides fast, non-cached, forward-only access to serialized data.
*/
public class Utf8FaunaReader {
public class FaunaParser {

private static final String INT_TAG = "@int";
private static final String LONG_TAG = "@long";
private static final String DOUBLE_TAG = "@double";
Expand All @@ -36,21 +45,27 @@ private enum TokenTypeInternal {
START_ESCAPED_OBJECT
}

public Utf8FaunaReader(InputStream body) throws IOException {
public FaunaParser(InputStream body) throws IOException {
JsonFactory factory = new JsonFactory();
this.jsonParser = factory.createParser(body);
currentFaunaTokenType = NONE;
}

public FaunaParser(JsonParser jsonParser) {
this.jsonParser = jsonParser;
currentFaunaTokenType = NONE;
}

public FaunaTokenType getCurrentTokenType() {
return currentFaunaTokenType;
}

private final Set<FaunaTokenType> closers = new HashSet<>(Arrays.asList(
END_OBJECT,
END_PAGE,
END_DOCUMENT,
END_REF,
END_ARRAY
END_OBJECT,
END_PAGE,
END_DOCUMENT,
END_REF,
END_ARRAY
));

public boolean read() throws IOException {
Expand Down Expand Up @@ -86,13 +101,13 @@ public boolean read() throws IOException {
currentFaunaTokenType = FaunaTokenType.FALSE;
break;
default:
throw new SerializationException("Unhandled JSON token type " + currentToken + ".");
throw new SerializationException(
"Unhandled JSON token type " + currentToken + ".");
}
} else {
return false;
}


return true;
}

Expand Down Expand Up @@ -173,7 +188,7 @@ private void advanceTrue() {

private boolean advance() {
try {
return jsonParser.nextToken() != JsonToken.END_OBJECT;
return Objects.nonNull(jsonParser.nextToken());
} catch (IOException e) {
throw new SerializationException("Failed to advance underlying JSON reader.", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
package com.fauna.serialization;

import com.fauna.common.enums.FaunaTokenType;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import com.fauna.common.enums.FaunaTokenType;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;


class Utf8FaunaReaderTest {
class FaunaParserTest {

@Test
public void testGetValueAsString() throws IOException {
InputStream inputStream = new ByteArrayInputStream("\"hello\"".getBytes());
Utf8FaunaReader reader = new Utf8FaunaReader(inputStream);
FaunaParser reader = new FaunaParser(inputStream);

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.STRING, "hello")
Map.entry(FaunaTokenType.STRING, "hello")
);

assertReader(reader, expectedTokens);
}

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

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

assertReader(reader, expectedTokens);
Expand All @@ -41,10 +44,10 @@ public void testReadInt() throws IOException {
@Test
public void testGetValueAsBooleanTrue() throws IOException {
InputStream inputStream = new ByteArrayInputStream("true".getBytes());
Utf8FaunaReader reader = new Utf8FaunaReader(inputStream);
FaunaParser reader = new FaunaParser(inputStream);

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.TRUE, true)
Map.entry(FaunaTokenType.TRUE, true)
);

assertReader(reader, expectedTokens);
Expand All @@ -53,16 +56,17 @@ public void testGetValueAsBooleanTrue() throws IOException {
@Test
public void testGetValueAsBooleanFalse() throws IOException {
InputStream inputStream = new ByteArrayInputStream("false".getBytes());
Utf8FaunaReader reader = new Utf8FaunaReader(inputStream);
FaunaParser reader = new FaunaParser(inputStream);

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.FALSE, false)
Map.entry(FaunaTokenType.FALSE, false)
);

assertReader(reader, expectedTokens);
}

private static void assertReader(Utf8FaunaReader reader, List<Map.Entry<FaunaTokenType, Object>> tokens) throws IOException {
private static void assertReader(FaunaParser reader,
List<Map.Entry<FaunaTokenType, Object>> tokens) {
for (Map.Entry<FaunaTokenType, Object> entry : tokens) {
reader.read();
assertNotNull(entry.getKey());
Expand Down

0 comments on commit 9f89cad

Please sign in to comment.