Skip to content

Commit

Permalink
Factor out serializeStepData method
Browse files Browse the repository at this point in the history
  • Loading branch information
albertchae committed Nov 6, 2024
1 parent 654a3be commit 6970f0e
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions inngest/src/main/kotlin/com/inngest/Function.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.inngest

import com.beust.klaxon.Json
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ObjectNode
import java.util.function.BiFunction
Expand Down Expand Up @@ -232,14 +233,8 @@ internal open class InternalInngestFunction(
// that wraps a
// step.run() - how can we prevent that or warn?

val mapper = ObjectMapper()
val jsonString = mapper.writeValueAsString(e.data)

val dataJson = mapper.readTree(jsonString) as? ObjectNode
dataJson?.put("class", e.data!!.javaClass.name)

return StepResult(
data = dataJson ?: e.data,
data = serializeStepData(e.data),
id = e.hashedId,
name = e.id,
op = OpCode.StepRun,
Expand All @@ -264,4 +259,22 @@ internal open class InternalInngestFunction(
// TODO use URL objects for serveUrl instead of strings so we can fetch things like scheme
return configBuilder.build(client.appId, serveUrl)
}

fun serializeStepData(stepData: Any?): JsonNode? {
if (stepData == null) {
return stepData
}

val mapper = ObjectMapper()
val jsonString = mapper.writeValueAsString(stepData)
val readOnlyJson = mapper.readTree(jsonString)

if (!readOnlyJson.isObject) {
return readOnlyJson
}

val writeableJson = mapper.readTree(jsonString) as ObjectNode
writeableJson.put("class", stepData.javaClass.name)
return writeableJson
}
}

0 comments on commit 6970f0e

Please sign in to comment.