https://github.com/davidafsilva/pretty-string
Kotlin's data class toString pretty cousin
https://github.com/davidafsilva/pretty-string
data-class kotlin pretty tostring
Last synced: about 1 year ago
JSON representation
Kotlin's data class toString pretty cousin
- Host: GitHub
- URL: https://github.com/davidafsilva/pretty-string
- Owner: davidafsilva
- License: bsd-3-clause
- Created: 2022-03-13T12:15:16.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-01-07T17:18:43.000Z (over 3 years ago)
- Last Synced: 2025-02-09T23:17:11.295Z (about 1 year ago)
- Topics: data-class, kotlin, pretty, tostring
- Language: Kotlin
- Homepage:
- Size: 113 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pretty-string
[](https://github.com/davidafsilva/pretty-string/actions/workflows/main-build.yml)
[](https://coveralls.io/github/davidafsilva/pretty-string)
[](https://repo1.maven.org/maven2/pt/davidafsilva/jvm/kotlin/pretty-string/)
[](https://opensource.org/licenses/BSD-3-Clause)
This small Kotlin library provides an alternative to the regular `toString` implementation that is generated for data
classes, which does not support prettifying its output right out-of-the-box.
## Table of Contents
* [Usage](#usage)
+ [Import](#import)
- [Gradle](#gradle)
- [Maven](#maven)
+ [API](#api)
- [Example](#example)
* [Building](#building)
## Usage
### Import
1. Add maven central repository to your configuration
2. Import the library
#### Gradle
Groovy:
```kotlin
repositories {
mavenCentral()
}
dependencies {
implementation("pt.davidafsilva.jvm.kotlin:pretty-string:0.1.0")
// be sure to include kotlin-reflect as one of your kotlin dependencies, if you don't have it already
// implementation(kotlin("reflect"))
}
```
#### Maven
```xml
pt.davidafsilva.jvm.kotlin
pretty-string
0.1.0
```
### API
This public API is very small. In fact, it only exposes single `toPrettyString()` top-level function. It is defined
as an extension of `Any?`, thus you should be able to call it with any receiver.
#### Example
Let's say you have the following data classes definitions:
```kotlin
data class Person(
val fullName: String,
val birthDate: LocalDate,
val children: List = emptyList(),
val socialProfiles: List = emptyList(),
)
data class SocialProfile(
val name: String,
val url: String
)
```
And you have the following instance of it:
```kotlin
val person = Person(
fullName = "John Doe",
birthDate = LocalDate.of(2000, Month.JANUARY, 31),
children = listOf(
Person(fullName = "John Doe Jr.", birthDate = LocalDate.of(2022, Month.MARCH, 13))
)
)
```
Calling `person.toPrettyString()` should return something along the lines of:
```text
Person@3e3abc88(
fullName = "John Doe",
birthDate = "2000-01-31",
children = [
Person@7857fe2(
fullName = "John Doe Jr.",
birthDate = "2022-03-13",
children = [],
socialProfiles = [],
),
],
socialProfiles = [],
)
```
Optionally, you can specify the indentation to be applied during the formatting via the `indentationWidth` parameter.
When omitted, it defaults to `2`.
## Building
At the project root, run the following command:
```shell
./gradlew clean build
```
The above command will run both the tests and verification checks.
## Disclaimer
The function has support for what I consider to be the bare minimum to properly prettify most of the data class
definitions we (developers) usually create. If there's something missing and you'd like to see it supported, feel free
to file an issue or just go ahead an open the PR with its support :)