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

Add mapNullable function to Stores and Inspectors #928

Merged
merged 7 commits into from
Jan 6, 2025

Conversation

haukesomm
Copy link
Collaborator

This PR adds a mapNullable function to Stores and Inspectors that works as the reverse equivalent for mapNull.

The function can be used to map non-nullable Stores/Inspectors to nullable ones as follows:

val nonNullableStore: Store<String> = storeOf("")

val nullableStore: Store<String?> = 
    nonNullableStore.mapNullable(placeholder = "Unknown")
//                               ^^^^^^^^^^^^^^^^^^^^^^^
//                               When the parent has the specified placeholder value,
//                               the mapped Store will have `null` as its value.

Closes #927

@haukesomm haukesomm added the enhancement New feature or request label Dec 20, 2024
@haukesomm haukesomm added this to the 1.0-RC20 milestone Dec 20, 2024
@haukesomm haukesomm requested a review from Lysander December 20, 2024 16:05
@haukesomm haukesomm force-pushed the haukesomm/mapNullable branch from e10bdd0 to c4be134 Compare January 6, 2025 15:12
Copy link
Collaborator

@Lysander Lysander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the remarked question first.

internal fun <T> defaultLens(id: String, default: T): Lens<T?, T> = object : Lens<T?, T> {
override val id: String = id
internal fun <T> mapToNonNullLens(default: T): Lens<T?, T> = object : Lens<T?, T> {
override val id: String = ""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure that this is a good idea? If the value of a Lens's id is an empty String, we do not modify the path like for formatting lenses.

How was this function used before?

Does this maybe affect the validation process for generated lenses? Have you checked this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! The parameter has never been used, though. I manually checked every occurrence and made sure it's an empty String. As the function is internal, we can be sure that it is not breaking as well 👍🏻

@Lysander
Copy link
Collaborator

Lysander commented Jan 6, 2025

I think the new function is also missing in the docs:
https://www.fritz2.dev/docs/storemapping/#summary-of-store-mapping-factories

@haukesomm haukesomm force-pushed the haukesomm/mapNullable branch from c4be134 to 01a759d Compare January 6, 2025 15:45
@haukesomm haukesomm merged commit 32dacf2 into master Jan 6, 2025
2 checks passed
@haukesomm haukesomm deleted the haukesomm/mapNullable branch January 6, 2025 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add reverse equivalent for mapNull
2 participants