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 Module values #21

Merged
merged 10 commits into from
Jan 31, 2024
33 changes: 33 additions & 0 deletions common/src/main/java/com/fauna/common/types/Module.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.fauna.common.types;

import java.util.Objects;

public final class Module {

private final String name;

public Module(String name) {
this.name = name;
}

public String getName() {
return name;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Module module = (Module) obj;
return Objects.equals(name, module.name);
}

@Override
public int hashCode() {
return Objects.hash(name);
}
}
17 changes: 12 additions & 5 deletions faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fauna.common.enums.FaunaTokenType;
import com.fauna.common.types.Module;
import com.fauna.exception.SerializationException;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -48,10 +49,6 @@ public class FaunaParser {
private FaunaTokenType bufferedFaunaTokenType;
private String taggedTokenValue;

private enum TokenTypeInternal {
START_ESCAPED_OBJECT
}

public FaunaParser(InputStream body) throws IOException {
JsonFactory factory = new JsonFactory();
this.jsonParser = factory.createParser(body);
Expand Down Expand Up @@ -138,8 +135,10 @@ private void handleStartObject() throws IOException {
case LONG_TAG:
handleTaggedString(FaunaTokenType.LONG);
break;
case DOC_TAG:
case MOD_TAG:
handleTaggedString(FaunaTokenType.MODULE);
break;
case DOC_TAG:
case OBJECT_TAG:
case REF_TAG:
case SET_TAG:
Expand Down Expand Up @@ -251,4 +250,12 @@ public Long getValueAsLong() {
throw new SerializationException("Error getting the current token as Long", e);
}
}

public Module getValueAsModule() {
try {
return new Module(taggedTokenValue);
} catch (Exception e) {
throw new SerializationException("Error getting the current token as Module", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.fauna.common.enums.FaunaTokenType;
import com.fauna.common.types.Module;
import com.fauna.exception.SerializationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -232,6 +233,19 @@ public void testGetValueAsLongFail() throws IOException {
assertEquals("Error getting the current token as Long", ex.getMessage());
}

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

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.MODULE, new Module("MyModule"))
);

assertReader(reader, expectedTokens);
}

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 @@ -264,6 +278,9 @@ private static void assertReader(FaunaParser reader,
case LONG:
assertEquals(entry.getValue(), reader.getValueAsLong());
break;
case MODULE:
assertEquals(entry.getValue(), reader.getValueAsModule());
break;
default:
assertNull(entry.getValue() == null);
break;
Expand Down
Loading