From 17d477b3632dff55ed9412e748010288f0611f2e Mon Sep 17 00:00:00 2001 From: Gino Miceli Date: Sat, 24 Feb 2024 15:27:48 -0500 Subject: [PATCH] Working prototype --- .../remote/firebase/ObjectMapper.kt | 22 ++++++------ .../remote/firebase/ObjectMapperTest.kt | 36 +++++++++++-------- shared/build.gradle.kts | 20 ++++++----- ...missionData.kt => SubmissionDataObject.kt} | 0 4 files changed, 42 insertions(+), 36 deletions(-) rename shared/src/main/kotlin/com/google/ground/shared/schema/{SubmissionData.kt => SubmissionDataObject.kt} (100%) diff --git a/ground/src/main/java/com/google/android/ground/persistence/remote/firebase/ObjectMapper.kt b/ground/src/main/java/com/google/android/ground/persistence/remote/firebase/ObjectMapper.kt index c51cc4860a..a43085d7a6 100644 --- a/ground/src/main/java/com/google/android/ground/persistence/remote/firebase/ObjectMapper.kt +++ b/ground/src/main/java/com/google/android/ground/persistence/remote/firebase/ObjectMapper.kt @@ -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 @@ -36,9 +36,7 @@ class ObjectMapper { fun toObject(map: Map, kotlinClass: KClass): 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) } @@ -72,12 +70,12 @@ class ObjectMapper { } } -class GeometryDeserializer : JsonDeserializer { +class GeometryDeserializer : JsonDeserializer { 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") @@ -87,7 +85,7 @@ class GeometryDeserializer : JsonDeserializer { 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 ) diff --git a/ground/src/test/java/com/google/android/ground/persistence/remote/firebase/ObjectMapperTest.kt b/ground/src/test/java/com/google/android/ground/persistence/remote/firebase/ObjectMapperTest.kt index 34da573966..fa6f6a9d90 100644 --- a/ground/src/test/java/com/google/android/ground/persistence/remote/firebase/ObjectMapperTest.kt +++ b/ground/src/test/java/com/google/android/ground/persistence/remote/firebase/ObjectMapperTest.kt @@ -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 @@ -23,7 +24,7 @@ class ObjectMapperTest { "submissionCount": 42, "geometry": { "type": "Point", - "coordinates": [1234.5678, 9876.5432] + "coordinates": {"x": 1234.5678, "y": 9876.5432} } } """ @@ -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) @@ -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} + ] + } ] } } @@ -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) @@ -81,4 +87,4 @@ internal object MapTypeToken : TypeToken>() internal fun String.toMap(): Map = 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()) diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index 5cae4b2412..4a360c6c8e 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -47,16 +47,18 @@ apply() configure { 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") diff --git a/shared/src/main/kotlin/com/google/ground/shared/schema/SubmissionData.kt b/shared/src/main/kotlin/com/google/ground/shared/schema/SubmissionDataObject.kt similarity index 100% rename from shared/src/main/kotlin/com/google/ground/shared/schema/SubmissionData.kt rename to shared/src/main/kotlin/com/google/ground/shared/schema/SubmissionDataObject.kt