Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path Configuration parsing fix #342

Merged
merged 5 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package dev.hotwire.turbo.config

import android.content.Context
import dev.hotwire.turbo.util.toObject
import com.google.gson.reflect.TypeToken
import dev.hotwire.turbo.util.dispatcherProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -30,29 +28,31 @@ internal class TurboPathConfigurationLoader(val context: Context) : CoroutineSco
loadCachedConfigurationForUrl(url, onCompletion)

launch {
repository.getRemoteConfiguration(url)?.let {
onCompletion(load(it))
cacheConfigurationForUrl(url, load(it))
repository.getRemoteConfiguration(url)?.let { remoteConfigJson ->
repository.parseFromJson(remoteConfigJson)?.let { config ->
jayohms marked this conversation as resolved.
Show resolved Hide resolved
onCompletion(config)
cacheConfigurationForUrl(url, config)
}
}
}
}

private fun loadBundledAssetConfiguration(filePath: String, onCompletion: (TurboPathConfiguration) -> Unit) {
val configuration = repository.getBundledConfiguration(context, filePath)
onCompletion(load(configuration))
val bundledConfigJson = repository.getBundledConfiguration(context, filePath)
repository.parseFromJson(bundledConfigJson)?.let { config ->
onCompletion(config)
}
}

private fun loadCachedConfigurationForUrl(url: String, onCompletion: (TurboPathConfiguration) -> Unit) {
repository.getCachedConfigurationForUrl(context, url)?.let {
onCompletion(load(it))
repository.getCachedConfigurationForUrl(context, url)?.let { cachedConfigJson ->
repository.parseFromJson(cachedConfigJson)?.let { config ->
onCompletion(config)
}
}
}

private fun cacheConfigurationForUrl(url: String, pathConfiguration: TurboPathConfiguration) {
repository.cacheConfigurationForUrl(context, url, pathConfiguration)
}

private fun load(json: String): TurboPathConfiguration {
return json.toObject(object : TypeToken<TurboPathConfiguration>() {})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package dev.hotwire.turbo.config
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.google.gson.reflect.TypeToken
import dev.hotwire.turbo.http.TurboHttpClient
import dev.hotwire.turbo.util.dispatcherProvider
import dev.hotwire.turbo.util.logError
import dev.hotwire.turbo.util.toJson
import dev.hotwire.turbo.util.toObject
import kotlinx.coroutines.withContext
import okhttp3.Request
import java.io.IOException

internal class TurboPathConfigurationRepository {
private val cacheFile = "turbo"
Expand Down Expand Up @@ -66,4 +67,13 @@ internal class TurboPathConfigurationRepository {
String(it.readBytes())
}
}

fun parseFromJson(json: String): TurboPathConfiguration? {
return try {
json.toObject(object : TypeToken<TurboPathConfiguration>() {})
} catch (e: Exception) {
logError("PathConfigurationLoadingException", e)
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import android.os.Build
import androidx.test.core.app.ApplicationProvider
import dev.hotwire.turbo.BaseRepositoryTest
import dev.hotwire.turbo.http.TurboHttpClient
import dev.hotwire.turbo.util.toObject
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -39,38 +37,49 @@ class TurboPathConfigurationRepositoryTest : BaseRepositoryTest() {
val json = repository.getRemoteConfiguration(baseUrl())
assertThat(json).isNotNull()

val config = load(json)
val config = repository.parseFromJson(json!!)
assertThat(config?.rules?.size).isEqualTo(2)
}
}
}

@Test
fun getMalformedRemoteConfiguration() {
enqueueResponse("test-configuration-malformed.json")

runBlocking {
launch(Dispatchers.Main) {
val json = repository.getRemoteConfiguration(baseUrl())
assertThat(json).isNotNull()

val config = repository.parseFromJson(json!!)
assertThat(config).isNull()
}
}
}

@Test
fun getBundledAssetConfiguration() {
val json = repository.getBundledConfiguration(context, "json/test-configuration.json")
assertThat(json).isNotNull()

val config = load(json)
val config = repository.parseFromJson(json)
assertThat(config?.rules?.size).isEqualTo(10)
}

@Test
fun getCachedConfiguration() {
val url = "https://turbo.hotwired.dev/demo/configurations/android-v1.json"
val config = requireNotNull(load(json()))
val config = requireNotNull(repository.parseFromJson(json()))
repository.cacheConfigurationForUrl(context, url, config)

val json = repository.getCachedConfigurationForUrl(context, url)
assertThat(json).isNotNull()

val cachedConfig = load(json)
val cachedConfig = repository.parseFromJson(json!!)
assertThat(cachedConfig?.rules?.size).isEqualTo(1)
}

private fun load(json: String?): TurboPathConfiguration? {
return json?.toObject(object : TypeToken<TurboPathConfiguration>() {})
}

private fun json(): String {
return """
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Not a valid path configuration
Loading