diff --git a/inngest-core/src/main/kotlin/com/inngest/State.kt b/inngest-core/src/main/kotlin/com/inngest/State.kt index 8df9176f..1c92588a 100644 --- a/inngest-core/src/main/kotlin/com/inngest/State.kt +++ b/inngest-core/src/main/kotlin/com/inngest/State.kt @@ -18,13 +18,18 @@ class State(val payloadJson: String) { return sb.toString() } - inline fun getState(hashedId: String): T? { + inline fun getState(hashedId: String): T? = getState(hashedId, T::class.java) + + fun getState( + hashedId: String, + type: Class, + ): T? { val mapper = ObjectMapper() val node: JsonNode = mapper.readTree(payloadJson) val stepResult = node.path("steps").get(hashedId) ?: throw StateNotFound() if (stepResult.has("data")) { val dataNode = stepResult.get("data") - return mapper.treeToValue(dataNode, T::class.java) + return mapper.treeToValue(dataNode, type) } else if (stepResult.has("error")) { // TODO - Parse the error and throw it return null diff --git a/inngest-core/src/main/kotlin/com/inngest/Step.kt b/inngest-core/src/main/kotlin/com/inngest/Step.kt index f8fdfe53..fc6aa5dd 100644 --- a/inngest-core/src/main/kotlin/com/inngest/Step.kt +++ b/inngest-core/src/main/kotlin/com/inngest/Step.kt @@ -26,14 +26,20 @@ class Step(val state: State) { * @param fn the function to run */ inline fun run( + id: String, + noinline fn: () -> T, + ): T = run(id, fn, T::class.java) + + fun run( id: String, fn: () -> T, + type: Class, ): T { val hashedId = state.getHashFromId(id) try { - val stepResult = state.getState(hashedId) - if (stepResult is T) { + val stepResult = state.getState(hashedId, type) + if (stepResult != null) { return stepResult } } catch (e: StateNotFound) { diff --git a/inngest-core/src/test/kotlin/com/inngest/StateTest.kt b/inngest-core/src/test/kotlin/com/inngest/StateTest.kt index e68645f6..5a0e505c 100644 --- a/inngest-core/src/test/kotlin/com/inngest/StateTest.kt +++ b/inngest-core/src/test/kotlin/com/inngest/StateTest.kt @@ -34,7 +34,7 @@ internal class StateTest { val state = State(json) val hashedId = state.getHashFromId("something-not-in-state") assertFailsWith { - state.getState(hashedId) + state.getState(hashedId) } } diff --git a/inngest-spring-boot-demo/build.gradle.kts b/inngest-spring-boot-demo/build.gradle.kts index af6e4c08..722494e5 100644 --- a/inngest-spring-boot-demo/build.gradle.kts +++ b/inngest-spring-boot-demo/build.gradle.kts @@ -33,4 +33,3 @@ dependencyManagement { tasks.withType { useJUnitPlatform() } - diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java index ed7856f1..bab481b2 100644 --- a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java +++ b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java @@ -6,22 +6,6 @@ import java.time.Duration; import java.util.HashMap; -import com.fasterxml.jackson.annotation.JsonProperty; - - -class Result { - @JsonProperty("sum") - private final int sum; - - public Result(@JsonProperty("sum") int sum) { - this.sum = sum; - } - - public int getSum() { - return sum; - } -} - // NOTE: We probably don't need this singleton anymore // when revisiting the SDK's interface. public class InngestSingleton { @@ -33,33 +17,31 @@ public static synchronized CommHandler getInstance() { FunctionTrigger[] triggers = {fnTrigger}; FunctionOptions fnConfig = new FunctionOptions("fn-id-slug", "My function!", triggers); - Function2 handler = (ctx, step) -> { + Function2> handler = (ctx, step) -> { int x = 10; System.out.println("-> handler called " + ctx.getEvent().getName()); - // Steps types are problematic for now as it's not possible to called - // reified types from Java. - -// int y = step.run("add-ten", () -> x + 10); + int y = step.run("add-ten", () -> x + 10, Integer.class); -// Result res = step.run("cast-to-type-add-ten", () -> { -// System.out.println("-> running step 1!! " + x); -// // throw new Exception("An error!"); -// return new Result(y + 10); -// }); + Result res = step.run("cast-to-type-add-ten", () -> { + System.out.println("-> running step 1!! " + x); + return new Result(y + 10); + }, Result.class); -// System.out.println("res" + res); -// int add = step.run("add-one-hundred", () -> { -// System.out.println("-> running step 2 :) " + (res != null ? res.getSum() : "")); -// return (res != null ? res.getSum() : 0) + 100; -// }); + System.out.println("res" + res); + int add = step.run("add-one-hundred", () -> { + System.out.println("-> running step 2 :) " + (res != null ? res.sum : "")); + return (res != null ? res.sum : 0) + 100; + }, Integer.class); step.sleep("wait-one-sec", Duration.ofSeconds(2)); -// step.run("last-step", () -> (res != null ? res.getSum() : 0) * add); + step.run("last-step", () -> (res != null ? res.sum : 0) * add, Integer.class); - return null; + return new HashMap() {{ + put("message", "cool - this finished running"); + }}; }; InngestFunction function = new InngestFunction(fnConfig, handler); diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/Result.java b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/Result.java new file mode 100644 index 00000000..01a87978 --- /dev/null +++ b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/Result.java @@ -0,0 +1,12 @@ +package com.inngest.springbootdemo; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Result { + @JsonProperty("sum") + public final int sum; + + public Result(@JsonProperty("sum") int sum) { + this.sum = sum; + } +}