what can I use to serialize a data class to compil...
# getting-started
r
what can I use to serialize a data class to compilable kotlin code? Just toString() and some manual editing? Is there something nice?
m
I’m not sure I understand your question: you want to serialize a data class to kotlin source code?
j
If you really want to generate kotlin code, I recommend Kotlin Poet.
toString
is meant for human readable text, the exact format is not supposed to be stable
r
yes I have test code where I cannot use mocks but real objects. Handling raw json literals or files in the tests is bad for maintenance. So I thought, deserialize from json and output it as kotlin source code and then add that as the test code
does the poet verse data classes tersely?
r
Won't that too be "bad for maintenance"? If you want a maintanable source of real objects, then I would write it manually. After all, only manually written code is really maintainable. As for how to do it so it's not a pain: write a builder pattern for everything that has everything optional, and for the unspecified parts either specifies a reasonable default, or generates a reasonable value.
☝️ 1
r
yes, we use builders a lot and even have a test DSL, just for these objects I wanted to cheat out on that work. Maintaining object literals is better than editing json. For example you get compile errors when fields are added to the data class etc
the test DSL we have does not cover this family of data classes in my system
and in the cases of data classes, there will be no difference between generated code and manually written code. The objects are pretty large and complex...
r
A code in tests that nobody wants to touch, is imho an unmaintainable code. Whenever it is due to it being complex, old, unreadable, or simply due to being too large object initialization...
What I'm trying to say is: you do your generation thing and everything works fine. What happens if something breaks? Well the next guy is going to go to the generation thing, fix the issue there, and completely re-generate every object. Basically, you introduced additional layer of complexity to tests (which should be as simple as possible/reasonable), and with it being code generation, this layer is not exactly simple either...
r
the generator will not be checked-in
its a one-off thing. After that it will be manually maintained
I agree builders and test DSL are very elegant and by far the best. I just wanted to test out how it looks like with these objects initialised as plain code (the test data needs to have non-default values for many fields, so the DSL needs to be built which takes time... )
it is just a large variations of different objects with many fields. I'll try to hack something together using reflection and poet and see how it looks
just for fun
j
As you wish, but if you feel like generating code as a one-off thing, probably the code shouldn't be written like this in the first place. If it's really that much boilerplate to write, maybe you should instead extend your test DSL so it's easier to write
1
r
i'm thinking to create a small lib which serializes any data class to code, I will keep that code for some use cases
so one-off in the project scope but will keep it for some uses... I just wanted to know if that already exists
and I agree on your points.. I guess i'm hell-bent on generating code just as a fun exercise. lets see what will actually be best in the long run.
and I will extend the test DSL soon
j
What's the use case for such a lib? I think I don't understand what you want to generate and from what
If I understand correctly, the input of your lib would be an actual runtime instance of a data class, and the output would be the equivalent code that could have created that instance. Is this correct?
If that's the case, I don't understand why the generated code couldn't be directly whatever code you used to create the initial instances in the first place 🤔
r
python has
repr()
which what I sought.. I think json has served javascript well also in some non data-interchange cases. So it is more the trick to serialize to code which is useful (but not maybe in my case as discussed above)
I use JSON to read the instances
so they are created from that serialization format
so repr(foo) yields valid python code to create foo
repr.kt
m
If I understood correctly, you want a one-shot code generation from JSON to Kotlin, to use in tests. So, avoiding complications and custom code, the easiest solution to me would be using an Intellij plugin that generates data classes from JSON, for example: https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-
k
That's an interesting plugin, possibly useful in the future for me too. I see in the demo video that it creates a Kotlin class from the JSON keys, but does it also create an instance from the JSON values? (I think the latter is what the OP is looking for.)
m
No, as far as I remember the plugin only generates source code from the structure. What the OP wanted is so twisted to me that I still haven’t understood his use case completely, sorry 😂
r
The code I posted in this thread (repr.kt) does what I want. Compare to repr in python or what json is to javascript. Given an instance of a data class print the code that creates it (with the given field values, not structure)
🆗 1