Skip to content

Commit

Permalink
fix problem with jackson not replacing node
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Jan 5, 2024
1 parent 62fddf9 commit d03dc44
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
This list is not intended to be all-encompassing - it will document major and breaking API changes with their rationale
when appropriate:

### v2.12.1.1
- **data4k** : Fix problem with Jackson not replacing node

### v2.12.1.0
- **data4k** : Encapsulate data in `content()` function.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ abstract class DataContainer<CONTENT>(
DataProperty<DataContainer<CONTENT>, IN>(
{ existsFn(content, it) },
{ getFn(content, it)?.let { value -> value as OUT }?.let(mapInFn) },
{ name, value -> setFn(content, name, (value as IN?)?.let(mapOutFn)) })
{ name, value -> setFn(content, name, value?.let(mapOutFn)) })
}
6 changes: 3 additions & 3 deletions data4k/src/main/kotlin/dev/forkhandles/data/DataProperty.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import kotlin.reflect.jvm.jvmErasure

open class DataProperty<IN, OUT : Any?>(
private val existsFn: IN.(String) -> Boolean,
private val getFn: IN.(String) -> Any?,
private val setFn: IN.(String, Any?) -> Unit
private val getFn: IN.(String) -> OUT?,
private val setFn: IN.(String, OUT?) -> Unit
) : ReadWriteProperty<IN, OUT> {
@Suppress("UNCHECKED_CAST")
override fun getValue(thisRef: IN, property: KProperty<*>): OUT {
Expand All @@ -20,7 +20,7 @@ open class DataProperty<IN, OUT : Any?>(
else -> throw NoSuchElementException("Field <${property.name}> is missing")
}

property.returnType.jvmErasure.isInstance(result) -> result as OUT
property.returnType.jvmErasure.isInstance(result) -> result

else -> throw NoSuchElementException("Value for field <${property.name}> is not a ${property.returnType.jvmErasure} but ${result.javaClass.kotlin}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ abstract class JsonNodeDataContainer(input: JsonNode) :
DataContainer<JsonNode>(
input,
{ content, it -> content.has(it) },
{ content, it -> content[it]?.let(Companion::nodeToValue) },
{ content, it -> content[it]?.let(::nodeToValue) },
{ node: JsonNode, name, value ->
(node as? ObjectNode)?.also { node.set<JsonNode>(name, value.toNode()) }
(node as? ObjectNode)?.also { node.replace(name, value.toNode()) }
?: error("Invalid node type ${input::class.java}")
}
) {
Expand Down Expand Up @@ -58,8 +58,9 @@ abstract class JsonNodeDataContainer(input: JsonNode) :
is Double -> DoubleNode(this)
is String -> TextNode(this)
is Iterable<*> -> ArrayNode(instance)
.also { map { if (it is JsonNode) it else it.toNode() }.forEach(it::add)
}
.also {
map { if (it is JsonNode) it else it.toNode() }.forEach(it::add)
}

else -> error("Cannot set value of type ${this::class.java}")
}
Expand Down

0 comments on commit d03dc44

Please sign in to comment.