kopykat-kt / kopykat

Little utilities for more pleasant immutable data in Kotlin
Other
283 stars 16 forks source link

Type Adapters #76

Open pablisco opened 2 years ago

pablisco commented 2 years ago

When creating copy constructors it would be useful to have mappings between types that are no exactly the same.

For instance, the representation of a date on the network layer may be a string, but it may be a date on the domain.

We could define strategies to work with such conventions:

@TypeAdapter
fun String.toDate(): Date = ...
serras commented 2 years ago

Wouldn't this work by simply having a Date function which takes a String as argument? In a sense, our functions with the same name as the type we want to build are "adapter" between those types already. WDYT?

pablisco commented 2 years ago

Hmm, yeah that could be an interesting idea. My only worry is the look up time. And naming clashes, as there could be the same Date on multiple places. Although, I guess we can determine it by the return type too. And then we don't even need to name it the same. But that would add even more potential clashes.

Need to have a play around with this.

dave08 commented 1 year ago

Does KopyKat already know how to use value class underlying values for copying? On the data layer we need value classes but, when sending the response, Jackson doesn't seem to know what to do with them... so we'd like to use KopyKat to map between them.

serras commented 1 year ago

In principle it should work out of the box, since value classes are just as any other class. If you find a specific case which is not covered, we're happy to look at it and add support for it :)