Skip to content

Commit

Permalink
Add basic input/output metadata to discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper committed Apr 24, 2024
1 parent f97c195 commit 16f890d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ object KtSerdes {
override fun deserialize(byteString: ByteString) {
return
}

override fun contentType(): String? {
return null
}
}

/** Creates a [Serde] implementation using the `kotlinx.serialization` json module. */
Expand All @@ -65,6 +69,10 @@ object KtSerdes {
override fun deserialize(value: ByteArray?): T {
return Json.decodeFromString(serializer, String(value!!, StandardCharsets.UTF_8))
}

override fun contentType(): String {
return "application/json"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ private constructor(
private val LOG = LogManager.getLogger()
}

fun toHandlerDefinition() = HandlerDefinition(handlerSignature.name, handlerType, this)
fun toHandlerDefinition() =
HandlerDefinition(
handlerSignature.name,
handlerType,
handlerSignature.requestSerde.contentType() != null,
handlerSignature.requestSerde.contentType(),
handlerSignature.responseSerde.contentType(),
this)

override fun handle(
syscalls: Syscalls,
Expand Down
8 changes: 7 additions & 1 deletion sdk-api/src/main/java/dev/restate/sdk/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ public BiFunction<Context, REQ, RES> getRunner() {
}

public HandlerDefinition<Service.Options> toHandlerDefinition() {
return new HandlerDefinition<>(this.handlerSignature.name, this.handlerType, this);
return new HandlerDefinition<>(
this.handlerSignature.name,
this.handlerType,
this.handlerSignature.requestSerde.contentType() != null,
this.handlerSignature.requestSerde.contentType(),
this.handlerSignature.responseSerde.contentType(),
this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,28 @@

import dev.restate.sdk.common.HandlerType;
import java.util.Objects;
import org.jspecify.annotations.Nullable;

public final class HandlerDefinition<O> {
private final String name;
private final HandlerType handlerType;
private final boolean inputRequired;
private final @Nullable String acceptInputContentType;
private final @Nullable String returnedContentType;
private final InvocationHandler<O> handler;

public HandlerDefinition(String name, HandlerType handlerType, InvocationHandler<O> handler) {
public HandlerDefinition(
String name,
HandlerType handlerType,
boolean inputRequired,
@Nullable String acceptInputContentType,
@Nullable String returnedContentType,
InvocationHandler<O> handler) {
this.name = name;
this.handlerType = handlerType;
this.inputRequired = inputRequired;
this.acceptInputContentType = acceptInputContentType;
this.returnedContentType = returnedContentType;
this.handler = handler;
}

Expand All @@ -30,6 +43,18 @@ public HandlerType getHandlerType() {
return handlerType;
}

public boolean isInputRequired() {
return inputRequired;
}

public String getAcceptInputContentType() {
return acceptInputContentType;
}

public String getReturnedContentType() {
return returnedContentType;
}

public InvocationHandler<O> getHandler() {
return handler;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

import dev.restate.sdk.common.HandlerType;
import dev.restate.sdk.common.ServiceType;
import dev.restate.sdk.common.syscalls.HandlerDefinition;
import dev.restate.sdk.common.syscalls.ServiceDefinition;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.core.manifest.Handler;
import dev.restate.sdk.core.manifest.Service;
import dev.restate.sdk.core.manifest.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

final class DeploymentManifest {
private static final Input EMPTY_INPUT = new Input();
private static final Output EMPTY_OUTPUT = new Output().withSetContentTypeIfEmpty(false);

private final DeploymentManifestSchema manifest;

Expand All @@ -37,12 +38,7 @@ public DeploymentManifest(
.withTy(convertServiceType(svc.getServiceType()))
.withHandlers(
svc.getHandlers().stream()
.map(
method ->
new Handler()
.withTy(
convertHandlerType(method.getHandlerType()))
.withName(method.getName()))
.map(DeploymentManifest::convertHandler)
.collect(Collectors.toList())))
.collect(Collectors.toList()));
}
Expand All @@ -62,6 +58,24 @@ private static Service.Ty convertServiceType(ServiceType serviceType) {
throw new IllegalStateException();
}

private static Handler convertHandler(HandlerDefinition<?> handler) {
return new Handler()
.withName(handler.getName())
.withTy(convertHandlerType(handler.getHandlerType()))
.withInput(
handler.getAcceptInputContentType() == null
? EMPTY_INPUT
: new Input()
.withRequired(handler.isInputRequired())
.withContentType(handler.getAcceptInputContentType()))
.withOutput(
handler.getReturnedContentType() == null
? EMPTY_OUTPUT
: new Output()
.withContentType(handler.getReturnedContentType())
.withSetContentTypeIfEmpty(false));
}

private static Handler.Ty convertHandlerType(HandlerType handlerType) {
switch (handlerType) {
case EXCLUSIVE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ void handleWithMultipleServices() {
new ServiceDefinition<>(
"MyGreeter",
ServiceType.SERVICE,
List.of(new HandlerDefinition<>("greet", HandlerType.EXCLUSIVE, null)))));
List.of(
new HandlerDefinition<>(
"greet",
HandlerType.EXCLUSIVE,
false,
"application/json",
"application/json",
null)))));

DeploymentManifestSchema manifest = deploymentManifest.manifest();

Expand Down

0 comments on commit 16f890d

Please sign in to comment.