Skip to content

Commit

Permalink
Chaining nullable lenses part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcg committed Dec 30, 2023
1 parent 66bd0b4 commit 9de9031
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/gildedrose/foundation/lenses.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ interface Lens<T, R> : (T) -> R {
operator fun invoke(subject: T, value: R): T = inject(subject, value)
}


infix fun <T1, T2, R> ((T1) -> T2?).andThen(second: (T2) -> R): (T1) -> R? = {
this.invoke(it)?.let { outer -> second.invoke(outer) }
}

infix fun <T1, T2, R> Lens<T1, T2>.andThen(second: Lens<T2, R>): Lens<T1, R> = LensObject(
{ second.get(get(it)) },
{ subject, value ->
Expand All @@ -23,13 +28,13 @@ infix fun <T1, T2, R> Lens<T1, T2>.andThen(second: Lens<T2, R>): Lens<T1, R> = L
)

@JvmName("andThenMaybe")
infix fun <T1, T2, R> Lens<T1, T2?>.andThen(second: Lens<T2, R>): Lens<T1, R?> = LensObject(
infix fun <T1, T2, R> Lens<T1, T2?>.andThen(second: Lens<T2, R?>): Lens<T1, R?> = LensObject(
getter = { get(it)?.let { outer -> second.get(outer) } },
injector = { subject, value ->
val outer = get(subject) ?: error("No parent found to inject into")
inject(
subject,
second.inject(outer, value ?: error("Cannot remove the parent to inject null"))
second.inject(outer, value)
)
}
)
Expand Down
28 changes: 22 additions & 6 deletions src/test/java/com/gildedrose/competition/CompetitionTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,32 @@ class CompetitionTests {
expectThat(reverted).isEqualTo(mapOf("propertyName" to null))
}

@Test
fun `chaining nullable functions`() {
val lens: (PropertySet) -> String? = lens<PropertySet?>("outer") andThen "inner".asLens<String>()

val populatedData = mapOf("outer" to mapOf("inner" to "value"))
expectThat(populatedData[lens]).isEqualTo("value")

val emptyData = mapOf<String, Any?>()
expectThat(emptyData[lens]).isNull()

val outerButNoInner = mapOf("outer" to emptyData)
expectThrows<NoSuchElementException> { outerButNoInner[lens] }
}

@Test
fun `chaining nullable lenses`() {
val lens: Lens<PropertySet, String?> = lens<PropertySet?>("outer") andThen "inner".asLens<String>()
val lens: Lens<PropertySet, String?> = lens<PropertySet?>("outer") andThen "inner".asLens<String?>()

val populatedData = mapOf("outer" to mapOf("inner" to "value"))
expectThat(populatedData[lens]).isEqualTo("value")
expectThat(populatedData.with(lens, "new value")).isEqualTo(
mapOf("outer" to mapOf("inner" to "new value"))
)
expectThrows<IllegalStateException> { populatedData.with(lens, null) }
.message.isEqualTo("Cannot remove the parent to inject null")
expectThat(populatedData.with(lens, null)).isEqualTo(
mapOf("outer" to mapOf("inner" to null))
)

val emptyData = mapOf<String, Any?>()
expectThat(emptyData[lens]).isNull()
Expand All @@ -89,12 +104,13 @@ class CompetitionTests {
.message.isEqualTo("No parent found to inject into")

val outerButNoInner = mapOf("outer" to emptyData)
expectThrows<NoSuchElementException> { outerButNoInner[lens] }
expectThat(outerButNoInner[lens]).isEqualTo(null)
expectThat(outerButNoInner.with(lens, "value")).isEqualTo(
mapOf("outer" to mapOf("inner" to "value"))
)
expectThrows<IllegalStateException> { outerButNoInner.with(lens, null) }
.message.isEqualTo("Cannot remove the parent to inject null")
expectThat(outerButNoInner.with(lens, null)).isEqualTo(
mapOf("outer" to mapOf("inner" to null))
)
}

data class Place(val properties: PropertySet) : PropertySet by properties {
Expand Down

0 comments on commit 9de9031

Please sign in to comment.