diff --git a/common/src/main/java/com/fauna/common/types/Module.java b/common/src/main/java/com/fauna/common/types/Module.java new file mode 100644 index 00000000..04b2642b --- /dev/null +++ b/common/src/main/java/com/fauna/common/types/Module.java @@ -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); + } +} diff --git a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java index a1a94550..16236f9e 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java @@ -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; @@ -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); @@ -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: @@ -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); + } + } } \ No newline at end of file diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java index 8030cb9b..b786f75c 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -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; @@ -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> expectedTokens = List.of( + Map.entry(FaunaTokenType.MODULE, new Module("MyModule")) + ); + + assertReader(reader, expectedTokens); + } + private static void assertReader(FaunaParser reader, List> tokens) throws IOException { for (Map.Entry entry : tokens) { @@ -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;