Skip to content

Allow value nullability for ValueCache #217

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

Merged
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: 2 additions & 1 deletion src/main/java/org/dataloader/ValueCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.dataloader.annotations.PublicSpi;
import org.dataloader.impl.CompletableFutureKit;
import org.dataloader.impl.NoOpValueCache;
import org.jspecify.annotations.Nullable;
import org.jspecify.annotations.NullMarked;

import java.util.ArrayList;
Expand Down Expand Up @@ -40,7 +41,7 @@
*/
@PublicSpi
@NullMarked
public interface ValueCache<K, V> {
public interface ValueCache<K, V extends @Nullable Object> {

/**
* Creates a new value cache, using the default no-op implementation.
Expand Down
15 changes: 15 additions & 0 deletions src/test/kotlin/org/dataloader/KotlinExamples.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.dataloader

import java.util.concurrent.CompletableFuture
import org.junit.jupiter.api.Test
import reactor.core.publisher.Flux
import java.util.concurrent.CompletableFuture.completedFuture
import org.dataloader.impl.NoOpValueCache

/**
* Some Kotlin code to prove that are JSpecify annotations work here
Expand Down Expand Up @@ -81,6 +83,19 @@ class KotlinExamples {
standardNullableAsserts(dataLoader)
}

@Test
fun `basic kotlin test of nullable value types in value cache`() {
val valueCache = object : ValueCache<String, String?> by NoOpValueCache() {
override fun get(key: String): CompletableFuture<String?> = if (key == "null")
completedFuture(null)
else
completedFuture(key)
}

assert(valueCache["key"].get() == "key")
assert(valueCache["null"].get() == null)
}

private fun standardNullableAsserts(dataLoader: DataLoader<String, String?>) {
val cfA = dataLoader.load("A")
val cfB = dataLoader.load("B")
Expand Down