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

Improve handling of nullable values in Stores #711

Merged
merged 4 commits into from
Nov 3, 2022
Merged

Conversation

jwstegemann
Copy link
Owner

@jwstegemann jwstegemann commented Nov 2, 2022

Handling nullable values in Stores

If you have a Store with a nullable content, you can use orDefault to derive a non-nullable Store from it, that transparently translates a null-value from its parent Store to the given default-value and vice versa.

In the following case, when you enter some text in the input and remove it again, you will have a value of null in your nameStore:

val nameStore = storeOf<String?>(null)

render {
    input {
        nameStore.orDefault("").also { formStore ->
            value(formStore.data)
            changes.values() handledBy formStore.update
        }
    }
}

In real world, you will often come across nullable attributes of complex entities. Then you can often call orDefault directly on the SubStore you create to use with your form elements:

@Lenses
data class Person(val name: String?)

//...

val applicationStore = storeOf(Person(null))

//...

val nameStore = applicationStore.sub(Person.name()).orDefault("")

Calling sub on a Store with nullable content

To call sub on a nullable Store only makes sense, when you have checked, that its value is not null:

@Lenses
data class Person(val name: String)

//...

val applicationStore = storeOf<Person>(null)

//...

applicationStore.data.render { person ->
    if (person != null) { // if person is null you would get NullPointerExceptions reading or updating its SubStores
        val nameStore = customerStore.sub(Person.name())
        input {
            value(nameStore.data)
            changes.values() handledBy nameStore.update
        }
    }
    else {
        p { + "no customer selected" }
    }
}

@jwstegemann jwstegemann added the enhancement New feature or request label Nov 2, 2022
@jwstegemann jwstegemann added this to the 1.0-RC2 milestone Nov 2, 2022
@jwstegemann jwstegemann requested a review from jamowei November 2, 2022 15:49
@jwstegemann jwstegemann changed the title Improve handline of nullable values in Stores Improve handling of nullable values in Stores Nov 2, 2022
@jwstegemann jwstegemann force-pushed the jwstegemann/nullables branch from a912fc1 to 8f84dc1 Compare November 2, 2022 17:36
added sub on nullable stores with non-nullable lens
@jwstegemann jwstegemann force-pushed the jwstegemann/nullables branch from 8f84dc1 to 4538575 Compare November 2, 2022 17:39
@jamowei jamowei marked this pull request as ready for review November 2, 2022 18:54
@jamowei jamowei merged commit 287f5e9 into master Nov 3, 2022
@jamowei jamowei deleted the jwstegemann/nullables branch November 3, 2022 08:22
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.

Better support / recipe for Null-Pattern of none collection based stores
2 participants