Skip to content

Commit 16f890d

Browse files
Add basic input/output metadata to discovery
1 parent f97c195 commit 16f890d

File tree

6 files changed

+80
-13
lines changed

6 files changed

+80
-13
lines changed

sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/KtSerdes.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ object KtSerdes {
5353
override fun deserialize(byteString: ByteString) {
5454
return
5555
}
56+
57+
override fun contentType(): String? {
58+
return null
59+
}
5660
}
5761

5862
/** Creates a [Serde] implementation using the `kotlinx.serialization` json module. */
@@ -65,6 +69,10 @@ object KtSerdes {
6569
override fun deserialize(value: ByteArray?): T {
6670
return Json.decodeFromString(serializer, String(value!!, StandardCharsets.UTF_8))
6771
}
72+
73+
override fun contentType(): String {
74+
return "application/json"
75+
}
6876
}
6977
}
7078
}

sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/Service.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,14 @@ private constructor(
121121
private val LOG = LogManager.getLogger()
122122
}
123123

124-
fun toHandlerDefinition() = HandlerDefinition(handlerSignature.name, handlerType, this)
124+
fun toHandlerDefinition() =
125+
HandlerDefinition(
126+
handlerSignature.name,
127+
handlerType,
128+
handlerSignature.requestSerde.contentType() != null,
129+
handlerSignature.requestSerde.contentType(),
130+
handlerSignature.responseSerde.contentType(),
131+
this)
125132

126133
override fun handle(
127134
syscalls: Syscalls,

sdk-api/src/main/java/dev/restate/sdk/Service.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ public BiFunction<Context, REQ, RES> getRunner() {
132132
}
133133

134134
public HandlerDefinition<Service.Options> toHandlerDefinition() {
135-
return new HandlerDefinition<>(this.handlerSignature.name, this.handlerType, this);
135+
return new HandlerDefinition<>(
136+
this.handlerSignature.name,
137+
this.handlerType,
138+
this.handlerSignature.requestSerde.contentType() != null,
139+
this.handlerSignature.requestSerde.contentType(),
140+
this.handlerSignature.responseSerde.contentType(),
141+
this);
136142
}
137143

138144
@Override

sdk-common/src/main/java/dev/restate/sdk/common/syscalls/HandlerDefinition.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,28 @@
1010

1111
import dev.restate.sdk.common.HandlerType;
1212
import java.util.Objects;
13+
import org.jspecify.annotations.Nullable;
1314

1415
public final class HandlerDefinition<O> {
1516
private final String name;
1617
private final HandlerType handlerType;
18+
private final boolean inputRequired;
19+
private final @Nullable String acceptInputContentType;
20+
private final @Nullable String returnedContentType;
1721
private final InvocationHandler<O> handler;
1822

19-
public HandlerDefinition(String name, HandlerType handlerType, InvocationHandler<O> handler) {
23+
public HandlerDefinition(
24+
String name,
25+
HandlerType handlerType,
26+
boolean inputRequired,
27+
@Nullable String acceptInputContentType,
28+
@Nullable String returnedContentType,
29+
InvocationHandler<O> handler) {
2030
this.name = name;
2131
this.handlerType = handlerType;
32+
this.inputRequired = inputRequired;
33+
this.acceptInputContentType = acceptInputContentType;
34+
this.returnedContentType = returnedContentType;
2235
this.handler = handler;
2336
}
2437

@@ -30,6 +43,18 @@ public HandlerType getHandlerType() {
3043
return handlerType;
3144
}
3245

46+
public boolean isInputRequired() {
47+
return inputRequired;
48+
}
49+
50+
public String getAcceptInputContentType() {
51+
return acceptInputContentType;
52+
}
53+
54+
public String getReturnedContentType() {
55+
return returnedContentType;
56+
}
57+
3358
public InvocationHandler<O> getHandler() {
3459
return handler;
3560
}

sdk-core/src/main/java/dev/restate/sdk/core/DeploymentManifest.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010

1111
import dev.restate.sdk.common.HandlerType;
1212
import dev.restate.sdk.common.ServiceType;
13+
import dev.restate.sdk.common.syscalls.HandlerDefinition;
1314
import dev.restate.sdk.common.syscalls.ServiceDefinition;
14-
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
15-
import dev.restate.sdk.core.manifest.Handler;
16-
import dev.restate.sdk.core.manifest.Service;
15+
import dev.restate.sdk.core.manifest.*;
1716
import java.util.stream.Collectors;
1817
import java.util.stream.Stream;
1918

2019
final class DeploymentManifest {
20+
private static final Input EMPTY_INPUT = new Input();
21+
private static final Output EMPTY_OUTPUT = new Output().withSetContentTypeIfEmpty(false);
2122

2223
private final DeploymentManifestSchema manifest;
2324

@@ -37,12 +38,7 @@ public DeploymentManifest(
3738
.withTy(convertServiceType(svc.getServiceType()))
3839
.withHandlers(
3940
svc.getHandlers().stream()
40-
.map(
41-
method ->
42-
new Handler()
43-
.withTy(
44-
convertHandlerType(method.getHandlerType()))
45-
.withName(method.getName()))
41+
.map(DeploymentManifest::convertHandler)
4642
.collect(Collectors.toList())))
4743
.collect(Collectors.toList()));
4844
}
@@ -62,6 +58,24 @@ private static Service.Ty convertServiceType(ServiceType serviceType) {
6258
throw new IllegalStateException();
6359
}
6460

61+
private static Handler convertHandler(HandlerDefinition<?> handler) {
62+
return new Handler()
63+
.withName(handler.getName())
64+
.withTy(convertHandlerType(handler.getHandlerType()))
65+
.withInput(
66+
handler.getAcceptInputContentType() == null
67+
? EMPTY_INPUT
68+
: new Input()
69+
.withRequired(handler.isInputRequired())
70+
.withContentType(handler.getAcceptInputContentType()))
71+
.withOutput(
72+
handler.getReturnedContentType() == null
73+
? EMPTY_OUTPUT
74+
: new Output()
75+
.withContentType(handler.getReturnedContentType())
76+
.withSetContentTypeIfEmpty(false));
77+
}
78+
6579
private static Handler.Ty convertHandlerType(HandlerType handlerType) {
6680
switch (handlerType) {
6781
case EXCLUSIVE:

sdk-core/src/test/java/dev/restate/sdk/core/ComponentDiscoveryHandlerTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ void handleWithMultipleServices() {
3232
new ServiceDefinition<>(
3333
"MyGreeter",
3434
ServiceType.SERVICE,
35-
List.of(new HandlerDefinition<>("greet", HandlerType.EXCLUSIVE, null)))));
35+
List.of(
36+
new HandlerDefinition<>(
37+
"greet",
38+
HandlerType.EXCLUSIVE,
39+
false,
40+
"application/json",
41+
"application/json",
42+
null)))));
3643

3744
DeploymentManifestSchema manifest = deploymentManifest.manifest();
3845

0 commit comments

Comments
 (0)