Skip to content

Commit

Permalink
[Kotlin 1.4] Update to Kotlin 1.4 (#766)
Browse files Browse the repository at this point in the history
* Update to kotlin 1.4

* Update version for dependencies

* Update compile error

* Update for moshi

* Update to version 1.4.10
  • Loading branch information
kittinunf authored Sep 18, 2020
1 parent c8593bc commit 3ae3a37
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 61 deletions.
22 changes: 12 additions & 10 deletions buildSrc/src/main/kotlin/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ object Fuel {

// Core dependencies
object Kotlin {
const val version = "1.3.70"
const val version = "1.4.10"
const val stdlib = "org.jetbrains.kotlin:kotlin-stdlib:$version"
const val plugin = "kotlin"
const val androidPlugin = "kotlin-android"
const val androidExtensionsPlugin = "kotlin-android-extensions"
}

object Result {
const val version = "2.2.0"
const val version = "3.1.0"
const val dependency = "com.github.kittinunf.result:result:$version"
}

object Json {
const val version = "20180813"
const val version = "20200518"
const val dependency = "org.json:json:$version"
}

Expand Down Expand Up @@ -129,37 +129,39 @@ object AndroidX {

// Modules dependencies
object Forge {
const val version = "1.0.0-alpha2"
const val version = "1.0.0-alpha3"
const val dependency = "com.github.kittinunf.forge:forge:$version"
}

object Gson {
const val version = "2.8.5"
const val version = "2.8.6"
const val dependency = "com.google.code.gson:gson:$version"
}

object Jackson {
const val version = "2.9.9"
const val version = "2.11.2"
const val dependency = "com.fasterxml.jackson.module:jackson-module-kotlin:$version"
}

object KotlinX {
object Coroutines {
const val version = "1.3.3"
const val version = "1.3.9"
val android = "org.jetbrains.kotlinx:kotlinx-coroutines-android:$version"
val jvm = "org.jetbrains.kotlinx:kotlinx-coroutines-core:$version"
}

object Serialization {
const val version = "0.20.0"
const val dependency = "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$version"
const val version = "1.0.0-RC"
const val dependency = "org.jetbrains.kotlinx:kotlinx-serialization-core:$version"
const val plugin = "kotlinx-serialization"
}
}

object Moshi {
const val version = "1.8.0"
const val version = "1.10.0"
const val dependency = "com.squareup.moshi:moshi:$version"

const val codegen = "com.squareup.moshi:moshi-kotlin-codegen:$version"
}

object Reactor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import com.github.kittinunf.forge.Forge
import com.github.kittinunf.forge.core.DeserializedResult
import com.github.kittinunf.forge.core.JSON
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.ResponseHandler
import com.github.kittinunf.fuel.core.Request
import com.github.kittinunf.fuel.core.Response
import com.github.kittinunf.fuel.core.ResponseDeserializable
import com.github.kittinunf.fuel.core.ResponseHandler
import com.github.kittinunf.fuel.core.response
import com.github.kittinunf.result.Result

Expand All @@ -28,13 +28,9 @@ inline fun <reified T : Any> Request.responseObject(noinline deserializer: JSON.
inline fun <reified T : Any> Request.responseObjects(noinline deserializer: JSON.() -> DeserializedResult<T>) = response(forgesDeserializerOf(deserializer))

fun <T : Any> forgeDeserializerOf(deserializer: JSON.() -> DeserializedResult<T>) = object : ResponseDeserializable<T> {
override fun deserialize(content: String): T? {
when (val result = Forge.modelFromJson(content, deserializer)) {
is DeserializedResult.Success -> return result.value
is DeserializedResult.Failure -> throw result.error
}
}
override fun deserialize(content: String): T? = Forge.modelFromJson(content, deserializer).get()
}

fun <T : Any> forgesDeserializerOf(deserializer: JSON.() -> DeserializedResult<T>) = object : ResponseDeserializable<List<T>> {
override fun deserialize(content: String): List<T>? = Forge.modelsFromJson(content, deserializer).map { it.get<T>() }
override fun deserialize(content: String): List<T>? = Forge.modelsFromJson(content, deserializer).get()
}
Original file line number Diff line number Diff line change
@@ -1,59 +1,58 @@
package com.github.kittinunf.fuel.serialization

import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.ResponseHandler
import com.github.kittinunf.fuel.core.Request
import com.github.kittinunf.fuel.core.Response
import com.github.kittinunf.fuel.core.ResponseDeserializable
import com.github.kittinunf.fuel.core.ResponseHandler
import com.github.kittinunf.fuel.core.response
import com.github.kittinunf.result.Result
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
import kotlinx.serialization.serializer
import java.io.InputStream
import java.io.Reader

inline fun <reified T : Any> Request.responseObject(
loader: DeserializationStrategy<T>,
json: Json = Json(JsonConfiguration.Stable),
json: Json = Json { allowStructuredMapKeys = true },
noinline deserializer: (Request, Response, Result<T, FuelError>) -> Unit
) = response(kotlinxDeserializerOf(loader, json), deserializer)

@ImplicitReflectionSerializer
@OptIn(InternalSerializationApi::class)
inline fun <reified T : Any> Request.responseObject(
json: Json = Json(JsonConfiguration.Stable),
json: Json = Json { allowStructuredMapKeys = true },
noinline deserializer: (Request, Response, Result<T, FuelError>) -> Unit
) = responseObject(T::class.serializer(), json, deserializer)

inline fun <reified T : Any> Request.responseObject(
deserializer: ResponseHandler<T>,
loader: DeserializationStrategy<T>,
json: Json = Json(JsonConfiguration.Stable)
json: Json = Json { allowStructuredMapKeys = true }
) = response(kotlinxDeserializerOf(loader, json), deserializer)

@ImplicitReflectionSerializer
@OptIn(InternalSerializationApi::class)
inline fun <reified T : Any> Request.responseObject(
deserializer: ResponseHandler<T>,
json: Json = Json(JsonConfiguration.Stable)
json: Json = Json { allowStructuredMapKeys = true }
) = responseObject(deserializer, T::class.serializer(), json)

inline fun <reified T : Any> Request.responseObject(
loader: DeserializationStrategy<T>,
json: Json = Json(JsonConfiguration.Stable)
json: Json = Json { allowStructuredMapKeys = true }
) = response(kotlinxDeserializerOf(loader, json))

@ImplicitReflectionSerializer
@OptIn(InternalSerializationApi::class)
inline fun <reified T : Any> Request.responseObject(
json: Json = Json(JsonConfiguration.Stable)
json: Json = Json { allowStructuredMapKeys = true }
) = responseObject(T::class.serializer(), json)

inline fun <reified T : Any> kotlinxDeserializerOf(
loader: DeserializationStrategy<T>,
json: Json = Json(JsonConfiguration.Stable)
json: Json = Json { allowStructuredMapKeys = true }
) = object : ResponseDeserializable<T> {
override fun deserialize(content: String): T? = json.parse(loader, content)
override fun deserialize(content: String): T? = json.decodeFromString(loader, content)
override fun deserialize(reader: Reader): T? = deserialize(reader.readText())
override fun deserialize(bytes: ByteArray): T? = deserialize(String(bytes))

Expand All @@ -64,7 +63,7 @@ inline fun <reified T : Any> kotlinxDeserializerOf(
}
}

@ImplicitReflectionSerializer
@OptIn(InternalSerializationApi::class)
inline fun <reified T : Any> kotlinxDeserializerOf(
json: Json = Json(JsonConfiguration.Stable)
json: Json = Json { allowStructuredMapKeys = true }
) = kotlinxDeserializerOf(T::class.serializer(), json)
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import com.github.kittinunf.fuel.serialization.kotlinxDeserializerOf
import com.github.kittinunf.fuel.serialization.responseObject
import com.github.kittinunf.fuel.test.MockHttpTestCase
import com.github.kittinunf.result.Result
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.list
import kotlinx.serialization.builtins.ListSerializer
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.CoreMatchers.instanceOf
import org.hamcrest.CoreMatchers.isA
Expand All @@ -24,7 +23,6 @@ import org.junit.Test
import org.mockserver.matchers.Times
import java.net.HttpURLConnection

@ImplicitReflectionSerializer
class FuelKotlinxSerializationTest : MockHttpTestCase() {

// Model
Expand Down Expand Up @@ -175,7 +173,7 @@ class FuelKotlinxSerializationTest : MockHttpTestCase() {
" ]").withStatusCode(HttpURLConnection.HTTP_OK)
)

Fuel.get(mock.path("issues")).responseObject(loader = IssueInfo.serializer().list) { _, _, result ->
Fuel.get(mock.path("issues")).responseObject(loader = ListSerializer(IssueInfo.serializer())) { _, _, result ->
val issues = result.get()
assertNotEquals(issues.size, 0)
assertThat(issues[0], isA(IssueInfo::class.java))
Expand Down
5 changes: 5 additions & 0 deletions fuel-moshi/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
plugins {
kotlin("kapt")
}

dependencies {
api(project(Fuel.name))

implementation(Moshi.dependency)

testImplementation(project(Fuel.Test.name))
kaptTest(Moshi.codegen)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ inline fun <reified T : Any> Request.responseObject(handler: ResponseHandler<T>)
inline fun <reified T : Any> Request.responseObject() = response(moshiDeserializerOf(T::class.java))

fun <T : Any> moshiDeserializerOf(clazz: Class<T>) = object : ResponseDeserializable<T> {
override fun deserialize(content: String): T? = defaultMoshi
.build()
.adapter(clazz)
.fromJson(content)
override fun deserialize(inputStream: InputStream): T? = defaultMoshi.build().adapter(clazz).fromJson(Okio.buffer(Okio.source(inputStream)))

override fun deserialize(content: String): T? = defaultMoshi.build().adapter(clazz).fromJson(content)
}

inline fun <reified T : Any> moshiDeserializerOf(adapter: JsonAdapter<T>) = object : ResponseDeserializable<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import com.github.kittinunf.fuel.moshi.moshiDeserializerOf
import com.github.kittinunf.fuel.moshi.responseObject
import com.github.kittinunf.fuel.test.MockHttpTestCase
import com.github.kittinunf.result.Result
import com.github.kittinunf.result.Result.Failure
import com.github.kittinunf.result.Result.Success
import com.google.common.reflect.TypeToken
import com.squareup.moshi.FromJson
import com.squareup.moshi.Json
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonClass
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.ToJson
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.CoreMatchers.instanceOf
import org.hamcrest.CoreMatchers.isA
Expand All @@ -28,6 +28,7 @@ import java.net.HttpURLConnection

class FuelMoshiTest : MockHttpTestCase() {

@JsonClass(generateAdapter = true)
data class HttpBinUserAgentModel(var userAgent: String = "")

@Test
Expand Down Expand Up @@ -158,6 +159,7 @@ class FuelMoshiTest : MockHttpTestCase() {
}
}

@JsonClass(generateAdapter = true)
data class IssueInfo(val id: Int, val title: String, val number: Int)

/**
Expand Down Expand Up @@ -187,12 +189,14 @@ class FuelMoshiTest : MockHttpTestCase() {
FINISHED
}

@JsonClass(generateAdapter = true)
data class StageDTO(val stage: Stage)

class StageMoshiAdapter : JsonAdapter<Stage>() {
private class StageAdapter : JsonAdapter<Stage>() {

@FromJson
override fun fromJson(reader: JsonReader): Stage? {
val value = reader.nextString()

return when (value) {
"na" -> Stage.UNKNOWN
"in_progress" -> Stage.IN_PROGRESS
Expand All @@ -201,13 +205,15 @@ class FuelMoshiTest : MockHttpTestCase() {
}
}

@ToJson
override fun toJson(writer: JsonWriter, value: Stage?) {
TODO("not implemented")
}
}

@Test
fun moshiTestCustomAdapterSuccess() {
defaultMoshi.add(TypeToken.of(Stage::class.java).type, StageMoshiAdapter())
defaultMoshi.add(StageAdapter())

mock.apply {
chain(
Expand All @@ -228,23 +234,23 @@ class FuelMoshiTest : MockHttpTestCase() {

assertThat(req, notNullValue())
assertThat(res, notNullValue())
assertThat(res1 as Success, isA(Result.Success::class.java))
assertThat(res1 as Result.Success, isA(Result.Success::class.java))
assertThat(res1.value.stage, equalTo(Stage.UNKNOWN))

val (_, _, res2) = Fuel.get(mock.path("stage2")).responseObject<StageDTO>()

assertThat(res2 as Success, isA(Result.Success::class.java))
assertThat(res2 as Result.Success, isA(Result.Success::class.java))
assertThat(res2.value.stage, equalTo(Stage.IN_PROGRESS))

val (_, _, res3) = Fuel.get(mock.path("stage3")).responseObject<StageDTO>()

assertThat(res3 as Success, isA(Result.Success::class.java))
assertThat(res3 as Result.Success, isA(Result.Success::class.java))
assertThat(res3.value.stage, equalTo(Stage.FINISHED))
}

@Test
fun moshiTestCustomAdapterFailure() {
defaultMoshi.add(TypeToken.of(Stage::class.java).type, StageMoshiAdapter())
defaultMoshi.add(StageAdapter())

mock.apply {
chain(
Expand All @@ -257,7 +263,6 @@ class FuelMoshiTest : MockHttpTestCase() {

assertThat(req, notNullValue())
assertThat(res, notNullValue())
assertThat(res1 as Failure, isA(Result.Failure::class.java))
assertThat(res1.error.exception as IllegalStateException, isA(IllegalStateException::class.java))
assertThat(res1 as Result.Failure, isA(Result.Failure::class.java))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.github.kittinunf.result.map
import com.github.kittinunf.result.mapError
import java.io.InputStream
import java.io.Reader
import kotlin.jvm.Throws

/**
* Generic interface for [Response] deserialization.
Expand Down Expand Up @@ -283,4 +284,6 @@ suspend fun <T : Any, U : Deserializable<T>> Request.awaitResponseResult(deseria

private fun <T : Any, U : Deserializable<T>> serializeFor(result: Result<Response, FuelError>, deserializable: U) =
result.map { (it to deserializable.deserialize(it)) }
.mapError <Pair<Response, T>, Exception, FuelError> { FuelError.wrap(it, result.getOrElse(Response.error())) }
.mapError <Pair<Response, T>, Exception, FuelError> {
FuelError.wrap(it, result.getOrElse { Response.error() })
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ class UploadRequest private constructor(private val wrapped: Request) : Request
* Add all [dataParts] to be written to the [UploadBody]
* @param dataParts [Iterable<DataPart>] the iterable that yields [DataPart]s
*/
operator fun plus(dataParts: Iterable<DataPart>) = dataParts.fold(this, UploadRequest::plus)
operator fun plus(dataParts: Iterable<DataPart>) = dataParts.fold(this) { acc, dataPart ->
acc.plus(dataPart)
}

/**
* Add a [ProgressCallback] to the [requestProgress]
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 3ae3a37

Please sign in to comment.