Skip to content

Commit

Permalink
Working prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
gino-m committed Feb 24, 2024
1 parent 04c50b9 commit 17d477b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package com.google.android.ground.persistence.remote.firebase

import com.google.ground.shared.schema.GeometryData
import com.google.ground.shared.schema.MultiPolygonData
import com.google.ground.shared.schema.PointData
import com.google.ground.shared.schema.PolygonData
import com.google.ground.shared.schema.GeometryObject
import com.google.ground.shared.schema.MultiPolygonObject
import com.google.ground.shared.schema.PointObject
import com.google.ground.shared.schema.PolygonObject
import com.google.gson.GsonBuilder
import com.google.gson.JsonArray
import com.google.gson.JsonDeserializationContext
Expand All @@ -36,9 +36,7 @@ class ObjectMapper {
fun <T : Any> toObject(map: Map<String, Any>, kotlinClass: KClass<T>): T {

val gson =
GsonBuilder()
.registerTypeAdapter(GeometryData::class.java, GeometryDeserializer())
.create()
GsonBuilder().registerTypeAdapter(GeometryObject::class.java, GeometryDeserializer()).create()
val json = map.toJsonElement()
return gson.fromJson(json, kotlinClass.java)
}
Expand Down Expand Up @@ -72,12 +70,12 @@ class ObjectMapper {
}
}

class GeometryDeserializer : JsonDeserializer<GeometryData> {
class GeometryDeserializer : JsonDeserializer<GeometryObject> {
override fun deserialize(
json: JsonElement,
typeOfT: Type,
context: JsonDeserializationContext
): GeometryData {
): GeometryObject {
val type = json.asJsonObject["type"].asString
val kotlinClass =
geometryClassesByType[type] ?: throw JsonParseException("Unknown geometry type $type")
Expand All @@ -87,7 +85,7 @@ class GeometryDeserializer : JsonDeserializer<GeometryData> {

internal val geometryClassesByType =
mapOf(
"Point" to PointData::class,
"Polygon" to PolygonData::class,
"MultiPolygon" to MultiPolygonData::class
"Point" to PointObject::class,
"Polygon" to PolygonObject::class,
"MultiPolygon" to MultiPolygonObject::class
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.google.android.ground.persistence.remote.firebase

import com.google.firebase.firestore.GeoPoint
import com.google.ground.schema.PointData
import com.google.ground.schema.PolygonData
import com.google.ground.shared.LoiDocument
import com.google.ground.shared.schema.CoordinatesObject
import com.google.ground.shared.schema.LinearRingObject
import com.google.ground.shared.schema.LoiDocument
import com.google.ground.shared.schema.PointObject
import com.google.ground.shared.schema.PolygonObject
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlin.test.assertEquals
Expand All @@ -23,7 +24,7 @@ class ObjectMapperTest {
"submissionCount": 42,
"geometry": {
"type": "Point",
"coordinates": [1234.5678, 9876.5432]
"coordinates": {"x": 1234.5678, "y": 9876.5432}
}
}
"""
Expand All @@ -33,7 +34,7 @@ class ObjectMapperTest {
jobId = "123",
customId = "foo",
submissionCount = 42,
geometry = PointData(coordinates = xy(1234.5678, 9876.5432))
geometry = PointObject(coordinates = xy(1234.5678, 9876.5432))
)
val actual = ObjectMapper().toObject(json.toMap(), LoiDocument::class)
assertEquals(expected, actual)
Expand All @@ -51,12 +52,14 @@ class ObjectMapperTest {
"geometry": {
"type": "Polygon",
"coordinates": [
[
[0.0, 0.0],
[1.0, 0.0],
[1.0, 1.0],
[0.0, 0.0]
]
{"$":
[
{"x": 0.0, "y": 0.0},
{"x": 1.0, "y": 0.0},
{"x": 1.0, "y": 1.0},
{"x": 0.0, "y": 0.0}
]
}
]
}
}
Expand All @@ -68,8 +71,11 @@ class ObjectMapperTest {
customId = "foo",
submissionCount = 42,
geometry =
PolygonData(
coordinates = listOf(listOf(xy(0.0, 0.0), xy(1.0, 0.0), xy(1.0, 1.0), xy(0.0, 0.0)))
PolygonObject(
coordinates =
listOf(
LinearRingObject(listOf(xy(0.0, 0.0), xy(1.0, 0.0), xy(1.0, 1.0), xy(0.0, 0.0)))
)
)
)
val actual = ObjectMapper().toObject(json.toMap(), LoiDocument::class)
Expand All @@ -81,4 +87,4 @@ internal object MapTypeToken : TypeToken<Map<String, Any>>()

internal fun String.toMap(): Map<String, Any> = Gson().fromJson(this, MapTypeToken.type)

internal fun xy(x: Double, y: Double) = GeoPoint(y, x)
internal fun xy(x: Double, y: Double) = CoordinatesObject(x.toBigDecimal(), y.toBigDecimal())
20 changes: 11 additions & 9 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ apply<JSONSchemaCodegenPlugin>()
configure<JSONSchemaCodegen> {
configFile.set(file("json-schema-codegen/config.json")) // if not in the default location
// inputs { inputFile(file("src/main/resources/schema")) }
val base = "../../ground-platform/schema/src"
inputs {
val base = "../../ground-platform/schema/src"
inputFile(file("$base/loi-document.schema.json"))
inputFile(file("$base/geometry-data.schema.json"))
inputFile(file("$base/point-data.schema.json"))
inputFile(file("$base/polygon-data.schema.json"))
inputFile(file("$base/multi-polygon-data.schema.json"))
inputFile(file("$base/linear-ring-data.schema.json"))
inputFile(file("$base/audit-info.schema.json"))
inputFile(file("$base/submission-document.schema.json"))
inputFile(file("$base/loi-document.json"))
inputFile(file("$base/geometry-object.json"))
inputFile(file("$base/point-object.json"))
inputFile(file("$base/polygon-object.json"))
inputFile(file("$base/multi-polygon-object.json"))
inputFile(file("$base/linear-ring-object.json"))
inputFile(file("$base/coordinates-object.json"))

inputFile(file("$base/submission-document.json"))
inputFile(file("$base/audit-info-object.json"))
}
outputDir.set(file("build/generated-sources/kotlin"))
packageName.set("com.google.ground.shared.schema")
Expand Down

0 comments on commit 17d477b

Please sign in to comment.