-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Optional resolver #67
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
MessiasLima
merged 3 commits into
main
from
add-java-optional-resolver-16690802065028185447
Jun 15, 2026
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/dev/appoutlet/some/resolver/JavaOptionalResolver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package dev.appoutlet.some.resolver | ||
|
|
||
| import dev.appoutlet.some.config.NullableStrategy | ||
| import dev.appoutlet.some.core.ResolverChain | ||
| import dev.appoutlet.some.core.TypeResolver | ||
| import java.util.Optional | ||
| import kotlin.random.Random | ||
| import kotlin.reflect.KClass | ||
| import kotlin.reflect.KType | ||
| import kotlin.reflect.full.createType | ||
| import kotlin.reflect.full.isSubclassOf | ||
|
|
||
| /** | ||
| * Resolves [Optional] types according to the configured [NullableStrategy]. | ||
| * | ||
| * - **AlwaysNull** – always returns [Optional.empty]. | ||
| * - **NeverNull** – always resolves a present [Optional]. | ||
| * - **Random** – returns [Optional.empty] based on the strategy's probability. | ||
| * - **NullOnCircularReference** – returns [Optional.empty] if a circular reference is detected for the optional field. | ||
| * | ||
| * @param nullableStrategy Strategy for resolving optional types. Defaults to [NullableStrategy.default] when null. | ||
| * @param random Random source used by [NullableStrategy.Random]. | ||
| */ | ||
| class JavaOptionalResolver( | ||
| nullableStrategy: NullableStrategy?, | ||
| private val random: Random | ||
| ) : TypeResolver { | ||
| private val nullableStrategy = nullableStrategy ?: NullableStrategy.default | ||
|
|
||
| override fun canResolve(type: KType): Boolean { | ||
| val kClass = type.classifier as? KClass<*> ?: return false | ||
| return kClass.isSubclassOf(Optional::class) | ||
| } | ||
|
|
||
| override fun resolve(type: KType, chain: ResolverChain): Any { | ||
| val valueType = requireNotNull(type.arguments.firstOrNull()?.type) { | ||
| "Star projection not supported in Optional" | ||
| } | ||
|
|
||
| return when (nullableStrategy) { | ||
| is NullableStrategy.AlwaysNull -> Optional.empty<Any>() | ||
| is NullableStrategy.NeverNull -> Optional.ofNullable(chain.resolve(valueType)) | ||
| is NullableStrategy.Random -> { | ||
| if (random.nextDouble() < nullableStrategy.probability) { | ||
| Optional.empty<Any>() | ||
| } else { | ||
| Optional.ofNullable(chain.resolve(valueType)) | ||
| } | ||
| } | ||
| is NullableStrategy.NullOnCircularReference -> { | ||
| val nullableValueType = (valueType.classifier as? KClass<*>)?.createType( | ||
| arguments = valueType.arguments, | ||
| nullable = true | ||
| ) ?: valueType | ||
|
|
||
| Optional.ofNullable(chain.resolve(nullableValueType)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
98 changes: 98 additions & 0 deletions
98
src/test/kotlin/dev/appoutlet/some/resolver/JavaOptionalResolverTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package dev.appoutlet.some.resolver | ||
|
|
||
| import dev.appoutlet.some.config.NullableStrategy | ||
| import dev.appoutlet.some.some | ||
| import java.util.Optional | ||
| import kotlin.reflect.typeOf | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertIs | ||
| import kotlin.test.assertNotNull | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class JavaOptionalResolverTest { | ||
| @Test | ||
| fun `JavaOptionalResolver generates Optional values`() { | ||
| val result: Optional<String> = some() | ||
| assertNotNull(result) | ||
| assertTrue(result.isPresent) | ||
| assertIs<String>(result.get()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `JavaOptionalResolver with AlwaysNull strategy returns empty Optional`() { | ||
| val result: Optional<String> = some { | ||
| strategy(NullableStrategy.AlwaysNull) | ||
| } | ||
| assertNotNull(result) | ||
| assertFalse(result.isPresent) | ||
| } | ||
|
|
||
| @Test | ||
| fun `JavaOptionalResolver with NeverNull strategy always returns present Optional`() { | ||
| repeat(100) { | ||
| val result: Optional<String> = some { | ||
| strategy(NullableStrategy.NeverNull) | ||
| } | ||
| assertNotNull(result) | ||
| assertTrue(result.isPresent) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `JavaOptionalResolver with Random strategy can return empty or present Optional`() { | ||
| val results = (1..100).map { | ||
| some<Optional<String>> { | ||
| strategy(NullableStrategy.Random(probability = 0.5)) | ||
| } | ||
| } | ||
| assertTrue(results.any { !it.isPresent }) | ||
| assertTrue(results.any { it.isPresent }) | ||
| } | ||
|
|
||
| data class OptionalCircular(val optional: Optional<OptionalCircular>) | ||
|
|
||
| @Test | ||
| fun `JavaOptionalResolver handles circular references with empty Optional`() { | ||
| val result: OptionalCircular = some { | ||
| strategy(NullableStrategy.NullOnCircularReference) | ||
| } | ||
|
|
||
| assertNotNull(result) | ||
| // Since Optional<OptionalCircular> is treated as OptionalCircular?, | ||
| // and we are at the first level of circularity for OptionalCircular, | ||
| // it should resolve to a non-null OptionalCircular, but wrapped in Optional. | ||
|
|
||
| // Wait, if it's the first time it sees OptionalCircular, it should resolve it. | ||
| // If it's a circular reference, it should be null. | ||
|
|
||
| // Let's re-examine how ResolverChain works. | ||
| // resolve(OptionalCircular) | ||
| // -> ClassResolver resolves OptionalCircular | ||
| // -> resolve(Optional<OptionalCircular>) | ||
| // -> JavaOptionalResolver | ||
| // -> resolve(OptionalCircular?) | ||
| // -> NullableResolver | ||
| // -> resolve(OptionalCircular) | ||
| // -> CIRCULAR REFERENCE! | ||
|
|
||
| // If NullableStrategy is NullOnCircularReference, resolve(OptionalCircular?) returns null. | ||
| // So Optional.ofNullable(null) is Optional.empty(). | ||
|
|
||
| assertFalse(result.optional.isPresent, "Circular reference should result in empty optional") | ||
| } | ||
|
|
||
| @Test | ||
| fun `JavaOptionalResolver canResolve detects Optional types`() { | ||
| val resolver = JavaOptionalResolver(NullableStrategy.default, kotlin.random.Random.Default) | ||
| assertTrue(resolver.canResolve(typeOf<Optional<String>>())) | ||
| assertTrue(resolver.canResolve(typeOf<Optional<Int>>())) | ||
| } | ||
|
|
||
| @Test | ||
| fun `JavaOptionalResolver rejects non-Optional types`() { | ||
| val resolver = JavaOptionalResolver(NullableStrategy.default, kotlin.random.Random.Default) | ||
| assertFalse(resolver.canResolve(typeOf<String>())) | ||
| assertFalse(resolver.canResolve(typeOf<List<String>>())) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.