-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cd8937
commit 762b169
Showing
7 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/io/weaviate/client/base/TypedSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.weaviate.client.base; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.reflect.TypeToken; | ||
import io.weaviate.client.v1.graphql.model.GraphQLTypedResponse; | ||
import java.lang.reflect.Type; | ||
|
||
public class TypedSerializer<T> { | ||
private Class<T> givenClass; | ||
private final Gson gson; | ||
|
||
public TypedSerializer() { | ||
this.gson = new GsonBuilder().disableHtmlEscaping().create(); | ||
} | ||
|
||
public GraphQLTypedResponse<T> toResponseTyped(String response, Class<T> classOfT) { | ||
return gson.fromJson(response, | ||
TypeToken.getParameterized(GraphQLTypedResponse.class, classOfT).getType()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/io/weaviate/client/v1/graphql/model/GraphQLBaseObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.weaviate.client.v1.graphql.model; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import java.util.Map; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class GraphQLBaseObject { | ||
@SerializedName(value = "_additional") | ||
Additional additional; | ||
|
||
@Getter | ||
public static class Additional { | ||
String id; | ||
Float certainty; | ||
Float[] vector; | ||
Map<String, Float[]> vectors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/io/weaviate/client/v1/graphql/model/GraphQLTypedResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.weaviate.client.v1.graphql.model; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
import lombok.experimental.FieldDefaults; | ||
|
||
@Getter | ||
@Builder | ||
@ToString | ||
@EqualsAndHashCode | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
public class GraphQLTypedResponse<T> { | ||
Operation<T> data; | ||
GraphQLError[] errors; | ||
|
||
@Getter | ||
@ToString | ||
@EqualsAndHashCode | ||
@AllArgsConstructor | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
public static class Operation<T> { | ||
@SerializedName(value = "Get", alternate = {"Aggregate", "Explore"}) | ||
private T objects; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/io/weaviate/client/v1/graphql/model/GraphQLTypedResponseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.weaviate.client.v1.graphql.model; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import com.google.gson.reflect.TypeToken; | ||
import io.weaviate.client.base.Serializer; | ||
import io.weaviate.client.base.TypedSerializer; | ||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Spliterator; | ||
import lombok.Getter; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import junit.framework.TestCase; | ||
import org.junit.Test; | ||
|
||
public class GraphQLTypedResponseTest extends TestCase { | ||
|
||
@Test | ||
public void testGraphQLGetResponse() throws IOException { | ||
// given | ||
Serializer s = new Serializer(); | ||
String json = new String(Files.readAllBytes(Paths.get("src/test/resources/json/graphql-response.json"))); | ||
// when | ||
// Type responseType = new TypeToken<GraphQLTypedResponse<Soups>>() {}.getType(); | ||
Type responseType = TypeToken.getParameterized(GraphQLTypedResponse.class, Soups.class).getType(); | ||
GraphQLTypedResponse < Soups > resp = s.toResponse(json, responseType); | ||
// | ||
assertThat(resp).isNotNull() | ||
.extracting(o -> o.getData().getObjects().getSoups()) | ||
.extracting(o -> o.get(0)).isNotNull() | ||
.extracting(Soups.Soup::getName).isEqualTo("JustSoup"); | ||
} | ||
|
||
@Test | ||
public void testGraphQLGetResponseTyped() throws IOException { | ||
// given | ||
Serializer s = new Serializer(); | ||
String json = new String(Files.readAllBytes(Paths.get("src/test/resources/json/graphql-response.json"))); | ||
// when | ||
// Type responseType = new TypeToken<GraphQLTypedResponse<Soups>>() {}.getType(); | ||
GraphQLTypedResponse<Soups> resp = s.toResponseTyped(json, Soups.class); | ||
// | ||
assertThat(resp).isNotNull() | ||
.extracting(o -> o.getData().getObjects().getSoups()) | ||
.extracting(o -> o.get(0)).isNotNull() | ||
.extracting(Soups.Soup::getName).isEqualTo("JustSoup"); | ||
} | ||
} | ||
|
||
@Getter | ||
class Soups { | ||
@SerializedName(value = "Soup") | ||
List<Soup> soups; | ||
|
||
@Getter | ||
public static class Soup extends GraphQLBaseObject { | ||
String name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"data": { | ||
"Get": { | ||
"Soup": [ | ||
{ | ||
"_additional": { | ||
"certainty": 0.9999998211860657 | ||
}, | ||
"name": "JustSoup" | ||
} | ||
] | ||
} | ||
} | ||
} |