Skip to content

Commit

Permalink
Creating manual printer in KMP
Browse files Browse the repository at this point in the history
  • Loading branch information
deusaquilus committed Dec 31, 2024
1 parent 5c915b4 commit c497453
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ allprojects {

allprojects {
group = "io.exoquery"
version = "3.0.0.D"
version = "3.0.0.E"
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,33 @@ import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.serializer

open class PPrinter<T>(open val serializer: SerializationStrategy<T>, override open val config: PPrinterConfig = PPrinterConfig()): PPrinterBase<T>(config) {
abstract class PPrinterKmp<T>(override open val config: PPrinterConfig): PPrinterBase<T>(config) {
open fun <R> treeifyValueOrNull(value: R, elementName: String?, escapeUnicode: Boolean, showFieldNames: Boolean): Tree? =
when {
value == null -> Tree.Literal("null", elementName)
value is Boolean -> Tree.Literal(value.toString(), elementName)
value is Byte -> Tree.Literal(value.toString(), elementName)
value is Short -> Tree.Literal(value.toString(), elementName)
value is Int -> Tree.Literal(value.toString(), elementName)
value is Long -> Tree.Literal("${value}L", elementName)
value is Float -> Tree.Literal("${value}F", elementName)
value is Double -> Tree.Literal(value.toString(), elementName)
value is Char -> EncodeHelperImpl.encodeChar(value, escapeUnicode, elementName)
value is String -> EncodeHelperImpl.encodeString(value, escapeUnicode, elementName)
// Empty iterator case which for some reason causes an exception with the regular kotlin serializer
value is Iterator<*> && !value.hasNext() -> Tree.Literal("empty iterator", elementName)
value is Iterator<*> && value.hasNext() -> Tree.Literal("non-empty iterator", elementName)
else -> null
}
}

open class PPrinterManual<T>(override val config: PPrinterConfig = PPrinterConfig()): PPrinterKmp<T>(config) {
override fun treeify(x: T, elementName: String?, escapeUnicode: Boolean, showFieldNames: Boolean): Tree =
treeifyValueOrNull(x, elementName, escapeUnicode, showFieldNames)
?: Tree.Literal(x.toString(), elementName)
}

open class PPrinter<T>(open val serializer: SerializationStrategy<T>, override open val config: PPrinterConfig = PPrinterConfig()): PPrinterKmp<T>(config) {

companion object {
inline operator fun <reified T> invoke(config: PPrinterConfig = PPrinterConfig()) = PPrinter(serializer<T>(), config)
Expand Down Expand Up @@ -76,24 +102,6 @@ open class PPrinter<T>(open val serializer: SerializationStrategy<T>, override o
Tree.Literal(treeifyable.value.toString(), elementName)
}
}

open fun <R> treeifyValueOrNull(value: R, elementName: String?, escapeUnicode: Boolean, showFieldNames: Boolean): Tree? =
when {
value == null -> Tree.Literal("null", elementName)
value is Boolean -> Tree.Literal(value.toString(), elementName)
value is Byte -> Tree.Literal(value.toString(), elementName)
value is Short -> Tree.Literal(value.toString(), elementName)
value is Int -> Tree.Literal(value.toString(), elementName)
value is Long -> Tree.Literal("${value}L", elementName)
value is Float -> Tree.Literal("${value}F", elementName)
value is Double -> Tree.Literal(value.toString(), elementName)
value is Char -> EncodeHelperImpl.encodeChar(value, escapeUnicode, elementName)
value is String -> EncodeHelperImpl.encodeString(value, escapeUnicode, elementName)
// Empty iterator case which for some reason causes an exception with the regular kotlin serializer
value is Iterator<*> && !value.hasNext() -> Tree.Literal("empty iterator", elementName)
value is Iterator<*> && value.hasNext() -> Tree.Literal("non-empty iterator", elementName)
else -> null
}
}

class PPrintSequenceSerializer<T>(val element: KSerializer<T>) : KSerializer<Sequence<T>> {
Expand Down

0 comments on commit c497453

Please sign in to comment.