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

Handle unmaterialized sets in PageCodec #183

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading