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

Make obj/arr encoding more restrictive #139

Merged
merged 1 commit into from
Sep 11, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Compiled class file
*.class
src/main/resources/*

# Log file
*.log
Expand Down
21 changes: 8 additions & 13 deletions src/main/java/com/fauna/query/builder/QueryArr.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package com.fauna.query.builder;

import com.fauna.codec.Codec;
import com.fauna.codec.CodecProvider;
import com.fauna.codec.UTF8FaunaGenerator;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
* Represents a value fragment of a Fauna query.
* This class encapsulates a value that can be a variable in the query.
*/
public class QueryArr<E,T> extends QueryFragment<T> {
public static <E,T> QueryArr<E,T> of(T val) {
return new QueryArr<>(val);
public class QueryArr<E extends QueryFragment> extends QueryFragment<List<E>> {
public static <E extends QueryFragment> QueryArr of(List<E> val) {
return new QueryArr(val);
}

private final T value;
private final List<E> value;

/**
* Constructs a ValueFragment with the specified value.
*
* @param value the value to encapsulate, which can be any object.
* @param value the value to encapsulate.
*/
public QueryArr(T value) {
public QueryArr(List<E> value) {
this.value = value;
}

Expand All @@ -34,7 +29,7 @@ public QueryArr(T value) {
* @return the encapsulated object.
*/
@Override
public T get() {
public List<E> get() {
return value;
}

Expand All @@ -43,7 +38,7 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

QueryArr<E,T> that = (QueryArr<E,T>) o;
QueryArr that = (QueryArr) o;

return Objects.equals(value, that.value);
}
Expand Down
22 changes: 9 additions & 13 deletions src/main/java/com/fauna/query/builder/QueryObj.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
package com.fauna.query.builder;

import com.fauna.codec.Codec;
import com.fauna.codec.CodecProvider;
import com.fauna.codec.UTF8FaunaGenerator;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

/**
* Represents an object fragment of a Fauna query. Object fragments allow for the evaluation of FQL statements
* stored on the object. This class encapsulates an object that can be a variable in the query.
*/
public class QueryObj<T> extends QueryFragment<T> {
public class QueryObj<E extends QueryFragment> extends QueryFragment<Map<String,E>> {

public static <T> QueryObj<T> of(T val) {
return new QueryObj<>(val);
public static <E extends QueryFragment> QueryObj of(Map<String, E> val) {
return new QueryObj(val);
}

private final T value;
private final Map<String, E> value;

/**
* Constructs a QueryObj with the specified value.
*
* @param value the value to encapsulate, which can be any object.
* @param value the value to encapsulate.
*/
public QueryObj(T value) {
public QueryObj(Map<String,E> value) {
this.value = value;
}

Expand All @@ -34,7 +30,7 @@ public QueryObj(T value) {
* @return the encapsulated object.
*/
@Override
public T get() {
public Map<String,E> get() {
return value;
}

Expand All @@ -43,7 +39,7 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

QueryObj<T> that = (QueryObj<T>) o;
QueryObj that = (QueryObj) o;

return Objects.equals(value, that.value);
}
Expand Down
19 changes: 0 additions & 19 deletions src/test/java/com/fauna/beans/ClassWithNestedQuery.java

This file was deleted.

11 changes: 6 additions & 5 deletions src/test/java/com/fauna/e2e/E2EQueryObjTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.fauna.e2e;

import com.fauna.beans.ClassWithNestedQuery;
import com.fauna.client.Fauna;
import com.fauna.client.FaunaClient;
import com.fauna.query.builder.Query;
import com.fauna.query.builder.QueryFragment;
import com.fauna.query.builder.QueryObj;
import com.fauna.query.builder.QueryVal;
import com.fauna.response.QuerySuccess;
import org.junit.jupiter.api.Test;

Expand All @@ -20,7 +21,7 @@ public class E2EQueryObjTest {
@Test
public void query_mapWithEmbeddedQuery() {
Query subq = fql("4 + 2");
Map<String,Query> obj = Map.of("k", subq);
Map<String, QueryFragment> obj = Map.of("k", subq);

Query q = fql("${obj}", Map.of("obj", QueryObj.of(obj)));
QuerySuccess<Map<String,Integer>> res = c.query(q, mapOf(Integer.class));
Expand All @@ -29,14 +30,14 @@ public void query_mapWithEmbeddedQuery() {
}

@Test
public void query_classWithEmbeddedQuery() {
public void query_mapMixedWithEmbeddedQuery() {
Query subq = fql("4 + 2");
var obj = new ClassWithNestedQuery(subq);
Map<String, QueryFragment> obj = Map.of("k", subq, "k2", new QueryVal<>(42));

Query q = fql("${obj}", Map.of("obj", QueryObj.of(obj)));
QuerySuccess<Map<String,Integer>> res = c.query(q, mapOf(Integer.class));

assertEquals(Map.of("result", 6), res.getData());
assertEquals(Map.of("k", 6, "k2", 42), res.getData());
}

@Test
Expand Down
Loading