Skip to content

Commit

Permalink
Remove Serde#schema. This is a leftover from a previous design, and i…
Browse files Browse the repository at this point in the history
…t's currently unused. It might look different in future.
  • Loading branch information
slinkydeveloper committed Apr 24, 2024
1 parent a3c4291 commit f97c195
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,7 @@ private constructor(
private val LOG = LogManager.getLogger()
}

fun toHandlerDefinition() =
HandlerDefinition(
handlerSignature.name,
handlerType,
handlerSignature.requestSerde.schema(),
handlerSignature.responseSerde.schema(),
this)
fun toHandlerDefinition() = HandlerDefinition(handlerSignature.name, handlerType, this)

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

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

@Override
Expand Down
77 changes: 68 additions & 9 deletions sdk-common/src/main/java/dev/restate/sdk/common/Serde.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
import java.util.Objects;
import org.jspecify.annotations.Nullable;

/** Interface defining serialization and deserialization of concrete types. */
/**
* Interface defining serialization and deserialization of concrete types.
*
* <p>You can create a custom one using {@link #using(String, ThrowingFunction, ThrowingFunction)}.
*/
public interface Serde<T> {

byte[] serialize(@Nullable T value);
Expand All @@ -30,20 +34,20 @@ default T deserialize(ByteString byteString) {
return deserialize(byteString.toByteArray());
}

// --- Metadata about the serialized/deserialized content

/**
* @return the schema of this object.
* Content-type to use in request/responses.
*
* <p>If null, the SDK assumes the produced output is empty. This might change in the future.
*/
default @Nullable Object schema() {
return null;
}

default @Nullable String contentType() {
return null;
return "application/octet-stream";
}

/**
* Create a {@link Serde} from {@code serializer}/{@code deserializer} lambdas. Before invoking
* the serializer, we check that {@code value} is non-null.
* Like {@link #using(String, ThrowingFunction, ThrowingFunction)}, using content-type {@code
* application/octet-stream}.
*/
static <T> Serde<T> using(
ThrowingFunction<T, byte[]> serializer, ThrowingFunction<byte[], T> deserializer) {
Expand All @@ -59,4 +63,59 @@ public T deserialize(byte[] value) {
}
};
}

/**
* Create a {@link Serde} from {@code serializer}/{@code deserializer} lambdas, tagging with
* {@code contentType}. Before invoking the serializer, we check that {@code value} is non-null.
*/
static <T> Serde<T> using(
String contentType,
ThrowingFunction<T, byte[]> serializer,
ThrowingFunction<byte[], T> deserializer) {
return new Serde<>() {
@Override
public byte[] serialize(T value) {
return serializer.asFunction().apply(Objects.requireNonNull(value));
}

@Override
public T deserialize(byte[] value) {
return deserializer.asFunction().apply(value);
}

@Override
public @Nullable String contentType() {
return contentType;
}
};
}

static <T> Serde<T> withContentType(String contentType, Serde<T> inner) {
return new Serde<>() {
@Override
public byte[] serialize(@Nullable T value) {
return inner.serialize(value);
}

@Override
public ByteString serializeToByteString(@Nullable T value) {
return inner.serializeToByteString(value);
}

@Override
public T deserialize(ByteString byteString) {
return inner.deserialize(byteString);
}

@Override
public T deserialize(byte[] value) {
return inner.deserialize(value);
}

@Override
public @Nullable String contentType() {
return contentType;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,11 @@
public final class HandlerDefinition<O> {
private final String name;
private final HandlerType handlerType;
private final Object inputSchema;
private final Object outputSchema;
private final InvocationHandler<O> handler;

public HandlerDefinition(
String name,
HandlerType handlerType,
Object inputSchema,
Object outputSchema,
InvocationHandler<O> handler) {
public HandlerDefinition(String name, HandlerType handlerType, InvocationHandler<O> handler) {
this.name = name;
this.handlerType = handlerType;
this.inputSchema = inputSchema;
this.outputSchema = outputSchema;
this.handler = handler;
}

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

public Object getInputSchema() {
return inputSchema;
}

public Object getOutputSchema() {
return outputSchema;
}

public InvocationHandler<O> getHandler() {
return handler;
}
Expand All @@ -56,14 +39,11 @@ public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
HandlerDefinition<?> that = (HandlerDefinition<?>) object;
return Objects.equals(name, that.name)
&& Objects.equals(inputSchema, that.inputSchema)
&& Objects.equals(outputSchema, that.outputSchema)
&& Objects.equals(handler, that.handler);
return Objects.equals(name, that.name) && Objects.equals(handler, that.handler);
}

@Override
public int hashCode() {
return Objects.hash(name, inputSchema, outputSchema, handler);
return Objects.hash(name, handler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ void handleWithMultipleServices() {
new ServiceDefinition<>(
"MyGreeter",
ServiceType.SERVICE,
List.of(
new HandlerDefinition<>(
"greet", HandlerType.EXCLUSIVE, null, null, null)))));
List.of(new HandlerDefinition<>("greet", HandlerType.EXCLUSIVE, null)))));

DeploymentManifestSchema manifest = deploymentManifest.manifest();

Expand Down

0 comments on commit f97c195

Please sign in to comment.