infinum / kotlin-jsonapix

JsonApiX is an Android, annotation processor library that was made to transform regular Kotlin classes into their JSON API representations, with the ability to serialize or deserialize them to or from strings.
Apache License 2.0
38 stars 9 forks source link

Feature/primitive attributes model #3

Closed stjepanbanek closed 3 years ago

stjepanbanek commented 3 years ago

In this PR I added the logic to filter out primitive properties of the input model and serialize them in a separate model. In the output, that model would end up under the attributes key.

Considering that there is a lot of going on in this code I will try to summarize the flow so far, so that the PR would be easier to read :).

Input model get transformed in multiple layer to try to fit the JsonApiWrapper interface.

interface JsonApiWrapper<out Model> {
    val data: ResourceObject<Model>?
    val errors: List<String>?
}

ResourceObject is a wrapper for all of the JSON API required parameters for a given model. It contains stuff like: type, id, attributes, relationships, included etc...

interface ResourceObject<out Model> {
    val id: String
    val type: String
    val attributes: AttributesModel?
}

So far, we have managed to account for type, id and attributes. Since type and id are primitive types, there is no issue there, but for the attributes object, we need to create another interface to filter out all of the primitive properties. No extra action is needed on this interface.

interface AttributesModel

For the filtering properties purposes, Ive used Kotlin Poet Metadata API, and made a custom PropertyTypesSeparator class that handles all the work and outputs 2 lists: primitive props list and composite props list. I have used the primitives list to populate the AttributesModel generated object and then placed it inside ResourceObject. Next up are the composite types which will be placed under relationships and included objects.

I hope I have explained enough. For any questions, Im always available :)

stjepanbanek commented 3 years ago

Features from this view model are contained in https://github.com/infinum/android-jsonapix/pull/5