Skip to content

Commit

Permalink
fix optional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Jan 5, 2024
1 parent 038ddee commit 82419c1
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions data4k/src/main/kotlin/dev/forkhandles/data/DataContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,22 @@ abstract class DataContainer<CONTENT>(
fun <OUT : Any?, NEXT> required(mapInFn: (OUT) -> NEXT, mapOutFn: (NEXT) -> OUT?) =
property<NEXT, OUT, OUT>(mapInFn, mapOutFn)

fun <OUT : Any> required() = required<OUT, OUT>({ it }, { it })

fun <OUT, NEXT> required(mapInFn: (OUT) -> NEXT) = required(mapInFn) { error("no outbound mapping defined") }

fun <OUT : Any> required() = required<OUT, OUT>({ it }, { it })

fun <IN : Any, OUT : Value<IN>> required(factory: ValueFactory<OUT, IN>) = required(factory::of) { it.value }

/** Optional **/

fun <OUT, NEXT : Any> optional(mapInFn: (OUT) -> NEXT) =
property<NEXT, OUT, OUT>(mapInFn) { error("no outbound mapping defined") }

fun <OUT, NEXT : Any> optional(mapInFn: (OUT) -> NEXT, mapOutFn: (NEXT) -> OUT?) =
property<NEXT?, OUT, OUT>(mapInFn) { it?.let(mapOutFn) }

fun <OUT, NEXT : Any> optional(mapInFn: (OUT) -> NEXT) = optional(mapInFn) { error("no outbound mapping defined") }

fun <OUT> optional() = property<OUT?, OUT, OUT>({ it }, { it })

fun <IN : Any, OUT : Value<IN>> optional(factory: ValueFactory<OUT, IN>): DataProperty<DataContainer<CONTENT>, OUT?> =
property(factory::of) { it?.value }
fun <IN : Any, OUT : Value<IN>> optional(factory: ValueFactory<OUT, IN>) = optional(factory::of) { it.value }

/** Object **/

Expand Down

0 comments on commit 82419c1

Please sign in to comment.