Skip to content

Commit

Permalink
add missing node types
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Jan 5, 2024
1 parent 1cde6a9 commit 9fda470
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 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.2.0
- **data4k** : Add missing node types

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dev.forkhandles.data

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.ArrayNode
import com.fasterxml.jackson.databind.node.BigIntegerNode
import com.fasterxml.jackson.databind.node.BinaryNode
import com.fasterxml.jackson.databind.node.BooleanNode
import com.fasterxml.jackson.databind.node.BooleanNode.FALSE
import com.fasterxml.jackson.databind.node.BooleanNode.TRUE
Expand All @@ -13,8 +15,10 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory.instance
import com.fasterxml.jackson.databind.node.LongNode
import com.fasterxml.jackson.databind.node.NullNode
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.databind.node.ShortNode
import com.fasterxml.jackson.databind.node.TextNode
import java.math.BigDecimal
import java.math.BigInteger

/**
* Jackson JsonNode-based implementation of the DataContainer
Expand All @@ -37,9 +41,12 @@ open class JsonNodeDataContainer(input: JsonNode) :
is LongNode -> input.longValue()
is FloatNode -> input.floatValue()
is DecimalNode -> input.decimalValue()
is ShortNode -> input.shortValue()
is DoubleNode -> input.doubleValue()
is BinaryNode -> input.binaryValue()
is TextNode -> input.textValue()
is ArrayNode -> input.map(::nodeToValue)
is BigIntegerNode -> input.map(::nodeToValue)
is ObjectNode -> input
is NullNode -> null
else -> error("Invalid node type ${input::class.java}")
Expand All @@ -54,13 +61,14 @@ open class JsonNodeDataContainer(input: JsonNode) :
is Int -> IntNode(this)
is Long -> LongNode(this)
is Float -> FloatNode(this)
is ByteArray -> BinaryNode(this)
is Short -> ShortNode(this)
is BigDecimal -> DecimalNode(this)
is BigInteger -> BigIntegerNode(this)
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import strikt.assertions.isEqualTo
import strikt.assertions.isNull
import strikt.assertions.message
import java.math.BigDecimal
import java.math.BigInteger
import kotlin.reflect.KMutableProperty0

interface MainClassFields<T : SubClassFields> {
Expand All @@ -18,6 +19,7 @@ interface MainClassFields<T : SubClassFields> {
var long: Long
var double: Double
var decimal: BigDecimal
var bigInt: BigInteger
var notAString: String

var mapped: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import dev.forkhandles.data.JsonNodeDataContainer
import java.math.BigDecimal
import java.math.BigInteger

class JsonNodeDataContainerTest : DataContainerContract<JsonNodeDataContainerTest.SubNodeBacked>() {

Expand All @@ -19,6 +20,7 @@ class JsonNodeDataContainerTest : DataContainerContract<JsonNodeDataContainerTes
override var long by required<Long>()
override var double by required<Double>()
override var decimal by required<BigDecimal>()
override var bigInt by required<BigInteger>()
override var notAString by required<String>()
override var listSubClass by list(::SubNodeBacked)
override var list by list<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.forkhandles.lens

import dev.forkhandles.data.MapDataContainer
import java.math.BigDecimal
import java.math.BigInteger

class MapDataContainerTest : DataContainerContract<MapDataContainerTest.SubMap>() {

Expand All @@ -18,6 +19,7 @@ class MapDataContainerTest : DataContainerContract<MapDataContainerTest.SubMap>(
override var double by required<Double>()
override var decimal by required<BigDecimal>()
override var notAString by required<String>()
override var bigInt by required<BigInteger>()

override var mapped by required(String::toInt, Int::toString)

Expand Down

0 comments on commit 9fda470

Please sign in to comment.