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] Add support to data types #30

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
120 changes: 118 additions & 2 deletions faunaJava/src/main/java/com/fauna/serialization/FaunaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import com.fauna.common.types.Module;
import com.fauna.exception.SerializationException;
import java.io.IOException;
import java.io.OutputStream;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class FaunaGenerator implements AutoCloseable {

Expand Down Expand Up @@ -49,6 +53,7 @@ public void flush() throws IOException {
* @throws IOException If an I/O error occurs.
*/
public void writeStartObject() throws IOException {
jsonGenerator.writeStartObject();
}

/**
Expand All @@ -57,6 +62,7 @@ public void writeStartObject() throws IOException {
* @throws IOException If an I/O error occurs.
*/
public void writeEndObject() throws IOException {
jsonGenerator.writeEndObject();
}

/**
Expand Down Expand Up @@ -115,6 +121,8 @@ public void writeEndRef() throws IOException {
* @throws IOException If an I/O error occurs.
*/
public void writeDouble(String fieldName, double value) throws IOException {
writeFieldName(fieldName);
writeDoubleValue(value);
}

/**
Expand All @@ -125,6 +133,8 @@ public void writeDouble(String fieldName, double value) throws IOException {
* @throws IOException If an I/O error occurs.
*/
public void writeInt(String fieldName, int value) throws IOException {
writeFieldName(fieldName);
writeIntValue(value);
}

/**
Expand All @@ -135,6 +145,8 @@ public void writeInt(String fieldName, int value) throws IOException {
* @throws IOException If an I/O error occurs.
*/
public void writeLong(String fieldName, long value) throws IOException {
writeFieldName(fieldName);
writeLongValue(value);
}

/**
Expand All @@ -145,6 +157,8 @@ public void writeLong(String fieldName, long value) throws IOException {
* @throws IOException If an I/O error occurs.
*/
public void writeString(String fieldName, String value) throws IOException {
writeFieldName(fieldName);
writeStringValue(value);
}

/**
Expand All @@ -155,6 +169,8 @@ public void writeString(String fieldName, String value) throws IOException {
* @throws IOException If an I/O error occurs.
*/
public void writeDate(String fieldName, LocalDate value) throws IOException {
writeFieldName(fieldName);
writeDateValue(value);
}

/**
Expand All @@ -164,7 +180,9 @@ public void writeDate(String fieldName, LocalDate value) throws IOException {
* @param value The LocalDateTime value to write.
* @throws IOException If an I/O error occurs.
*/
public void writeTime(String fieldName, LocalDateTime value) throws IOException {
public void writeTime(String fieldName, Instant value) throws IOException {
writeFieldName(fieldName);
writeTimeValue(value);
}

/**
Expand All @@ -175,6 +193,8 @@ public void writeTime(String fieldName, LocalDateTime value) throws IOException
* @throws IOException If an I/O error occurs.
*/
public void writeBoolean(String fieldName, boolean value) throws IOException {
writeFieldName(fieldName);
writeBooleanValue(value);
}

/**
Expand All @@ -184,6 +204,8 @@ public void writeBoolean(String fieldName, boolean value) throws IOException {
* @throws IOException If an I/O error occurs.
*/
public void writeNull(String fieldName) throws IOException {
writeFieldName(fieldName);
writeNullValue();
}

/**
Expand All @@ -203,6 +225,7 @@ public void writeModule(String fieldName, Module value) throws IOException {
* @throws IOException If an I/O error occurs.
*/
public void writeFieldName(String value) throws IOException {
jsonGenerator.writeFieldName(value);
}

/**
Expand All @@ -213,6 +236,99 @@ public void writeFieldName(String value) throws IOException {
* @throws IOException If an I/O error occurs.
*/
public void writeTaggedValue(String tag, String value) throws IOException {
writeStartObject();
writeString(tag, value);
writeEndObject();
}

/**
* Writes a double value as a tagged element.
*
* @param value The double value to write.
* @throws IOException If an I/O error occurs.
*/
public void writeDoubleValue(double value) throws IOException {
writeTaggedValue("@double", Double.toString(value));
}

/**
* Writes an integer value as a tagged element.
*
* @param value The integer value to write.
* @throws IOException If an I/O error occurs.
*/
public void writeIntValue(int value) throws IOException {
writeTaggedValue("@int", Integer.toString(value));
}

/**
* Writes a long integer value as a tagged element.
*
* @param value The long integer value to write.
* @throws IOException If an I/O error occurs.
*/
public void writeLongValue(long value) throws IOException {
writeTaggedValue("@long", Long.toString(value));
}

/**
* Writes a string value as a tagged element.
*
* @param value The string value to write.
* @throws IOException If an I/O error occurs.
*/
public void writeStringValue(String value) throws IOException {
jsonGenerator.writeString(value);
}

/**
* Writes a date value as a tagged element.
*
* @param value The date value to write.
* @throws IOException If an I/O error occurs.
*/
public void writeDateValue(LocalDate value) throws IOException {
try {
String str = value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
writeTaggedValue("@date", str);
} catch (DateTimeException e) {
throw new SerializationException("Error writing date value", e);
}
}

/**
* Writes a time value as a tagged element.
*
* @param value The time value to write.
* @throws IOException If an I/O error occurs.
*/
public void writeTimeValue(Instant value) throws IOException {
try {
Instant instant = value.atZone(ZoneOffset.UTC).toInstant();
String formattedTime = instant.toString();
writeTaggedValue("@time", formattedTime);
} catch (DateTimeException e) {
throw new SerializationException("Error writing time value", e);
}
}

/**
* Writes a boolean value to the stream.
*
* @param value The boolean value to write.
* @throws IOException If an I/O error occurs.
*/
public void writeBooleanValue(boolean value) throws IOException {
jsonGenerator.writeBoolean(value);
}

/**
* Writes a null value to the stream.
*
* @throws IOException If an I/O error occurs.
*/
public void writeNullValue() throws IOException {
jsonGenerator.writeNull();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,86 @@
package com.fauna.serialization;

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

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDate;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class FaunaGeneratorTest {
public class FaunaGeneratorTest {

private FaunaGenerator writer;
private ByteArrayOutputStream stream;

@Before
@BeforeEach
public void setUp() throws IOException {
stream = new ByteArrayOutputStream();
writer = new FaunaGenerator(stream);
}

@After
@AfterEach
public void tearDown() throws IOException {
writer.close();
stream.close();
}

@Test
public void writeIntValue() throws IOException {
writer.writeIntValue(42);
assertWriter("{\"@int\":\"42\"}");
}

@Test
public void writeLongValue() throws IOException {
writer.writeLongValue(42L);
assertWriter("{\"@long\":\"42\"}");
}

@Test
public void writeDoubleValue() throws IOException {
writer.writeDoubleValue(1.2d);
assertWriter("{\"@double\":\"1.2\"}");
}

@Test
public void writeTrueValue() throws IOException {
writer.writeBooleanValue(true);
assertWriter("true");
}

@Test
public void writeFalseValue() throws IOException {
writer.writeBooleanValue(false);
assertWriter("false");
}

@Test
public void writeNullValue() throws IOException {
writer.writeNullValue();
assertWriter("null");
}

@Test
public void writeDate() throws IOException {
LocalDate dateTime = LocalDate.of(2023, 1, 1);
writer.writeDateValue(dateTime);
assertWriter("{\"@date\":\"2023-01-01\"}");
}

@Test
public void writeTime() throws IOException {
Instant instant = Instant.parse("2024-01-23T13:33:10.300Z");
writer.writeTimeValue(instant);
assertWriter("{\"@time\":\"2024-01-23T13:33:10.300Z\"}");
}
pchitolina-fauna marked this conversation as resolved.
Show resolved Hide resolved

private void assertWriter(String expected) throws IOException {
writer.flush();
String actual = new String(stream.toByteArray(), StandardCharsets.UTF_8);
assertEquals(expected, actual);
}
}
Loading