-
Notifications
You must be signed in to change notification settings - Fork 23
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
Showing
10 changed files
with
246 additions
and
20 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
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,16 @@ | ||
package com.surrealdb.refactor; | ||
|
||
import com.surrealdb.refactor.types.surrealdb.Value; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Helpers { | ||
public static Map<String, Value> asMap(Tuple<String, Value>... data) { | ||
Map<String, Value> obj = new HashMap<>(); | ||
for (Tuple<String, Value> entry : data) { | ||
obj.put(entry.key, entry.value); | ||
} | ||
return obj; | ||
} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
src/main/java/com/surrealdb/refactor/driver/parsing/JsonQueryResultParser.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,79 @@ | ||
package com.surrealdb.refactor.driver.parsing; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.surrealdb.refactor.exception.SurrealDBUnimplementedException; | ||
import com.surrealdb.refactor.exception.UnhandledProtocolResponse; | ||
import com.surrealdb.refactor.types.QueryResult; | ||
import com.surrealdb.refactor.types.surrealdb.ObjectValue; | ||
import com.surrealdb.refactor.types.surrealdb.Value; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class JsonQueryResultParser { | ||
private static final String ARRAY = "Array"; | ||
private static final String OBJECT = "Object"; | ||
private static final String PRIMTIVE = "Primitive"; | ||
private static final String NULL = "Null"; | ||
|
||
public QueryResult parse(JsonElement jsonElement) { | ||
// Test | ||
if (!jsonElement.isJsonObject()) { | ||
throw new UnhandledProtocolResponse(String.format("Expected json object, but was %s", type(jsonElement))); | ||
} | ||
JsonObject jsonObject = jsonElement.getAsJsonObject(); | ||
String status = forceGet(jsonObject, "status", new UnhandledProtocolResponse("Expected the object to have a status")).getAsString(); | ||
String time = forceGet(jsonObject, "time", new UnhandledProtocolResponse("Expected the object to have a time")).getAsString(); | ||
JsonElement result = forceGet(jsonObject, "result", new UnhandledProtocolResponse("Expected the object to contain a result")); | ||
List<Value> valueList = new ArrayList<>(); | ||
if (result.isJsonObject()) { | ||
JsonObject object = result.getAsJsonObject(); | ||
valueList.add(parseValue(object)); | ||
} else if (result.isJsonArray()) { | ||
JsonArray array = result.getAsJsonArray(); | ||
for (JsonElement arrayElement: array) { | ||
valueList.add(parseValue(arrayElement)); | ||
} | ||
} else { | ||
throw new UnhandledProtocolResponse(String.format("Expected the result type in a result to be an object of array instead was: %s", type(jsonElement))); | ||
} | ||
return new QueryResult(valueList, status, time); | ||
} | ||
|
||
private Value parseValue(JsonElement jsonElement) { | ||
if (!jsonElement.isJsonObject()) { | ||
throw new UnhandledProtocolResponse(String.format("Expected the result type to be object but was %s", type(jsonElement))); | ||
} | ||
Map<String, Value> objectProperties = new HashMap<>(); | ||
JsonObject jsonObject = jsonElement.getAsJsonObject(); | ||
for (Map.Entry<String, JsonElement> entry: jsonObject.entrySet()) { | ||
String value = entry.getValue().getAsString(); | ||
objectProperties.put(entry.getKey(), new Value(value)); | ||
} | ||
return new Value(new ObjectValue(objectProperties)); | ||
} | ||
|
||
private JsonElement forceGet(JsonObject jsonObject, String property, UnhandledProtocolResponse cause) { | ||
if (!jsonObject.has(property)) { | ||
throw cause; | ||
} | ||
return jsonObject.get(property); | ||
} | ||
|
||
private static String type(JsonElement jsonElement) { | ||
if (jsonElement.isJsonArray()) { | ||
return ARRAY; | ||
} else if (jsonElement.isJsonObject()) { | ||
return OBJECT; | ||
} else if (jsonElement.isJsonNull()) { | ||
return NULL; | ||
} else if (jsonElement.isJsonPrimitive()) { | ||
return PRIMTIVE; | ||
} | ||
throw new SurrealDBUnimplementedException("todo create ticket", "really we should be doing more in depth handling of types to also give Value types"); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/surrealdb/refactor/exception/UnhandledProtocolResponse.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,14 @@ | ||
package com.surrealdb.refactor.exception; | ||
|
||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@ToString | ||
public class UnhandledProtocolResponse extends SurrealDBException{ | ||
private final String message; | ||
public UnhandledProtocolResponse(String message) { | ||
super(message); | ||
this.message = message; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/com/surrealdb/refactor/types/surrealdb/Number.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
18 changes: 18 additions & 0 deletions
18
src/main/java/com/surrealdb/refactor/types/surrealdb/ObjectValue.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,18 @@ | ||
package com.surrealdb.refactor.types.surrealdb; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
import java.util.Map; | ||
|
||
@Getter | ||
@ToString | ||
@EqualsAndHashCode | ||
public class ObjectValue { | ||
private final Map<String, Value> values; | ||
|
||
public ObjectValue(Map<String, Value> values) { | ||
this.values = values; | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
src/test/java/com/surrealdb/refactor/driver/parsing/JsonQueryResultParserTest.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,45 @@ | ||
package com.surrealdb.refactor.driver.parsing; | ||
|
||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParser; | ||
import com.surrealdb.refactor.types.QueryResult; | ||
import com.surrealdb.refactor.types.surrealdb.ObjectValue; | ||
import com.surrealdb.refactor.types.surrealdb.Value; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class JsonQueryResultParserTest { | ||
|
||
@Test | ||
public void regular_result() { | ||
String rawJson = """ | ||
{"result":[{"id":"person:lamport","name":"leslie"}],"status":"OK","time":"438.583µs"} | ||
"""; | ||
|
||
JsonElement jsonElement = JsonParser.parseString(rawJson); | ||
QueryResult res = new JsonQueryResultParser().parse(jsonElement); | ||
|
||
assertEquals("OK", res.getStatus()); | ||
assertEquals("438.583µs", res.getTime()); | ||
Value expectedValue = new Value(new ObjectValue(asMap( | ||
Tuple.of("id", new Value("person:lamport")), | ||
Tuple.of("name", new Value("leslie")) | ||
))); | ||
assertArrayEquals(new Value[] {expectedValue}, res.getResult().toArray(new Value[0])); | ||
} | ||
|
||
private static Map<String, Value> asMap(Tuple<String, Value>... data) { | ||
Map<String, Value> obj = new HashMap<>(); | ||
for (Tuple<String, Value> entry : data) { | ||
obj.put(entry.key, entry.value); | ||
} | ||
return obj; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/java/com/surrealdb/refactor/driver/parsing/Tuple.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,15 @@ | ||
package com.surrealdb.refactor.driver.parsing; | ||
|
||
public class Tuple<K, V> { | ||
final K key; | ||
final V value; | ||
|
||
private Tuple(K key, V value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
|
||
public static <K, V> Tuple<K, V> of(K key, V value) { | ||
return new Tuple<>(key, value); | ||
} | ||
} |