Skip to content

Commit

Permalink
Special handling for primitive arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
deusaquilus committed Jan 25, 2024
1 parent ab379b7 commit 88cbb96
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/main/kotlin/io/exoquery/pprint/Walker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ interface Walker {

fun treeify(x: Any?, escapeUnicode: Boolean, showFieldNames: Boolean): Tree {
fun treeifySame(x: Any?) = treeify(x, escapeUnicode, showFieldNames)

fun <T> applyArray(name: String, seq: Sequence<T>) =
Tree.Apply(name, seq.map {x -> treeifySame(x)}.iterator())

return when {

x == null -> Tree.Literal("null")
Expand Down Expand Up @@ -133,9 +137,18 @@ interface Walker {
Tree.Literal("non-empty iterator")
}

x is Array<*> -> Tree.Apply("Array", x.asSequence().map {x -> treeify(x, escapeUnicode, showFieldNames)}.iterator())
x is BooleanArray -> applyArray("BooleanArray", x.asSequence())
x is ByteArray -> applyArray("ByteArray", x.asSequence())
x is CharArray -> applyArray("CharArray", x.asSequence())
x is DoubleArray -> applyArray("DoubleArray", x.asSequence())
x is FloatArray -> applyArray("FloatArray", x.asSequence())
x is IntArray -> applyArray("IntArray", x.asSequence())
x is LongArray -> applyArray("LongArray", x.asSequence())
x is ShortArray -> applyArray("ShortArray", x.asSequence())

x is Array<*> -> applyArray("Array", x.asSequence())

x is Sequence<*> -> Tree.Apply("Sequence", x.map {x -> treeify(x, escapeUnicode, showFieldNames)}.iterator())
x is Sequence<*> -> Tree.Apply("Sequence", x.map {x -> treeifySame(x)}.iterator())

x is Pair<*, *> -> {
// We could also do Tree.Infix(treeifySame(x.first), "to", treeifySame(x.second)), so it would be "a to b" not sure if that is better or worse
Expand Down
10 changes: 10 additions & 0 deletions src/test/kotlin/io/exoquery/pprint/HorizontalTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ class HorizontalTests : FunSpec({
test("Iterator") { Check(listOf('1', '2', '3'), "List('1', '2', '3')") }

test("Array") { Check(arrayOf(1, 2, 3), "Array(1, 2, 3)") }

test("BooleanArray") { Check(booleanArrayOf(true, false, true), "BooleanArray(true, false, true)") }
test("ByteArray") { Check(byteArrayOf(1, 2, 3), "ByteArray(1, 2, 3)") }
test("CharArray") { Check(charArrayOf('a', 'b', 'c'), "CharArray('a', 'b', 'c')") }
test("DoubleArray") { Check(doubleArrayOf(1.0, 2.0, 3.0), "DoubleArray(1.0, 2.0, 3.0)") }
test("FloatArray") { Check(floatArrayOf(1.0F, 2.0F, 3.0F), "FloatArray(1.0F, 2.0F, 3.0F)") }
test("IntArray") { Check(intArrayOf(1, 2, 3), "IntArray(1, 2, 3)") }
test("LongArray") { Check(longArrayOf(1L, 2L, 3L), "LongArray(1L, 2L, 3L)") }
test("ShortArray") { Check(shortArrayOf(1, 2, 3), "ShortArray(1, 2, 3)") }

test("Seq") { Check(listOf(1, 2, 3), "List(1, 2, 3)") }
test("List") { Check(listOf("1", "2", "3"), """List("1", "2", "3")""") }
//test("Vector") { Check(Vector('omg, 'wtf, 'bbq), """Vector('omg, 'wtf, 'bbq)""") }
Expand Down

0 comments on commit 88cbb96

Please sign in to comment.