Skip to content

Commit

Permalink
fix: handle unmaterialized sets
Browse files Browse the repository at this point in the history
  • Loading branch information
pnwpedro committed Oct 23, 2024
1 parent 605384a commit f5768ee
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/main/java/com/fauna/codec/UTF8FaunaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ public class UTF8FaunaParser {
private final Stack<Object> tokenStack = new Stack<>();
private FaunaTokenType currentFaunaTokenType = NONE;
private FaunaTokenType bufferedFaunaTokenType;
private Object bufferedTokenValue;
private String taggedTokenValue;


private enum InternalTokenType {
START_ESCAPED_OBJECT
START_ESCAPED_OBJECT,
START_PAGE_UNMATERIALIZED
}

public UTF8FaunaParser(JsonParser jsonParser) {
Expand Down Expand Up @@ -128,6 +130,8 @@ public boolean read() throws CodecException {
return true;
}

bufferedTokenValue = null;

if (!advance()) {
return false;
}
Expand Down Expand Up @@ -218,7 +222,20 @@ private void handleStartObject() throws CodecException {
case SET_TAG:
advanceTrue();
currentFaunaTokenType = FaunaTokenType.START_PAGE;
tokenStack.push(FaunaTokenType.START_PAGE);

if (jsonParser.currentToken() == JsonToken.VALUE_STRING) {
bufferedFaunaTokenType = FaunaTokenType.STRING;

try {
bufferedTokenValue = jsonParser.getValueAsString();
} catch (IOException e) {
throw new CodecException(e.getMessage(), e);
}

tokenStack.push(InternalTokenType.START_PAGE_UNMATERIALIZED);
} else {
tokenStack.push(FaunaTokenType.START_PAGE);
}
break;
case REF_TAG:
advanceTrue();
Expand Down Expand Up @@ -248,6 +265,8 @@ private void handleEndObject() {
if (startToken.equals(FaunaTokenType.START_DOCUMENT)) {
currentFaunaTokenType = END_DOCUMENT;
advanceTrue();
} else if (startToken.equals(InternalTokenType.START_PAGE_UNMATERIALIZED)) {
currentFaunaTokenType = END_PAGE;
} else if (startToken.equals(FaunaTokenType.START_PAGE)) {
currentFaunaTokenType = END_PAGE;
advanceTrue();
Expand Down Expand Up @@ -320,6 +339,9 @@ public Character getValueAsCharacter() {

public String getValueAsString() {
try {
if (bufferedTokenValue != null) {
return bufferedTokenValue.toString();
}
return jsonParser.getValueAsString();
} catch (IOException e) {
throw new CodecException("Error getting the current token as String", e);
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/fauna/codec/UTF8FaunaParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,26 @@ public void testReadSet() throws IOException {
assertReader(reader, expectedTokens);
}


@Test
public void testReadUnmaterializedSet() {
String s = "{\"products\":{\"@set\":\"sometoken\"},\"name\":\"foo\"}";
UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(s.getBytes()));

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
new AbstractMap.SimpleEntry<>(FaunaTokenType.START_OBJECT, null),
Map.entry(FaunaTokenType.FIELD_NAME, "products"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.START_PAGE, null),
Map.entry(FaunaTokenType.STRING, "sometoken"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_PAGE, null),
Map.entry(FaunaTokenType.FIELD_NAME, "name"),
Map.entry(FaunaTokenType.STRING, "foo"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_OBJECT, null)
);

assertReader(reader, expectedTokens);
}

@Test
public void testReadRef() throws IOException {
String s = "{\"@ref\": {\"id\": \"123\", \"coll\": {\"@mod\": \"Col\"}}}";
Expand Down

0 comments on commit f5768ee

Please sign in to comment.