Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Serde nullability annotations #361

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions sdk-api/src/main/java/dev/restate/sdk/AwakeableHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package dev.restate.sdk;

import dev.restate.sdk.common.Serde;
import org.jspecify.annotations.NonNull;

/** This class represents a handle to an {@link Awakeable} created in another service. */
public interface AwakeableHandle {
Expand All @@ -21,7 +20,7 @@ public interface AwakeableHandle {
* @param payload the result payload. MUST NOT be null.
* @see Awakeable
*/
<T> void resolve(Serde<T> serde, @NonNull T payload);
<T> void resolve(Serde<T> serde, T payload);

/**
* Complete with failure the {@link Awakeable}.
Expand Down
22 changes: 11 additions & 11 deletions sdk-api/src/main/java/dev/restate/sdk/JsonSerdes.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.jspecify.annotations.Nullable;
import org.jspecify.annotations.NonNull;

/**
* Collection of common serializers/deserializers.
Expand All @@ -30,7 +30,7 @@ public abstract class JsonSerdes {
private JsonSerdes() {}

/** {@link Serde} for {@link String}. This writes and reads {@link String} as JSON value. */
public static Serde<String> STRING =
public static Serde<@NonNull String> STRING =
usingJackson(
JsonGenerator::writeString,
p -> {
Expand All @@ -42,7 +42,7 @@ private JsonSerdes() {}
});

/** {@link Serde} for {@link Boolean}. This writes and reads {@link Boolean} as JSON value. */
public static Serde<Boolean> BOOLEAN =
public static Serde<@NonNull Boolean> BOOLEAN =
usingJackson(
JsonGenerator::writeBoolean,
p -> {
Expand All @@ -51,7 +51,7 @@ private JsonSerdes() {}
});

/** {@link Serde} for {@link Byte}. This writes and reads {@link Byte} as JSON value. */
public static Serde<Byte> BYTE =
public static Serde<@NonNull Byte> BYTE =
usingJackson(
JsonGenerator::writeNumber,
p -> {
Expand All @@ -60,7 +60,7 @@ private JsonSerdes() {}
});

/** {@link Serde} for {@link Short}. This writes and reads {@link Short} as JSON value. */
public static Serde<Short> SHORT =
public static Serde<@NonNull Short> SHORT =
usingJackson(
JsonGenerator::writeNumber,
p -> {
Expand All @@ -69,7 +69,7 @@ private JsonSerdes() {}
});

/** {@link Serde} for {@link Integer}. This writes and reads {@link Integer} as JSON value. */
public static Serde<Integer> INT =
public static Serde<@NonNull Integer> INT =
usingJackson(
JsonGenerator::writeNumber,
p -> {
Expand All @@ -78,7 +78,7 @@ private JsonSerdes() {}
});

/** {@link Serde} for {@link Long}. This writes and reads {@link Long} as JSON value. */
public static Serde<Long> LONG =
public static Serde<@NonNull Long> LONG =
usingJackson(
JsonGenerator::writeNumber,
p -> {
Expand All @@ -87,7 +87,7 @@ private JsonSerdes() {}
});

/** {@link Serde} for {@link Float}. This writes and reads {@link Float} as JSON value. */
public static Serde<Float> FLOAT =
public static Serde<@NonNull Float> FLOAT =
usingJackson(
JsonGenerator::writeNumber,
p -> {
Expand All @@ -96,7 +96,7 @@ private JsonSerdes() {}
});

/** {@link Serde} for {@link Double}. This writes and reads {@link Double} as JSON value. */
public static Serde<Double> DOUBLE =
public static Serde<@NonNull Double> DOUBLE =
usingJackson(
JsonGenerator::writeNumber,
p -> {
Expand All @@ -108,12 +108,12 @@ private JsonSerdes() {}

private static final JsonFactory JSON_FACTORY = new JsonFactory();

private static <T> Serde<T> usingJackson(
private static <T extends @NonNull Object> Serde<T> usingJackson(
ThrowingBiConsumer<JsonGenerator, T> serializer,
ThrowingFunction<JsonParser, T> deserializer) {
return new Serde<>() {
@Override
public byte[] serialize(@Nullable T value) {
public byte[] serialize(T value) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (JsonGenerator gen = JSON_FACTORY.createGenerator(outputStream)) {
serializer.asBiConsumer().accept(gen, value);
Expand Down
32 changes: 16 additions & 16 deletions sdk-common/src/main/java/dev/restate/sdk/common/Serde.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import dev.restate.sdk.common.function.ThrowingFunction;
import java.nio.ByteBuffer;
import java.util.Objects;
import org.jspecify.annotations.Nullable;
import org.jspecify.annotations.*;

/**
* Interface defining serialization and deserialization of concrete types.
Expand All @@ -22,11 +22,12 @@
*
* <p>You can create a custom one using {@link #using(String, ThrowingFunction, ThrowingFunction)}.
*/
public interface Serde<T> {
@NullMarked
public interface Serde<T extends @Nullable Object> {

byte[] serialize(@Nullable T value);
byte[] serialize(T value);

default ByteBuffer serializeToByteBuffer(@Nullable T value) {
default ByteBuffer serializeToByteBuffer(T value) {
// This is safe because we don't mutate the generated byte[] afterward.
return ByteBuffer.wrap(serialize(value));
}
Expand Down Expand Up @@ -57,7 +58,7 @@ default T deserialize(ByteBuffer byteBuffer) {
* Like {@link #using(String, ThrowingFunction, ThrowingFunction)}, using content-type {@code
* application/octet-stream}.
*/
static <T> Serde<T> using(
static <T extends @NonNull Object> Serde<@NonNull T> using(
ThrowingFunction<T, byte[]> serializer, ThrowingFunction<byte[], T> deserializer) {
return new Serde<>() {
@Override
Expand All @@ -76,7 +77,7 @@ 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(
static <T extends @NonNull Object> Serde<@NonNull T> using(
String contentType,
ThrowingFunction<T, byte[]> serializer,
ThrowingFunction<byte[], T> deserializer) {
Expand All @@ -92,7 +93,7 @@ public T deserialize(byte[] value) {
}

@Override
public @Nullable String contentType() {
public String contentType() {
return contentType;
}
};
Expand All @@ -101,12 +102,12 @@ public T deserialize(byte[] value) {
static <T> Serde<T> withContentType(String contentType, Serde<T> inner) {
return new Serde<>() {
@Override
public byte[] serialize(@Nullable T value) {
public byte[] serialize(T value) {
return inner.serialize(value);
}

@Override
public ByteBuffer serializeToByteBuffer(@Nullable T value) {
public ByteBuffer serializeToByteBuffer(T value) {
return inner.serializeToByteBuffer(value);
}

Expand All @@ -121,22 +122,22 @@ public T deserialize(byte[] value) {
}

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

/** Noop {@link Serde} for void. */
Serde<Void> VOID =
Serde<@Nullable Void> VOID =
new Serde<>() {
@Override
public byte[] serialize(Void value) {
return new byte[0];
}

@Override
public ByteBuffer serializeToByteBuffer(@Nullable Void value) {
public ByteBuffer serializeToByteBuffer(Void value) {
return ByteBuffer.allocate(0);
}

Expand All @@ -151,7 +152,7 @@ public Void deserialize(ByteBuffer byteBuffer) {
}

@Override
public @Nullable String contentType() {
public String contentType() {
return null;
}
};
Expand All @@ -173,9 +174,8 @@ public byte[] deserialize(byte[] value) {
/** Pass through {@link Serde} for {@link ByteBuffer}. */
Serde<ByteBuffer> BYTE_BUFFER =
new Serde<>() {

@Override
public byte[] serialize(@Nullable ByteBuffer byteBuffer) {
public byte[] serialize(ByteBuffer byteBuffer) {
if (byteBuffer == null) {
return new byte[] {};
}
Expand All @@ -188,7 +188,7 @@ public byte[] serialize(@Nullable ByteBuffer byteBuffer) {
}

@Override
public ByteBuffer serializeToByteBuffer(@Nullable ByteBuffer value) {
public ByteBuffer serializeToByteBuffer(ByteBuffer value) {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.restate.sdk.common.Serde;
import java.io.IOException;
import org.jspecify.annotations.Nullable;

/**
* {@link Serde} implementations for Jackson.
Expand Down Expand Up @@ -57,7 +56,7 @@ public static <T> Serde<T> of(Class<T> clazz) {
public static <T> Serde<T> of(ObjectMapper mapper, Class<T> clazz) {
return new Serde<>() {
@Override
public byte[] serialize(@Nullable T value) {
public byte[] serialize(T value) {
try {
return mapper.writeValueAsBytes(value);
} catch (JsonProcessingException e) {
Expand Down Expand Up @@ -92,7 +91,7 @@ public static <T> Serde<T> of(TypeReference<T> typeReference) {
public static <T> Serde<T> of(ObjectMapper mapper, TypeReference<T> typeReference) {
return new Serde<>() {
@Override
public byte[] serialize(@Nullable T value) {
public byte[] serialize(T value) {
try {
return mapper.writeValueAsBytes(value);
} catch (JsonProcessingException e) {
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencyResolutionManagement {
library("opentelemetry-kotlin", "io.opentelemetry", "opentelemetry-extension-kotlin")
.withoutVersion()

library("jspecify", "org.jspecify", "jspecify").version("0.3.0")
library("jspecify", "org.jspecify", "jspecify").version("1.0.0")

library("jwt", "com.nimbusds:nimbus-jose-jwt:9.37.3")
library("tink", "com.google.crypto.tink:tink:1.13.0")
Expand Down
Loading