Skip to content

Commit

Permalink
Proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
gino-m committed Nov 7, 2023
1 parent 172107e commit b1c236a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ plugins {
id "com.ncorti.ktfmt.gradle" version "0.11.0"
id "com.google.dagger.hilt.android" version "$hiltVersion" apply false
id "io.gitlab.arturbosch.detekt" version "1.23.0"
id "com.google.protobuf" version "0.9.4" apply false
}

allprojects {
Expand Down
32 changes: 31 additions & 1 deletion ground/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ apply plugin: 'kotlinx-serialization'
apply plugin: 'com.google.dagger.hilt.android'
apply plugin: 'com.google.firebase.firebase-perf'
apply plugin: 'com.google.firebase.crashlytics'

apply plugin: 'com.google.protobuf'
apply from: '../config/checkstyle/checkstyle.gradle'
apply from: '../config/lint/lint.gradle'
apply from: '../config/jacoco/jacoco.gradle'
Expand Down Expand Up @@ -321,6 +321,11 @@ dependencies {
stagingImplementation "androidx.fragment:fragment-testing:$fragmentVersion"

implementation "com.google.guava:guava:31.1-android"

api("com.google.protobuf:protobuf-kotlin-lite:3.25.0")
// TODO: Move protos into shared module and set correct path here.
// testProtobuf(files("lib/protos-test.tar.gz"))
// implementation("com.google.protobuf:protobuf-java-util:3.25.0")
}

// Allow references to generated code.
Expand All @@ -337,3 +342,28 @@ apply plugin: 'com.google.gms.google-services'
kotlin {
jvmToolchain rootProject.jvmToolchainVersion
}

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.25.0"
}
plugins {
create("java") {
artifact = "io.grpc:protoc-gen-grpc-java:1.59.0"
}
}
generateProtoTasks {
all().each {
it.plugins {
create("java") {
option("lite")
}
}
it.builtins {
create("kotlin") {
option("lite")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2023 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.android.ground.persistence.remote.firebase

import com.google.firebase.firestore.DocumentSnapshot
import com.google.protobuf.GeneratedMessageLite
import timber.log.Timber

fun <T : GeneratedMessageLite<*, *>> DocumentSnapshot.copyInto(proto: T?): T? {
val o = proto ?: return null
val map = data
// TODO: Replace `id` with `uuid`?
map?.set("id", id)
map?.forEach { (key, value) ->
try {
val field = o::class.java.getDeclaredField(key + "_")
field.isAccessible = true
// TODO: Handle maps, arrays, GeoPoint, and other types.
field.set(proto, value)
field.isAccessible = false
} catch (e: NoSuchFieldException) {
Timber.v("Skipping unknown field: $key")
} catch (e: IllegalArgumentException) {
// TODO: Add expected and actual
Timber.v("Skipping field with wrong type: $key")
}
}
return proto
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ import com.google.android.ground.model.Survey
import com.google.android.ground.model.imagery.TileSource
import com.google.android.ground.model.job.Job
import com.google.android.ground.persistence.remote.DataStoreException
import com.google.android.ground.persistence.remote.firebase.proto.copyInto
import com.google.android.ground.persistence.remote.firebase.schema.JobConverter.toJob
import com.google.android.ground.proto.survey
import com.google.firebase.firestore.DocumentSnapshot
import kotlinx.collections.immutable.toPersistentList
import kotlinx.collections.immutable.toPersistentMap
import timber.log.Timber

/** Converts between Firestore documents and [Survey] instances. */
internal object SurveyConverter {

@Throws(DataStoreException::class)
fun toSurvey(doc: DocumentSnapshot): Survey {

Timber.e("!!! Survey: " + doc.copyInto(survey {}))

if (!doc.exists()) throw DataStoreException("Missing survey")

val pd =
Expand Down
13 changes: 13 additions & 0 deletions ground/src/main/proto/com/google/ground/survey.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

option java_package = "com.google.android.ground.proto";
option java_multiple_files = true;

message Survey {
string id = 1;
string title = 2;
string description = 3;
// val jobMap: Map<String, Job>,
// val tileSources: List<TileSource> = listOf(),
// val acl: Map<String, String> = mapOf()
}

0 comments on commit b1c236a

Please sign in to comment.