-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aec1d4f
commit ec9e99b
Showing
4 changed files
with
65 additions
and
41 deletions.
There are no files selected for viewing
19 changes: 18 additions & 1 deletion
19
lens4k/src/main/kotlin/dev/forkhandles/lens/AbstractWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,19 @@ | ||
package dev.forkhandles.lens | ||
package dev.forkhandles.lens | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
abstract class AbstractWrapper<CONTENT>( | ||
private val contents: CONTENT, | ||
existsFn: (CONTENT, String) -> Boolean, | ||
getFn: (CONTENT, String) -> Any? | ||
) { | ||
private val exists: AbstractWrapper<CONTENT>.(String) -> Boolean = { existsFn(contents, it) } | ||
private val get: AbstractWrapper<CONTENT>.(String) -> Any? = { getFn(contents, it) } | ||
|
||
inner class Field<OUT> : LensProp<AbstractWrapper<CONTENT>, OUT>(exists, get) | ||
|
||
inner class ListField<IN : Any, OUT>(mapFn: (IN) -> OUT) : | ||
LensProp<AbstractWrapper<CONTENT>, List<OUT>>(exists, { (get(it) as List<IN>).map(mapFn) }) | ||
|
||
inner class ObjectField<OUT : AbstractWrapper<CONTENT>>(mapFn: (CONTENT) -> OUT) : | ||
LensProp<AbstractWrapper<CONTENT>, OUT>(exists, { mapFn(get(it) as CONTENT) }) | ||
} |
28 changes: 27 additions & 1 deletion
28
lens4k/src/main/kotlin/dev/forkhandles/lens/JacksonWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,28 @@ | ||
package dev.forkhandles.lens | ||
package dev.forkhandles.lens | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.node.* | ||
|
||
abstract class JacksonWrapper(node: JsonNode) : | ||
AbstractWrapper<JsonNode>( | ||
node, | ||
{ content, it -> content.has(it) }, | ||
{ content, it -> content[it]?.let(::asValue) } | ||
) { | ||
|
||
companion object { | ||
private fun asValue(jsonNode: JsonNode): Any? = when (jsonNode) { | ||
is BooleanNode -> jsonNode.booleanValue() | ||
is IntNode -> jsonNode.intValue() | ||
is LongNode -> jsonNode.longValue() | ||
is DecimalNode -> jsonNode.decimalValue() | ||
is DoubleNode -> jsonNode.doubleValue() | ||
is TextNode -> jsonNode.textValue() | ||
is ArrayNode -> jsonNode.map { asValue(it) } | ||
is ObjectNode -> jsonNode | ||
is NullNode -> null | ||
else -> error("Invalid node type $jsonNode") | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,6 @@ | ||
package dev.forkhandles.lens | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
abstract class AbstractWrapper<CONTENT>( | ||
private val contents: CONTENT, | ||
existsFn: CONTENT.(String) -> Boolean, | ||
getFn: CONTENT.(String) -> Any? | ||
) { | ||
private val exists: AbstractWrapper<CONTENT>.(String) -> Boolean = { contents.existsFn(it) } | ||
private val get: AbstractWrapper<CONTENT>.(String) -> Any? = { contents.getFn(it) } | ||
|
||
inner class Field<OUT> : LensProp<AbstractWrapper<CONTENT>, OUT>(exists, get) | ||
|
||
inner class ListField<IN : Any, OUT>(mapFn: (IN) -> OUT) : | ||
LensProp<AbstractWrapper<CONTENT>, List<OUT>>(exists, { (get(it) as List<IN>).map(mapFn) }) | ||
|
||
inner class ObjectField<OUT : AbstractWrapper<CONTENT>>(mapFn: (CONTENT) -> OUT) : | ||
LensProp<AbstractWrapper<CONTENT>, OUT>(exists, { mapFn(get(it) as CONTENT) }) | ||
} | ||
|
||
abstract class MapWrapper(map: Map<String, Any?>) : | ||
AbstractWrapper<Map<String, Any?>>(map, { map.containsKey(it) }, { map[it] }) | ||
AbstractWrapper<Map<String, Any?>>(map, { content, it -> content.containsKey(it) }, { content, it -> content[it] }) | ||
|
||
|
||
abstract class JacksonWrapper(node: JsonNode) : | ||
AbstractWrapper<JsonNode>(node, { node.has(it) }, { node[it] }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters