Skip to content

Commit

Permalink
Fold json with precision
Browse files Browse the repository at this point in the history
  • Loading branch information
orangain committed Mar 8, 2024
1 parent 53a7b25 commit 35e4ebe
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ class MyConsoleFolding : ConsoleFolding() {

override fun shouldFoldLine(project: Project, line: String): Boolean {
thisLogger().debug("shouldFoldLine: $line")
return line.startsWith("{") || line.startsWith(" ") || line.startsWith("}")
return isPartOfPrettyJson(line)
}

// override fun shouldBeAttachedToThePreviousLine(): Boolean {
// return false
// }
}

private val prettyJsonPartRegex = Regex("""^([{}]| {2,}(".*": |}))""")
fun isPartOfPrettyJson(line: String): Boolean {
return prettyJsonPartRegex.containsMatchIn(line)
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MyConsoleInputFilter : InputFilter {
val message = messageKey?.let { node.get(it) }
?.asText()

val jsonString = writer.writeValueAsString(node)
val jsonString = prettyPrintJson(node)
// return mutableListOf(
// Pair("[$timestamp] ", contentType),
// Pair(level, contentTypeOf(level, contentType)),
Expand All @@ -74,6 +74,10 @@ private val mapper = jacksonObjectMapper().apply {
}
private val writer = mapper.writer(MyPrettyPrinter())

fun prettyPrintJson(node: JsonNode): String {
return writer.writeValueAsString(node)
}

class MyPrettyPrinter : DefaultPrettyPrinter() {
init {
_objectFieldValueSeparatorWithSpaces = ": "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package io.github.orangain.prettyjsonlog.console

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import junit.framework.TestCase

class FoldingTest : TestCase() {
private data class Param(
val description: String,
val jsonCreator: (ObjectNode) -> Unit
)

private val mapper = jacksonObjectMapper()

private val params = listOf(
Param("Empty object") { },
Param("Simple object") {
it.put("string", "x")
it.put("number", 1)
it.put("true", true)
it.put("false", false)
it.putNull("null")
},
Param("Nested object") {
it.putObject("nested").apply {
put("string", "x")
put("number", 1)
put("true", true)
put("false", false)
putNull("null")
}
},
Param("Array") {
it.putArray("array").apply {
add("x")
add(1)
add(true)
add(false)
addNull()
addObject().apply {
put("foo", "bar")
}
}
},
Param("Nested array") {
it.putObject("nested").apply {
putArray("array").apply {
add("x")
add(1)
add(true)
add(false)
addNull()
}
}
},
Param("Array with many elements") {
it.putArray("array").apply {
for (i in 1..100) {
add(i)
}
}
},
)

fun testFolding() {
params.forEach { param ->
val node = mapper.createObjectNode().apply(param.jsonCreator)
assertAllLinesFolded(param.description, node)
}
}

private fun assertAllLinesFolded(description: String, node: JsonNode) {
val json = prettyPrintJson(node)
println(json)
val lines = json.lines()
assertTrue("[$description] Lines should not be empty", lines.isNotEmpty())
lines.forEachIndexed { index, line ->
assertTrue("[$description] Line $index should be folded", isPartOfPrettyJson(line))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.orangain.prettyjsonlog.console

import junit.framework.TestCase


class NotFoldingTest : TestCase() {
private data class Param(
val description: String,
val text: String
)

private val params = listOf(
Param("Log message", "[12:34:56.789] INFO: This is a log message"),
Param("Empty line", ""),
Param(
"Multiline SQL", """
SELECT
id,
name
FROM
table
WHERE
id = 1
""".trimIndent()
),
Param("Starts with space and double quote", """ "foo" """),
)

fun testNotFolding() {
params.forEach { param ->
assertAllLinesNotFolded(param.description, param.text)
}
}

private fun assertAllLinesNotFolded(description: String, text: String) {
val lines = text.lines()
assertTrue("[$description] Lines should not be empty", lines.isNotEmpty())
lines.forEachIndexed { index, line ->
assertFalse("[$description] Line $index should not be folded", isPartOfPrettyJson(line))
}
}
}

0 comments on commit 35e4ebe

Please sign in to comment.