GithubHelp home page GithubHelp logo

davidafsilva / pretty-string Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 116 KB

Kotlin's data class toString pretty cousin

License: BSD 3-Clause "New" or "Revised" License

Kotlin 100.00%
kotlin data-class tostring pretty

pretty-string's Introduction

pretty-string

Main Build Status Coverage Report Latest Release License

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

Import

  1. Add maven central repository to your configuration
  2. Import the library

Gradle

Groovy:

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

<dependencies>
  <dependency>
      <groupId>pt.davidafsilva.jvm.kotlin</groupId>
      <artifactId>pretty-string</artifactId>
      <version>0.1.0</version>
  </dependency>
  <!-- be sure to include kotlin-reflect as one of your kotlin dependencies, if you don't have it already -->
  <!--
  <dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-reflect</artifactId>
  </dependency>
  -->
</dependencies>

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:

data class Person(
    val fullName: String,
    val birthDate: LocalDate,
    val children: List<Person> = emptyList(),
    val socialProfiles: List<SocialProfile> = emptyList(),
)

data class SocialProfile(
    val name: String,
    val url: String
)

And you have the following instance of it:

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:

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:

./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 :)

pretty-string's People

Contributors

davidafsilva avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.