Skip to content

Commit

Permalink
Moved useStrictPrimitiveDecoders check to decode step
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Apr 28, 2024
1 parent 04a03ab commit 9ddb439
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ fun interface Decoder<T> {
fun decoderFor(type: KType): Decoder<*> {
val decoder: Decoder<*> = when (val classifier = type.classifier) {
String::class -> StringDecoder
Boolean::class -> if (useStrictPrimitiveDecoders) StrictBooleanDecoder else BooleanDecoder
Float::class -> if (useStrictPrimitiveDecoders) StrictFloatDecoder else FloatDecoder
Double::class -> if (useStrictPrimitiveDecoders) StrictDoubleDecoder else DoubleDecoder
Int::class -> if (useStrictPrimitiveDecoders) StrictIntDecoder else IntDecoder
Long::class -> if (useStrictPrimitiveDecoders) StrictLongDecoder else LongDecoder
Boolean::class -> BooleanDecoder
Float::class -> FloatDecoder
Double::class -> DoubleDecoder
Int::class -> IntDecoder
Long::class -> LongDecoder
Byte::class -> ByteDecoder
Short::class -> ShortDecoder
List::class -> ListDecoder(decoderFor(type.arguments.first().type!!))
LongArray::class -> LongArrayDecoder(if (useStrictPrimitiveDecoders) StrictLongDecoder else LongDecoder)
IntArray::class -> IntArrayDecoder(if (useStrictPrimitiveDecoders) StrictIntDecoder else IntDecoder)
LongArray::class -> LongArrayDecoder(LongDecoder)
IntArray::class -> IntArrayDecoder(IntDecoder)
Set::class -> SetDecoder(decoderFor(type.arguments.first().type!!))
Map::class -> MapDecoder(decoderFor(type.arguments[1].type!!))
LocalTime::class -> LocalTimeDecoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ object ShortDecoder : Decoder<Short> {
object IntDecoder : Decoder<Int> {
override fun decode(schema: Schema): (Any?) -> Int {
require(schema.type == Schema.Type.INT)
if (Decoder.useStrictPrimitiveDecoders) return { it as Int }
return { value ->
when (value) {
is Int -> value
Expand All @@ -41,13 +42,10 @@ object IntDecoder : Decoder<Int> {
}
}

object StrictLongDecoder : Decoder<Long> {
override fun decode(schema: Schema): (Any?) -> Long = { it as Long }
}

object LongDecoder : Decoder<Long> {
override fun decode(schema: Schema): (Any?) -> Long {
require(schema.type == Schema.Type.LONG)
if (Decoder.useStrictPrimitiveDecoders) return { it as Long }
return { value ->
when (value) {
is Long -> value
Expand All @@ -60,13 +58,10 @@ object LongDecoder : Decoder<Long> {
}
}

object StrictIntDecoder : Decoder<Int> {
override fun decode(schema: Schema): (Any?) -> Int = { it as Int }
}

object DoubleDecoder : Decoder<Double> {
override fun decode(schema: Schema): (Any?) -> Double {
require(schema.type == Schema.Type.DOUBLE)
if (Decoder.useStrictPrimitiveDecoders) return { it as Double }
return { value ->
when (value) {
is Double -> value
Expand All @@ -77,13 +72,10 @@ object DoubleDecoder : Decoder<Double> {
}
}

object StrictDoubleDecoder : Decoder<Double> {
override fun decode(schema: Schema): (Any?) -> Double = { it as Double }
}

object FloatDecoder : Decoder<Float> {
override fun decode(schema: Schema): (Any?) -> Float {
require(schema.type == Schema.Type.FLOAT)
if (Decoder.useStrictPrimitiveDecoders) return { it as Float }
return { value ->
when (value) {
is Float -> value
Expand All @@ -93,13 +85,10 @@ object FloatDecoder : Decoder<Float> {
}
}

object StrictFloatDecoder : Decoder<Float> {
override fun decode(schema: Schema): (Any?) -> Float = { it as Float }
}

object BooleanDecoder : Decoder<Boolean> {
override fun decode(schema: Schema): (Any?) -> Boolean {
require(schema.type == Schema.Type.BOOLEAN)
if (Decoder.useStrictPrimitiveDecoders) return { it as Boolean }
return { value ->
when (value) {
is Boolean -> value
Expand All @@ -108,7 +97,3 @@ object BooleanDecoder : Decoder<Boolean> {
}
}
}

object StrictBooleanDecoder : Decoder<Boolean> {
override fun decode(schema: Schema): (Any?) -> Boolean = { it as Boolean }
}

0 comments on commit 9ddb439

Please sign in to comment.