Skip to content

Commit

Permalink
WIP prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
gino-m committed Feb 24, 2024
1 parent 75e41ed commit abafd21
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 180 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.geometry.Geometry
import com.google.ground.shared.schema.geometry.MultiPolygon
import com.google.ground.shared.schema.geometry.Point
import com.google.ground.shared.schema.geometry.Polygon
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.gson.GsonBuilder
import com.google.gson.JsonArray
import com.google.gson.JsonDeserializationContext
Expand All @@ -36,7 +36,9 @@ class ObjectMapper {
fun <T : Any> toObject(map: Map<String, Any>, kotlinClass: KClass<T>): T {

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

class GeometryDeserializer : JsonDeserializer<Geometry> {
class GeometryDeserializer : JsonDeserializer<GeometryData> {
override fun deserialize(
json: JsonElement,
typeOfT: Type,
context: JsonDeserializationContext
): Geometry {
val type = json.asJsonObject.get("type").asString
): GeometryData {
val type = json.asJsonObject["type"].asString
val kotlinClass =
geometryClassesByType[type] ?: throw JsonParseException("Unknown geometry type $type")
return context.deserialize(json, kotlinClass.java)
}
}

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

import com.google.ground.shared.schema.geometry.Point
import com.google.ground.shared.schema.geometry.Polygon
import com.google.ground.shared.schema.loi.LoiDocument
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.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlin.test.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.junit.MockitoJUnitRunner
import java.math.BigDecimal
import kotlin.test.assertEquals

@RunWith(MockitoJUnitRunner::class)
class ObjectMapperTest {
Expand All @@ -33,7 +33,7 @@ class ObjectMapperTest {
jobId = "123",
customId = "foo",
submissionCount = 42,
geometry = Point(coordinates = xy(1234.5678, 9876.5432))
geometry = PointData(coordinates = xy(1234.5678, 9876.5432))
)
val actual = ObjectMapper().toObject(json.toMap(), LoiDocument::class)
assertEquals(expected, actual)
Expand Down Expand Up @@ -68,7 +68,7 @@ class ObjectMapperTest {
customId = "foo",
submissionCount = 42,
geometry =
Polygon(
PolygonData(
coordinates = listOf(listOf(xy(0.0, 0.0), xy(1.0, 0.0), xy(1.0, 1.0), xy(0.0, 0.0)))
)
)
Expand All @@ -81,4 +81,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): List<BigDecimal> = listOf(x.toBigDecimal(), y.toBigDecimal())
internal fun xy(x: Double, y: Double) = GeoPoint(y, x)
16 changes: 14 additions & 2 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,19 @@ buildscript {
apply<JSONSchemaCodegenPlugin>()

configure<JSONSchemaCodegen> {
inputs { inputFile(file("../../ground-platform/schema/src")) }
configFile.set(file("json-schema-codegen/config.json")) // if not in the default location
// inputs { inputFile(file("src/main/resources/schema")) }
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"))
}
outputDir.set(file("build/generated-sources/kotlin"))
packageName.set("com.google.ground.schema")
packageName.set("com.google.ground.shared.schema")
}
9 changes: 9 additions & 0 deletions shared/json-schema-codegen/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"customClasses": {
"extension": {
"x-kt-type": {
"SubmissionData": "com.google.ground.shared.schema.SubmissionData"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.ground.shared.schema

// TODO(#2256): Update schema so that values can be strongly typed.
typealias SubmissionData = Map<String, Any>

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions shared/src/main/resources/schema/config.json

This file was deleted.

0 comments on commit abafd21

Please sign in to comment.