Skip to content

Commit

Permalink
add objects
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Jan 3, 2024
1 parent e376426 commit 77b39bd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ abstract class AbstractLensProp<IN, OUT>(
thisRef.existsFn(property.name) -> throw NoSuchElementException("Value for field <${property.name}> is null")
else -> throw NoSuchElementException("Field <${property.name}> is missing")
}
(property.returnType.jvmErasure == result.javaClass.kotlin) -> result as OUT
property.returnType.jvmErasure.isInstance(result) -> result as OUT

else -> throw NoSuchElementException("Value for field <${property.name}> is not a ${property.returnType.jvmErasure}")
else -> throw NoSuchElementException("Value for field <${property.name}> is not a ${property.returnType.jvmErasure} but ${result.javaClass.kotlin}")
}
}
}

9 changes: 9 additions & 0 deletions lens4k/src/main/kotlin/dev/forkhandles/lens/MapWrapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.forkhandles.lens

abstract class MapWrapper(private val map: Map<String, Any?>) {
class Field<OUT> : AbstractLensProp<MapWrapper, OUT>({ map.containsKey(it) }, { map[it] })
class ListField<IN, OUT>(mapFn: (IN) -> OUT) : AbstractLensProp<MapWrapper, List<OUT>>({ map.containsKey(it) }, {
@Suppress("UNCHECKED_CAST")
(map[it] as List<IN>).map(mapFn)
})
}
4 changes: 0 additions & 4 deletions lens4k/src/main/kotlin/dev/forkhandles/lens/PropertySet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,3 @@ object PropertySets {
injector = { subject, value -> subject.toMutableMap().apply { this[propertyName] = value } }
)
}

abstract class MapWrapper(private val map: Map<String, Any?>) {
class Primitive<OUT> : AbstractLensProp<MapWrapper, OUT>({ map.containsKey(it) }, { map[it] })
}
27 changes: 17 additions & 10 deletions lens4k/src/test/kotlin/dev/forkhandles/lens/MapWrapperTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ import strikt.assertions.message

class MapWrapperTest {

class SubMap(propertySet: Map<String, Any?>) : MapWrapper(propertySet) {
val stringField by Field<String>()
}

class MapBacked(propertySet: Map<String, Any?>) : MapWrapper(propertySet) {
val stringField by Primitive<String>()
val booleanField by Primitive<Boolean>()
val intField by Primitive<Int>()
val longField by Primitive<Long>()
val decimalField by Primitive<Double>()
val notAStringField by Primitive<String>()
val noSuchField by Primitive<String>()
val stringField by Field<String>()
val booleanField by Field<Boolean>()
val intField by Field<Int>()
val longField by Field<Long>()
val decimalField by Field<Double>()
val notAStringField by Field<String>()
val noSuchField by Field<String>()
val listField by ListField(::SubMap)
}

@Test
Expand All @@ -28,8 +33,9 @@ class MapWrapperTest {
"longField" to Long.MAX_VALUE,
"decimalField" to 1.1234,
"notAStringField" to 123,
"objectField" to mapOf(
"stringField" to "string"
"listField" to listOf(
mapOf("stringField" to "string1"),
mapOf("stringField" to "string2"),
)
)
)
Expand All @@ -39,7 +45,8 @@ class MapWrapperTest {
expectThat(mapBacked.intField).isEqualTo(123)
expectThat(mapBacked.longField).isEqualTo(Long.MAX_VALUE)
expectThat(mapBacked.decimalField).isEqualTo(1.1234)
expectThrows<NoSuchElementException> { mapBacked.notAStringField }.message.isEqualTo("Value for field <notAStringField> is not a class kotlin.String")
expectThat(mapBacked.listField.map { it.stringField }).isEqualTo(listOf("string1", "string2"))
expectThrows<NoSuchElementException> { mapBacked.notAStringField }.message.isEqualTo("Value for field <notAStringField> is not a class kotlin.String but class kotlin.Int")
expectThrows<NoSuchElementException> { mapBacked.noSuchField }.message.isEqualTo("Field <noSuchField> is missing")
}
}

0 comments on commit 77b39bd

Please sign in to comment.