From 4550fb9382318ad8cdd625e2820153871b137b56 Mon Sep 17 00:00:00 2001 From: piotradamczyk5 <65554637+piotradamczyk5@users.noreply.github.com> Date: Wed, 23 Sep 2020 15:06:15 +0200 Subject: [PATCH] fix: update flank-scripts serialization (#1151) --- .../flank/scripts/ci/releasenotes/GitHubRelease.kt | 2 +- .../scripts/ci/releasenotes/GithubPullRequest.kt | 7 ++----- .../exceptions/FlankScriptsExceptionMappers.kt | 8 +++----- .../scripts/release/updatebugsnag/BugSnagResponse.kt | 2 +- .../scripts/release/updatebugsnag/UpdateBugSnag.kt | 2 +- .../main/kotlin/flank/scripts/utils/Serialization.kt | 10 ++++------ .../src/test/kotlin/flank/scripts/FuelTestRunner.kt | 11 ++++++----- .../kotlin/flank/scripts/utils/SerializationTest.kt | 4 ++-- 8 files changed, 20 insertions(+), 26 deletions(-) diff --git a/flank-scripts/src/main/kotlin/flank/scripts/ci/releasenotes/GitHubRelease.kt b/flank-scripts/src/main/kotlin/flank/scripts/ci/releasenotes/GitHubRelease.kt index 3bae658ffb..82c214d3e2 100644 --- a/flank-scripts/src/main/kotlin/flank/scripts/ci/releasenotes/GitHubRelease.kt +++ b/flank-scripts/src/main/kotlin/flank/scripts/ci/releasenotes/GitHubRelease.kt @@ -11,5 +11,5 @@ data class GitHubRelease( ) object GithubReleaseDeserializable : ResponseDeserializable { - override fun deserialize(content: String) = content.toObject(GitHubRelease.serializer()) + override fun deserialize(content: String): GitHubRelease = content.toObject() } diff --git a/flank-scripts/src/main/kotlin/flank/scripts/ci/releasenotes/GithubPullRequest.kt b/flank-scripts/src/main/kotlin/flank/scripts/ci/releasenotes/GithubPullRequest.kt index 658c284e98..f599322a5c 100644 --- a/flank-scripts/src/main/kotlin/flank/scripts/ci/releasenotes/GithubPullRequest.kt +++ b/flank-scripts/src/main/kotlin/flank/scripts/ci/releasenotes/GithubPullRequest.kt @@ -1,10 +1,9 @@ package flank.scripts.ci.releasenotes import com.github.kittinunf.fuel.core.ResponseDeserializable +import flank.scripts.utils.toObject import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import kotlinx.serialization.decodeFromString -import kotlinx.serialization.json.Json @Serializable data class GithubPullRequest( @@ -21,7 +20,5 @@ data class GithubUser( ) object GithubPullRequestDeserializer : ResponseDeserializable> { - override fun deserialize(content: String): List { - return Json.decodeFromString(content) - } + override fun deserialize(content: String): List = content.toObject() } diff --git a/flank-scripts/src/main/kotlin/flank/scripts/exceptions/FlankScriptsExceptionMappers.kt b/flank-scripts/src/main/kotlin/flank/scripts/exceptions/FlankScriptsExceptionMappers.kt index 29f446bb03..a44ff31b38 100644 --- a/flank-scripts/src/main/kotlin/flank/scripts/exceptions/FlankScriptsExceptionMappers.kt +++ b/flank-scripts/src/main/kotlin/flank/scripts/exceptions/FlankScriptsExceptionMappers.kt @@ -3,18 +3,16 @@ package flank.scripts.exceptions import com.github.kittinunf.fuel.core.FuelError import com.github.kittinunf.fuel.core.isClientError import com.github.kittinunf.result.Result -import flank.scripts.github.GitHubErrorResponse -import flank.scripts.release.updatebugsnag.BugSnagResponse import flank.scripts.utils.toObject fun Result.mapClientError(transform: (E) -> E2) = when (this) { is Result.Success -> this is Result.Failure -> if (error.response.isClientError) Result.Failure(transform(error)) else this } -fun FuelError.toGithubException() = - GitHubException(response.body().asString(APPLICATION_JSON_CONTENT_TYPE).toObject(GitHubErrorResponse.serializer())) + +fun FuelError.toGithubException() = GitHubException(response.body().asString(APPLICATION_JSON_CONTENT_TYPE).toObject()) fun FuelError.toBugsnagException() = - BugsnagException(response.body().asString(APPLICATION_JSON_CONTENT_TYPE).toObject(BugSnagResponse.serializer())) + BugsnagException(response.body().asString(APPLICATION_JSON_CONTENT_TYPE).toObject()) private const val APPLICATION_JSON_CONTENT_TYPE = "application/json" diff --git a/flank-scripts/src/main/kotlin/flank/scripts/release/updatebugsnag/BugSnagResponse.kt b/flank-scripts/src/main/kotlin/flank/scripts/release/updatebugsnag/BugSnagResponse.kt index 14976d6e7a..1f63298de0 100644 --- a/flank-scripts/src/main/kotlin/flank/scripts/release/updatebugsnag/BugSnagResponse.kt +++ b/flank-scripts/src/main/kotlin/flank/scripts/release/updatebugsnag/BugSnagResponse.kt @@ -12,5 +12,5 @@ data class BugSnagResponse( ) object BugSnagResponseDeserializer : ResponseDeserializable { - override fun deserialize(content: String) = content.toObject(BugSnagResponse.serializer()) + override fun deserialize(content: String): BugSnagResponse = content.toObject() } diff --git a/flank-scripts/src/main/kotlin/flank/scripts/release/updatebugsnag/UpdateBugSnag.kt b/flank-scripts/src/main/kotlin/flank/scripts/release/updatebugsnag/UpdateBugSnag.kt index a36fa860e0..b623b6e8a3 100644 --- a/flank-scripts/src/main/kotlin/flank/scripts/release/updatebugsnag/UpdateBugSnag.kt +++ b/flank-scripts/src/main/kotlin/flank/scripts/release/updatebugsnag/UpdateBugSnag.kt @@ -24,7 +24,7 @@ private fun createRequestBody(bugsnagApiKey: String, appVersion: String, githubW builderName = "github-actions", sourceControl = githubActionsSourceControl(appVersion), metadata = mapOf("github_actions_build_url" to githubWorkflowUrl) - ).toJson(BugSnagRequest.serializer()) + ).toJson() private fun githubActionsSourceControl(appVersion: String) = SourceControl( "github", diff --git a/flank-scripts/src/main/kotlin/flank/scripts/utils/Serialization.kt b/flank-scripts/src/main/kotlin/flank/scripts/utils/Serialization.kt index 9d2abad70f..7846c9eb73 100644 --- a/flank-scripts/src/main/kotlin/flank/scripts/utils/Serialization.kt +++ b/flank-scripts/src/main/kotlin/flank/scripts/utils/Serialization.kt @@ -1,7 +1,7 @@ package flank.scripts.utils -import kotlinx.serialization.DeserializationStrategy -import kotlinx.serialization.SerializationStrategy +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json private val json by lazy { @@ -10,8 +10,6 @@ private val json by lazy { } } -fun T.toJson(serializationStrategy: SerializationStrategy) = - json.encodeToString(serializationStrategy, this) +internal inline fun T.toJson() = json.encodeToString(this) -fun String.toObject(deserializationStrategy: DeserializationStrategy) = - json.decodeFromString(deserializationStrategy, this) +internal inline fun String.toObject() = json.decodeFromString(this) diff --git a/flank-scripts/src/test/kotlin/flank/scripts/FuelTestRunner.kt b/flank-scripts/src/test/kotlin/flank/scripts/FuelTestRunner.kt index be2472b457..9b0511c2e0 100644 --- a/flank-scripts/src/test/kotlin/flank/scripts/FuelTestRunner.kt +++ b/flank-scripts/src/test/kotlin/flank/scripts/FuelTestRunner.kt @@ -29,7 +29,7 @@ class FuelTestRunner(klass: Class<*>) : BlockJUnit4ClassRunner(klass) { val url = request.url.toString() return when { url == "https://api.github.com/repos/Flank/flank/git/refs/tags/success" -> request.buildResponse("", 200) - url == "https://api.github.com/repos/flank/flank/releases/latest" && request.headers["Authorization"].contains("token success") -> request.buildResponse(GitHubRelease("v20.08.0").toJson(GitHubRelease.serializer()), 200) + url == "https://api.github.com/repos/flank/flank/releases/latest" && request.headers["Authorization"].contains("token success") -> request.buildResponse(GitHubRelease("v20.08.0").toJson(), 200) url == "https://api.github.com/repos/flank/flank/commits/success/pulls" -> request.buildResponse( Json.encodeToString(githubPullRequestTest), 200) request.isFailedGithubRequest() -> request.buildResponse(githubErrorBody, 422) @@ -52,17 +52,18 @@ class FuelTestRunner(klass: Class<*>) : BlockJUnit4ClassRunner(klass) { )) private fun Request.handleBugsnagResponse() = - if (body.asString("application/json").toObject(BugSnagRequest.serializer()).apiKey == "success") { + if (body.asString("application/json").toObject().apiKey == "success") { buildResponse( - body = BugSnagResponse("success") - .toJson(BugSnagResponse.serializer()), statusCode = 200 + body = BugSnagResponse("success").toJson(), + statusCode = 200 ) } else { buildResponse( body = BugSnagResponse( status = "failure", errors = listOf("errors") - ).toJson(BugSnagResponse.serializer()), statusCode = 422 + ).toJson(), + statusCode = 422 ) } } diff --git a/flank-scripts/src/test/kotlin/flank/scripts/utils/SerializationTest.kt b/flank-scripts/src/test/kotlin/flank/scripts/utils/SerializationTest.kt index 1f9c82a1c0..cb883bac50 100644 --- a/flank-scripts/src/test/kotlin/flank/scripts/utils/SerializationTest.kt +++ b/flank-scripts/src/test/kotlin/flank/scripts/utils/SerializationTest.kt @@ -22,7 +22,7 @@ class SerializationTest { ) // when - val actual = testJson.toObject(GitHubErrorResponse.serializer()) + val actual = testJson.toObject() // then assertThat(actual).isEqualTo(expected) @@ -35,7 +35,7 @@ class SerializationTest { val expected = "{\"provider\":\"a\",\"repository\":\"b\",\"revision\":\"c\"}" // when - val actual = testObject.toJson(SourceControl.serializer()) + val actual = testObject.toJson() // then assertThat(actual).isEqualTo(expected)