Skip to content

Commit

Permalink
add aptos result json parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Nov 10, 2024
1 parent a46ed54 commit 11fbc3d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ allprojects {
}
// cannot be updated further, problems with underlying library
implementation("com.github.ajalt.clikt:clikt:3.5.4")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.18.+")

testImplementation("junit:junit:4.13.2")
testImplementation("org.opentest4j:opentest4j:1.3.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.move.cli.runConfigurations.aptos

import com.fasterxml.jackson.core.JacksonException
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import org.intellij.lang.annotations.Language

sealed class AptosExitStatus(val message: String) {
class Result(message: String): AptosExitStatus(message)
class Error(message: String): AptosExitStatus(message)
class Malformed(jsonText: String): AptosExitStatus(jsonText)

companion object {
@Throws(JacksonException::class)
fun fromJson(@Language("JSON") json: String): AptosExitStatus {
val parsedResult = JSON_MAPPER.readValue(json, AptosJsonResult::class.java)
return when {
parsedResult.Error != null -> Error(parsedResult.Error)
parsedResult.Result != null -> Result(parsedResult.Result)
else -> Malformed(json)
}
}
}
}

@Suppress("PropertyName")
private data class AptosJsonResult(
val Result: String?,
val Error: String?
)

private val JSON_MAPPER: ObjectMapper = ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerKotlinModule()
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.move.cli.runConfigurations.aptos

import org.move.utils.tests.MvTestBase

class AptosExitStatusTest: MvTestBase() {
fun `test parse result`() {
val status = AptosExitStatus.fromJson(
"""
{
"Result": "my result message"
}
"""
)
check(status is AptosExitStatus.Result)
check(status.message == "my result message")
}

fun `test parse error`() {
val status = AptosExitStatus.fromJson(
"""
{
"Error": "my error message"
}
"""
)
check(status is AptosExitStatus.Error)
check(status.message == "my error message")
}

fun `test parse malformed`() {
val status = AptosExitStatus.fromJson(
"""
{
"Unknown": "unknown"
}
"""
)
check(status is AptosExitStatus.Malformed)
}
}

0 comments on commit 11fbc3d

Please sign in to comment.