Skip to content

Commit

Permalink
Move namespaces. Begin adding some more docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
deusaquilus committed Jan 31, 2024
1 parent c997656 commit d8fbf88
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 13 deletions.
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ Add the following to your build.gradle.kts:

```kotlin
implementation("io.exoquery:pprint-kotlin:1.1.0")

// For Kotlin Multiplatform use:
implementation("io.exoquery:pprint-kotlin-kmp:1.1.0")
```

Then use the library like this:
```kotlin
import io.exoquery.pprint
// For kotlin multiplatform use: import io.exoquery.kmp.pprint

data class Name(val first: String, val last: String)
data class Person(val name: Name, val age: Int)
Expand Down Expand Up @@ -164,7 +168,7 @@ println(pprint(parent, defaultHeight = 10))
The output of the pprint function is not actually a java.lang.String, but a fansi.Str. This
means you can control how it is printed. For example, to print it in black and white simple do:
```kotlin
import io.exoquery.pprint.PPrinter
import io.exoquery.pprint

val p = Person(Name("Joe", "Bloggs"), 42)

Expand All @@ -177,7 +181,7 @@ println(pprint(p).plainText)
In order to extend pprint, subclass the PPrinter class and override the `treeify` function.
For example:
```kotlin
class CustomPPrinter1(config: PPrinterConfig) : PPrinter(config) {
class CustomPPrinter1(val config: PPrinterConfig) : PPrinter(config) {
override fun treeify(x: Any?, escapeUnicode: Boolean, showFieldNames: Boolean): Tree =
when (x) {
is java.time.LocalDate -> Tree.Literal(x.format(DateTimeFormatter.ofPattern("MM/dd/YYYY")))
Expand All @@ -192,6 +196,24 @@ val joe = Person("Joe", LocalDate.of(1981, 1, 1))
println(pp.invoke(joe))
//> Person(name = "Joe", born = 01/01/1981)
```
This printer can then be used as the basis of a custom `pprint`-like user defined function.

> ### Note for Kotlin Multiplatform
> In Kotlin Multiplatform, the PPrinter is parametrized and takes an additional `SerializationStrategy<T>` parameter.
> You can extend it like this:
> ```kotlin
> class CustomPPrinter1<T>(val serializer: SerializationStrategy<T>, val config: PPrinterConfig) : PPrinter<T>(config) {
> ...
> }
>
> // You can then use this class like so:
> @Serializeable data class Person(val name: String, val born: LocalDate)
> val pp = CustomPPrinter1(Person.serializer(), PPrinterConfig())
> val joe = Person("Joe", LocalDate.of(1981, 1, 1))
> println(pp.invoke(joe))
> ```
> TODO finish this example and write docs for how to write a custom pprint function that uses serializer<T> instead of requring passing it in
> You can then use this ()
For nested objects use Tree.Apply and recursively call the treeify method.
```kotlin
Expand All @@ -202,7 +224,7 @@ class MyJavaBean(val a: String, val b: Int) {
}
// Create the custom printer
class CustomPPrinter2(config: PPrinterConfig) : PPrinter(config) {
class CustomPPrinter2(val config: PPrinterConfig) : PPrinter(config) {
override fun treeify(x: Any?, esc: Boolean, names: Boolean): Tree =
when (x) {
// List through the properties of 'MyJavaBean' and recursively call treeify on them.
Expand All @@ -220,7 +242,7 @@ println(pp.invoke(bean))
To print field-names you use Tree.KeyValue:
```kotlin
class CustomPPrinter3(config: PPrinterConfig) : PPrinter(config) {
class CustomPPrinter3(val config: PPrinterConfig) : PPrinter(config) {
override fun treeify(x: Any?, escapeUnicode: Boolean, showFieldNames: Boolean): Tree {
// function to make recursive calls shorter
fun rec(x: Any?) = treeify(x, escapeUnicode, showFieldNames)
Expand All @@ -245,7 +267,7 @@ println(pp.invoke(bean))

Often it is a good idea to honor the `showFieldNames` parameter only display key-values if it is enabled:
```kotlin
class CustomPPrinter4(config: PPrinterConfig) : PPrinter(config) {
class CustomPPrinter4(val config: PPrinterConfig) : PPrinter(config) {
override fun treeify(x: Any?, escapeUnicode: Boolean, showFieldNames: Boolean): Tree {
// function to make recursive calls shorter
fun rec(x: Any?) = treeify(x, escapeUnicode, showFieldNames)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ nexusPublishing {
}

allprojects {
group = "io.exoquery.pprint"
group = "io.exoquery"
version = "1.2.0"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.exoquery
package io.exoquery.kmp

import io.exoquery.fansi.Attrs
import io.exoquery.fansi.Color
import io.exoquery.pprint.PPrinter
import io.exoquery.kmp.pprint.PPrinter
import io.exoquery.pprint.PPrinterConfig

inline fun <reified T> pprint(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.exoquery.pprint
package io.exoquery.kmp.pprint

import io.exoquery.fansi.Attrs
import io.exoquery.pprint.PPrinterBase
import io.exoquery.pprint.PPrinterConfig
import io.exoquery.pprint.Tree
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.descriptors.SerialDescriptor
Expand Down Expand Up @@ -54,7 +57,10 @@ open class PPrinter<T>(val serializer: SerializationStrategy<T>, override val co
value is Sequence<*> && treeifyable is Treeifyable.Elem && treeifyable.serializer is PPrintSequenceSerializer<*> -> {
@Suppress("UNCHECKED_CAST")
val elementSerializer = treeifyable.serializer.element as KSerializer<Any?>
Tree.Apply("Sequence", value.map { treeifyWithSerializer(Treeifyable.Elem(it, elementSerializer), escapeUnicode, showFieldNames) }.iterator())
Tree.Apply("Sequence",
value.map { treeifyWithSerializer(Treeifyable.Elem(it, elementSerializer), escapeUnicode, showFieldNames) }
.iterator()
)
}
treeifyable is Treeifyable.Elem -> {
val encoder = TreeElementEncoder(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.exoquery.pprint
package io.exoquery.kmp.pprint

import io.exoquery.pprint.PPrinterConfig
import io.exoquery.pprint.Tree
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.StructureKind
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.exoquery.pprint
package io.exoquery.kmp.pprint

import io.exoquery.pprint.EncodeHelper
import io.exoquery.pprint.Tree
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerializationException
import kotlinx.serialization.SerializationStrategy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.exoquery.pprint

import io.exoquery.fansi.Str
import io.exoquery.kmp.pprint.PPrinter
import io.kotest.assertions.fail
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationStrategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import io.exoquery.fansi.Color
import io.exoquery.fansi.Console.GREEN
import io.exoquery.fansi.Console.YELLOW
import io.exoquery.fansi.Str
import io.exoquery.kmp.pprint.PPrintSequenceSerializer
import io.exoquery.kmp.pprint.PPrinter
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.exoquery.pprint

import io.exoquery.pprint
import io.exoquery.kmp.pprint.PPrintSequenceSerializer
import io.exoquery.kmp.pprint.PPrinter
import io.exoquery.kmp.pprint
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable

Expand Down

0 comments on commit d8fbf88

Please sign in to comment.