Skip to content

Commit

Permalink
clena up deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
albertchae committed Nov 6, 2024
1 parent 6970f0e commit 988a10e
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions inngest/src/main/kotlin/com/inngest/State.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,7 @@ class State(
val stepResult = node.path("steps").get(hashedId) ?: throw StateNotFound()

if (stepResult.has(fieldName)) {
val objectNode = stepResult.get(fieldName) as? ObjectNode
if(objectNode?.has("class") == true){
val klass = objectNode.remove("class").asText()
val value = mapper.treeToValue(objectNode, Class.forName(klass)) as T
println("what is received data.kclass.qualifiedName?: " + value!!::class.qualifiedName)
return value
}else{
// Handles primitive types
return mapper.treeToValue(stepResult.get(fieldName), type)
}

return deserializeStepData(stepResult.get(fieldName), type)
} else if (stepResult.has("error")) {
val error = mapper.treeToValue(stepResult.get("error"), StepError::class.java)
throw error
Expand All @@ -81,4 +71,16 @@ class State(
// TODO - Check the state is actually null
return null
}

fun <T> deserializeStepData(serializedStepData: JsonNode?, type: Class<T>): T? {
val mapper = ObjectMapper()
if (serializedStepData == null || !serializedStepData.isObject || !serializedStepData.has("class")) {
// handles null and primitives
return mapper.treeToValue(serializedStepData, type)
}

val writeableJson = serializedStepData as ObjectNode
val className = writeableJson.remove("class").asText()
return mapper.treeToValue(writeableJson, Class.forName(className)) as T
}
}

0 comments on commit 988a10e

Please sign in to comment.