Skip to content

Commit

Permalink
Add support for JSON->proto conversion.
Browse files Browse the repository at this point in the history
This is the basis for converting from GAE JSON structures to proto "v2"
(that is, the proto structures defined as part of oppia-proto-api). "v1"
conversion (that is, the proto structures used in Oppia Android's data
layer) will happen at a higher level in a downstream PR.
  • Loading branch information
BenHenning committed May 27, 2024
1 parent e250f69 commit 85f2be9
Show file tree
Hide file tree
Showing 7 changed files with 3,017 additions and 2 deletions.
62 changes: 60 additions & 2 deletions scripts/src/java/org/oppia/android/scripts/gae/proto/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_java//java:defs.bzl", "java_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

# TODO: The dependencies in this module are a bit broken with compat/. Needs refactor.

proto_library(
name = "extra_exploration_definitions_proto",
srcs = ["extra_exploration_definitions.proto"],
Expand All @@ -14,3 +13,62 @@ java_proto_library(
visibility = ["//scripts/src/java/org/oppia/android/scripts/gae:__subpackages__"],
deps = [":extra_exploration_definitions_proto"],
)

kt_jvm_library(
name = "json_to_proto_converter",
testonly = True,
srcs = [
"JsonToProtoConverter.kt",
],
visibility = ["//scripts:oppia_script_library_visibility"],
deps = [
":extra_exploration_definitions_java_proto",
":localization_tracker",
"//scripts/src/java/org/oppia/android/scripts/gae/json:model",
"//third_party:oppia_proto_api_java_protos",
"//third_party:org_jetbrains_kotlinx_kotlinx-coroutines-core",
],
)

kt_jvm_library(
name = "localization_tracker",
testonly = True,
srcs = [
"LocalizationTracker.kt",
"OppiaWebTranslationExtractor.kt",
],
visibility = ["//scripts:oppia_script_library_visibility"],
deps = [
":image_downloader",
":proto_version_provider",
"//scripts/src/java/org/oppia/android/scripts/gae/json:model",
"//third_party:moshi",
"//third_party:oppia_proto_api_java_protos",
"//third_party:org_jetbrains_kotlinx_kotlinx-coroutines-core",
],
)

kt_jvm_library(
name = "image_downloader",
testonly = True,
srcs = [
"ImageDownloader.kt",
],
visibility = ["//scripts:oppia_script_library_visibility"],
deps = [
"//scripts/src/java/org/oppia/android/scripts/gae/gcs:api",
"//third_party:org_jetbrains_kotlinx_kotlinx-coroutines-core",
],
)

kt_jvm_library(
name = "proto_version_provider",
testonly = True,
srcs = [
"ProtoVersionProvider.kt",
],
visibility = ["//scripts:oppia_script_library_visibility"],
deps = [
"//third_party:oppia_proto_api_java_protos",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.oppia.android.scripts.gae.proto

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import org.oppia.android.scripts.gae.gcs.GcsService
import java.util.concurrent.ConcurrentHashMap

class ImageDownloader(
private val gcsService: GcsService,
private val coroutineDispatcher: CoroutineDispatcher
) {
private val imageLengths = ConcurrentHashMap<ImageId, Long>()

fun <T> retrieveImageLengthAsync(
imageContainerType: GcsService.ImageContainerType,
imageType: GcsService.ImageType,
entityId: String,
filename: String,
transform: (Int) -> T
): Deferred<T> {
return CoroutineScope(coroutineDispatcher).async {
val imageId = ImageId(imageContainerType, entityId, imageType, filename)
val length = imageLengths.getOrPut(imageId) {
gcsService.fetchImageContentLengthAsync(
imageContainerType, imageType, entityId, filename
).await() ?: -1
}
return@async transform(length.toInt())
}
}

fun retrieveImageContentAsync(
imageContainerType: GcsService.ImageContainerType,
imageType: GcsService.ImageType,
entityId: String,
filename: String
): Deferred<ByteArray?> =
gcsService.fetchImageContentDataAsync(imageContainerType, imageType, entityId, filename)

fun computeImageUrl(
imageContainerType: GcsService.ImageContainerType,
imageType: GcsService.ImageType,
entityId: String,
filename: String
): String = gcsService.computeImageUrl(imageContainerType, imageType, entityId, filename)

private data class ImageId(
val imageContainerType: GcsService.ImageContainerType,
val entityId: String,
val imageType: GcsService.ImageType,
val filename: String
)
}
Loading

0 comments on commit 85f2be9

Please sign in to comment.