Skip to content

Commit

Permalink
Handle unmaterialized sets in PageCodec
Browse files Browse the repository at this point in the history
  • Loading branch information
pnwpedro committed Nov 12, 2024
1 parent 2cf49bd commit 597a1f9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/main/java/com/fauna/codec/codecs/PageCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fauna.exception.CodecException;
import com.fauna.types.Page;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -84,10 +85,19 @@ public Class<?> getCodecClass() {

private L decodePage(final UTF8FaunaParser parser, final FaunaTokenType endToken)
throws CodecException {

parser.read();
if (parser.getCurrentTokenType() == FaunaTokenType.STRING) {
return handleUnmaterialized(parser, endToken);
} else {
return handleMaterialized(parser, endToken);
}
}

private L handleMaterialized(final UTF8FaunaParser parser, final FaunaTokenType endToken) {
List<E> data = null;
String after = null;

while (parser.read() && parser.getCurrentTokenType() != endToken) {
do {
String fieldName = parser.getValueAsString();
parser.read();

Expand All @@ -101,16 +111,23 @@ private L decodePage(final UTF8FaunaParser parser, final FaunaTokenType endToken
default:
break;
}
}
} while (parser.read() && parser.getCurrentTokenType() != endToken);

if (data == null) {
throw new CodecException(
"No page data found while deserializing into Page<>");
//noinspection unchecked
return (L) new Page<>(data, after);
}

private L handleUnmaterialized(final UTF8FaunaParser parser, final FaunaTokenType endToken) {
var after = parser.getValueAsString();
parser.read();

if (parser.getCurrentTokenType() != endToken) {
throw new CodecException(unexpectedTokenExceptionMessage(parser.getCurrentTokenType()));
}

@SuppressWarnings("unchecked")
L res = (L) new Page<>(data, after);
return res;
//noinspection unchecked
return (L) new Page<>(new ArrayList<>(), after);

}

private L wrapInPage(final UTF8FaunaParser parser) throws CodecException {
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/fauna/codec/codecs/PageCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.fauna.codec.Codec;
import com.fauna.codec.DefaultCodecProvider;
import com.fauna.codec.FaunaType;
import com.fauna.codec.Helpers;
import com.fauna.exception.CodecException;
import com.fauna.types.Page;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -16,6 +18,8 @@
import java.util.List;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;


public class PageCodecTest extends TestBase {
public static final String PAGE_WIRE =
Expand Down Expand Up @@ -77,4 +81,15 @@ public void page_runUnsupportedTypeTestCases(String wire, FaunaType type)
runCase(TestType.Decode, pageCodecOf(Object.class), wire, null,
new CodecException(exMsg));
}

@Test
public void page_decodeUnmaterializedSet()
{
var token = "aftertoken";
var wire = "{\"@set\":\"" + token + "\"}";
var codec = pageCodecOf(Object.class);
var decoded = Helpers.decode(codec, wire);
assertEquals(token, decoded.getAfter().get().getToken());
assertEquals(0, decoded.getData().size());
}
}

0 comments on commit 597a1f9

Please sign in to comment.