brady-aiello / deepprint Goto Github PK
View Code? Open in Web Editor NEWPrint Kotlin data class instances as constructor statements using KSP
License: Apache License 2.0
Print Kotlin data class instances as constructor statements using KSP
License: Apache License 2.0
This will involve
DeepPrint currently just uses 4 space indentation. It would be nice if users could configure indentation to suit other coding styles and needs.
WithAList(
name = "some list",
items = listOf<Int>( 0, 1, 2, 3, 4,),
)
WithAList(
name = "some list",
items = listOf<Int>( 0, 1, 2, 3, 4,),
)
If we have a data class we don't own, we should be able to deep print it somehow.
Given:
data class SomeExternalClass(val name: String, val age: Int)
Not sure the best way to do this.
SomeExternalClass.deepPrint()
exactly once, and place it in a package that matches its declaration, if any instances are the properties of an annotated class, without any modification:@DeepPrint
data class MyClass(val externalClass: SomeExternalClass)
@DeepPrint
data class MyClass(@DeepPrint val externalClass: SomeExternalClass)
@DeepPrint
data class MyClass(@DeepPrint(with = ::myExternalClassDeepPrintFunction)val externalClass: SomeExternalClass)
fun myEternalClassDepPrintFunction(externalClass: SomeExternalClass) {
println("""
SomeExternalClass(
val someProperty = ${externalClass.someProperty}
)
""").trimIndent()
}
Options 1 or 2 would be ideal, as it would require little to no extra work from the caller. The main issue with options 1 and 2 is getting enough information about the externally declared class. KSP is not being run on compiled code, and there is just less type information available. There might be a way to do this still, so it's worth taking another stab at.
Option 3 is probably the easiest to implement, and the most flexible. It would allow for any class from any module to be deep-printable. It does require an extra step. But, depending on the class in question, the benefit might outweigh the cost of writing another function.
Background: https://twitter.com/HandstandSam/status/1625653445045673984
Using KSP is cool because it works on all platforms, and printing should be faster than with reflection. However, the KSP approach is limited because it requires annotation of all data classes in an object tree. Besides adding extra work, it may also be impossible to get everything printing correctly if it contains a data class from an external library.
Making a lighter-weight reflection-based version of the library could cover more use cases for JVM-only consumers.
To start, we'll just cover data classes and primitives, and will add collections in another ticket + PR.
Like tests with lists, which are supported:
WithAList(
name = "some list",
items = listOf<Int>( 0, 1, 2, 3, 4,),
)
We should also support maps:
WithAMap(
name = "some map",
items = mapOf('a' to 5, 'b' to 23, 'c' to 11),
)
Need to support Map
and MutableMap
, both for standalone objects, and as properties of a data class.
Need to support standalone Arrays as well as Arrays as properties of data classes
Need to publish the deep-print-annotations
module and the deep-print-processor
module so they can actually be used ๐
Describe how to:
Make sure to add references
Manual text building is fine, but it would be great if the processor code was more readable.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.