Skip to content

Commit

Permalink
Merge pull request #138 from fauna/nullable-document
Browse files Browse the repository at this point in the history
Change Nullable to NullableDocument
  • Loading branch information
findgriffin authored Sep 11, 2024
2 parents 1d1cab7 + 34d85ef commit 7a34995
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 72 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ of a query is a generic, including:
* `ListOf<T>` where `T` is the element type.
* `MapOf<T>` where `T` is the value type.
* `OptionalOf<T>` where `T` is the value type.
* `NullableOf<T>` where `T` is the value type. This is specifically for cases when you return a Fauna document that may be null and want to receive a concrete `NullDoc<T>` or `NonNullDoc<T>` instead of catching a `NullDocumentException`.
* `NullableDocumentOf<T>` where `T` is the value type. This is specifically for cases when you return a Fauna document that may be null and want to receive a concrete `NullDocument<T>` or `NonNullDocument<T>` instead of catching a `NullDocumentException`.

### Variable interpolation

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/fauna/codec/DefaultCodecProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.fauna.codec.codecs.EnumCodec;
import com.fauna.codec.codecs.ListCodec;
import com.fauna.codec.codecs.MapCodec;
import com.fauna.codec.codecs.NullableCodec;
import com.fauna.codec.codecs.NullableDocumentCodec;
import com.fauna.codec.codecs.OptionalCodec;
import com.fauna.codec.codecs.PageCodec;
import com.fauna.codec.codecs.QueryArrCodec;
Expand All @@ -24,7 +24,7 @@
import com.fauna.types.BaseDocument;
import com.fauna.types.Document;
import com.fauna.types.NamedDocument;
import com.fauna.types.Nullable;
import com.fauna.types.NullableDocument;
import com.fauna.types.Page;

import java.lang.reflect.Type;
Expand Down Expand Up @@ -101,9 +101,9 @@ private <T,E> Codec<T> generate(Class<T> clazz, Type[] typeArgs) {
return (Codec<T>) new PageCodec<E,Page<E>>((Codec<E>) valueCodec);
}

if (clazz == Nullable.class) {
if (clazz == NullableDocument.class) {
Codec<?> valueCodec = this.get((Class<?>) ta, null);
return (Codec<T>) new NullableCodec<E,Nullable<E>>((Codec<E>) valueCodec);
return (Codec<T>) new NullableDocumentCodec<E, NullableDocument<E>>((Codec<E>) valueCodec);
}

if (clazz.isEnum()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/fauna/codec/Generic.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static <E> OptionalOf<E> optionalOf(Class<E> valueClass) {
return new OptionalOf<>(valueClass);
}

public static <E> NullableOf<E> nullableOf(Class<E> valueClass) {
return new NullableOf<>(valueClass);
public static <E> NullableDocumentOf<E> nullableDocumentOf(Class<E> valueClass) {
return new NullableDocumentOf<>(valueClass);
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/fauna/codec/NullableDocumentOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.fauna.codec;

import com.fauna.types.NullableDocument;

import java.lang.reflect.Type;


/**
* NullableDocumentOf stores the generic parameter class to evade type erasure during deserialization.
* @param <E> The value class of the list.
*/
public class NullableDocumentOf<E> extends ParameterizedOf<NullableDocument<E>> {
public NullableDocumentOf(Class<E> valueClass) {
super(NullableDocument.class, new Type[]{valueClass});
}
}
16 changes: 0 additions & 16 deletions src/main/java/com/fauna/codec/NullableOf.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
import com.fauna.exception.NullDocumentException;
import com.fauna.codec.UTF8FaunaGenerator;
import com.fauna.codec.UTF8FaunaParser;
import com.fauna.types.NonNull;
import com.fauna.types.NullDoc;
import com.fauna.types.Nullable;
import com.fauna.types.NonNullDocument;
import com.fauna.types.NullDocument;
import com.fauna.types.NullableDocument;

public class NullableCodec<E,L extends Nullable<E>> extends BaseCodec<L> {

public class NullableDocumentCodec<E,L extends NullableDocument<E>> extends BaseCodec<L> {

private final Codec<E> valueCodec;

public NullableCodec(Codec<E> valueCodec) {
public NullableDocumentCodec(Codec<E> valueCodec) {
this.valueCodec = valueCodec;
}

Expand All @@ -29,19 +30,19 @@ public L decode(UTF8FaunaParser parser) throws CodecException {
try {
E decoded = valueCodec.decode(parser);

if (decoded instanceof NullDoc) return (L) decoded;
if (decoded instanceof NullDocument) return (L) decoded;

return (L) new NonNull<>(decoded);
return (L) new NonNullDocument<>(decoded);
} catch (NullDocumentException e) {
return (L) new NullDoc<>(e.getId(), e.getCollection(), e.getNullCause());
return (L) new NullDocument<>(e.getId(), e.getCollection(), e.getNullCause());
}
}

@Override
public void encode(UTF8FaunaGenerator gen, L obj) throws CodecException {
if (obj instanceof NonNull) {
if (obj instanceof NonNullDocument) {
@SuppressWarnings("unchecked")
NonNull<E> nn = (NonNull<E>) obj;
NonNullDocument<E> nn = (NonNullDocument<E>) obj;
valueCodec.encode(gen, nn.get());
} else {
throw new CodecException(unsupportedTypeMessage(obj.getClass()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import java.util.Objects;

public final class NonNull<T> extends Nullable<T> {
public final class NonNullDocument<T> extends NullableDocument<T> {

public NonNull(T val) {
public NonNullDocument(T val) {
super(val);
}

Expand Down Expand Up @@ -36,9 +36,9 @@ public boolean equals(Object o) {

if (getClass() != o.getClass()) return false;

if (val.getClass() != ((NonNull<?>) o).get().getClass()) return false;
if (val.getClass() != ((NonNullDocument<?>) o).get().getClass()) return false;

return val.equals(((NonNull<?>) o).get());
return val.equals(((NonNullDocument<?>) o).get());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import java.util.Objects;

public final class NullDoc<T> extends Nullable<T> {
public final class NullDocument<T> extends NullableDocument<T> {

private final String id;
private final Module coll;
private final String cause;

public NullDoc(String id, Module coll, String cause) {
public NullDocument(String id, Module coll, String cause) {
super(null);
this.id = id;
this.coll = coll;
Expand Down Expand Up @@ -55,7 +55,7 @@ public boolean equals(Object o) {

if (getClass() != o.getClass()) return false;

var c = (NullDoc<?>) o;
var c = (NullDocument<?>) o;
return id.equals(c.getId())
&& coll.equals(c.getCollection())
&& cause.equals(c.getCause());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.fauna.types;

public abstract class Nullable<T> {
public abstract class NullableDocument<T> {
final T val;

public Nullable(T val) {
public NullableDocument(T val) {
this.val = val;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import java.util.Map;
import java.util.stream.Stream;

public class NullableCodecTest extends TestBase {
public static final Codec<Nullable<Document>> NULLABLE_DOC_CODEC = (Codec)DefaultCodecProvider.SINGLETON.get(Nullable.class, new Type[]{Document.class});
public static final Codec<Nullable<ClassWithAttributes>> NULLABLE_ClASS_CODEC = (Codec)DefaultCodecProvider.SINGLETON.get(Nullable.class, new Type[]{ClassWithAttributes.class});
public class NullableDocumentCodecTest extends TestBase {
public static final Codec<NullableDocument<Document>> NULLABLE_DOC_CODEC = (Codec)DefaultCodecProvider.SINGLETON.get(NullableDocument.class, new Type[]{Document.class});
public static final Codec<NullableDocument<ClassWithAttributes>> NULLABLE_ClASS_CODEC = (Codec)DefaultCodecProvider.SINGLETON.get(NullableDocument.class, new Type[]{ClassWithAttributes.class});

// Class with attributes
public static final String CLASS_WITH_ATTRIBUTES_WIRE = "{\"first_name\":\"foo\",\"last_name\":\"bar\",\"age\":{\"@int\":\"42\"}}";
Expand Down Expand Up @@ -48,17 +48,17 @@ public class NullableCodecTest extends TestBase {

// Null doc
public static final String NULL_DOC_WIRE = "{\"@ref\":{\"id\":\"123\",\"coll\":{\"@mod\":\"Foo\"},\"exists\":false,\"cause\":\"not found\"}}";
public static final NullDoc<Document> NULL_DOCUMENT = new NullDoc<>("123", new Module("Foo"), "not found");
public static final NullDocument<Document> NULL_DOCUMENT = new NullDocument<>("123", new Module("Foo"), "not found");

public static Stream<Arguments> testCases() {
return Stream.of(
Arguments.of(TestType.Decode, NULLABLE_DOC_CODEC, DOCUMENT_WIRE, new NonNull<>(DOCUMENT), null),
Arguments.of(TestType.Encode, NULLABLE_DOC_CODEC, DOCUMENT_REF_WIRE, new NonNull<>(DOCUMENT), null),
Arguments.of(TestType.Decode, NULLABLE_DOC_CODEC, NAMED_DOCUMENT_WIRE, new NonNull<>(NAMED_DOCUMENT), null),
Arguments.of(TestType.Encode, NULLABLE_DOC_CODEC, NAMED_DOCUMENT_REF_WIRE, new NonNull<>(NAMED_DOCUMENT), null),
Arguments.of(TestType.Decode, NULLABLE_DOC_CODEC, DOCUMENT_WIRE, new NonNullDocument<>(DOCUMENT), null),
Arguments.of(TestType.Encode, NULLABLE_DOC_CODEC, DOCUMENT_REF_WIRE, new NonNullDocument<>(DOCUMENT), null),
Arguments.of(TestType.Decode, NULLABLE_DOC_CODEC, NAMED_DOCUMENT_WIRE, new NonNullDocument<>(NAMED_DOCUMENT), null),
Arguments.of(TestType.Encode, NULLABLE_DOC_CODEC, NAMED_DOCUMENT_REF_WIRE, new NonNullDocument<>(NAMED_DOCUMENT), null),
Arguments.of(TestType.Decode, NULLABLE_DOC_CODEC, NULL_DOC_WIRE, NULL_DOCUMENT, null),
Arguments.of(TestType.Decode, NULLABLE_ClASS_CODEC, DOCUMENT_WIRE, new NonNull<>(CLASS_WITH_ATTRIBUTES), null),
Arguments.of(TestType.Encode, NULLABLE_ClASS_CODEC, CLASS_WITH_ATTRIBUTES_WIRE, new NonNull<>(CLASS_WITH_ATTRIBUTES), null),
Arguments.of(TestType.Decode, NULLABLE_ClASS_CODEC, DOCUMENT_WIRE, new NonNullDocument<>(CLASS_WITH_ATTRIBUTES), null),
Arguments.of(TestType.Encode, NULLABLE_ClASS_CODEC, CLASS_WITH_ATTRIBUTES_WIRE, new NonNullDocument<>(CLASS_WITH_ATTRIBUTES), null),
Arguments.of(TestType.Decode, NULLABLE_ClASS_CODEC, NULL_DOC_WIRE, NULL_DOCUMENT, null)
);
}
Expand Down
26 changes: 13 additions & 13 deletions src/test/java/com/fauna/e2e/E2EQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import com.fauna.exception.AbortException;
import com.fauna.query.QueryOptions;
import com.fauna.query.builder.Query;
import com.fauna.types.NonNull;
import com.fauna.types.NullDoc;
import com.fauna.types.Nullable;
import com.fauna.response.QuerySuccess;
import com.fauna.types.NonNullDocument;
import com.fauna.types.NullDocument;
import com.fauna.types.NullableDocument;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -20,7 +20,7 @@
import java.util.Optional;
import java.util.concurrent.ExecutionException;

import static com.fauna.codec.Generic.nullableOf;
import static com.fauna.codec.Generic.nullableDocumentOf;
import static com.fauna.query.builder.Query.fql;
import static com.fauna.codec.Generic.listOf;
import static com.fauna.codec.Generic.mapOf;
Expand Down Expand Up @@ -60,7 +60,7 @@ public void query_syncWithClass() {
@Test
public void query_syncWithParameterized() {
var q = fql("[42]");
QuerySuccess<List<Integer>> res = c.query(q, listOf(int.class));
var res = c.query(q, listOf(int.class));
var exp = List.of(42);
assertEquals(exp, res.getData());
}
Expand Down Expand Up @@ -200,19 +200,19 @@ public void query_optionalNotNull() {
public void query_nullableOf() {
var q = fql("Author.byId('9090090')");

var qs = c.query(q, nullableOf(Author.class));
Nullable<Author> actual = qs.getData();
assertInstanceOf(NullDoc.class, actual);
assertEquals("not found", ((NullDoc<Author>)actual).getCause());
var qs = c.query(q, nullableDocumentOf(Author.class));
NullableDocument<Author> actual = qs.getData();
assertInstanceOf(NullDocument.class, actual);
assertEquals("not found", ((NullDocument<Author>)actual).getCause());
}

@Test
public void query_nullableOfNotNull() {
var q = fql("Author.all().first()");
var qs = c.query(q, nullableOf(Author.class));
Nullable<Author> actual = qs.getData();
assertInstanceOf(NonNull.class, actual);
assertEquals("Alice", ((NonNull<Author>)actual).getValue().getFirstName());
var qs = c.query(q, nullableDocumentOf(Author.class));
NullableDocument<Author> actual = qs.getData();
assertInstanceOf(NonNullDocument.class, actual);
assertEquals("Alice", ((NonNullDocument<Author>)actual).getValue().getFirstName());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

public class NonNullTest extends TypeTestBase {
public class NonNullDocumentTest extends TypeTestBase {

@Test
public void nonNull_playsNiceWithJackson() throws JsonProcessingException {
Expand All @@ -18,7 +18,7 @@ public void nonNull_playsNiceWithJackson() throws JsonProcessingException {
Instant.parse("2024-01-23T13:33:10.300Z"),
Map.of("some_key", "some_val")
);
var nonNull = new NonNull<>(doc);
var nonNull = new NonNullDocument<>(doc);

var result = mapper.writeValueAsString(nonNull);
assertEquals("{\"value\":{\"data\":{\"some_key\":\"some_val\"},\"ts\":1706016790.300000000,\"collection\":{\"name\":\"MyColl\"},\"id\":\"123\"}}", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.Map;

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

public class NullDocTest extends TypeTestBase {
public class NullDocumentTest extends TypeTestBase {

@Test
public void nullDoc_playsNiceWithJackson() throws JsonProcessingException {
var nonNull = new NullDoc<>("123", new Module("MyColl"), "not found");
var nonNull = new NullDocument<>("123", new Module("MyColl"), "not found");

var result = mapper.writeValueAsString(nonNull);
assertEquals("{\"id\":\"123\",\"cause\":\"not found\",\"collection\":{\"name\":\"MyColl\"}}", result);
Expand Down

0 comments on commit 7a34995

Please sign in to comment.