https://kotlinlang.org logo
#announcements
Title
# announcements
a

Alex Kuznetsov

04/19/2020, 7:32 PM
Is there a library that serializes data classes into compilable Kotlin code? Something like this:
Copy code
data class Thing(val color: String, val shape: String)

val myThing = Thing("Red", "Square")
Copy code
serializeToSourceCode(myThing)

output:
Thing(color="Red", shape="Square")
k

Kroppeb

04/19/2020, 7:54 PM
huh?
a

Alex Kuznetsov

04/19/2020, 7:56 PM
@Kroppeb what do you mean?
k

Kroppeb

04/19/2020, 7:57 PM
So you want to print a representation that is valid Kotlin code that would generate the originial object?
a

Alex Kuznetsov

04/19/2020, 7:58 PM
yes. The context: I am replacing an API call with a mock.
Capturing the call's parameters, and what it returns, and spitting out:
k

Kroppeb

04/19/2020, 7:59 PM
Not really possible. The issue is that you'd have to do this recursive. In Python this would be somewhat possible.
I don't understand why you would need this for mock
Maybe #mockk can help you?
a

Alex Kuznetsov

04/19/2020, 8:00 PM
Copy code
every { myApi.callMethod(1, Thing("Red", "Square") } returns LocalDate.of(2020, 4, 30)
So I need to generate these mockk calls
Yes I know this needs to be recursive. And I know this is doable. Just don't want to write it if there already is something.
Thank you for a quick reply!
regarding "Maybe #mockk can help you?" of course it will, I am using
mockk
, I just don't want to type it manually.
too much work
k

Kroppeb

04/19/2020, 8:06 PM
I mean, maybe people in that channel know an other way to solve your problem
Might be something that happens often
a

Alex Kuznetsov

04/19/2020, 8:06 PM
Got it thank you so much!
2 Views