https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
d

Daniele B

04/07/2021, 3:45 PM
SqlDelight question: Possible to remove any of this boilerplate code? I have a database table called “Countries”, for which SqlDelight generated a data class “Countries”. I then have a serializable Repository data class “CountryData”, used by Ktor to serialize the Api response. These two data classes have basically the same properties, and at the moment I need to write this code to match the two objects, which seems to be a lot of boilerplate code: I have to write the same property 3 times. Is it possible to simplify in any way?
Copy code
fun LocalDb.getCountriesList() : List<CountryData> {
    return countriesQueries.getCountriesList(
        mapper = { name, population, first_doses, fully_vaccinated ->
            CountryData(name = name, population = population, firstDoses = first_doses, fullyVaccinated = fully_vaccinated)
        }).executeAsList()
}
k

kevin.cianfarini

04/07/2021, 3:47 PM
#squarelibraries
d

Daniele B

04/07/2021, 3:47 PM
ok, I will post it there. Thanks
k

kevin.cianfarini

04/07/2021, 3:48 PM
Just create a function that does this work for you and pass it in as a reference
this::mapQueryToApiModel
d

Daniele B

04/07/2021, 3:58 PM
I just got the answer there by JW. Solution is pretty simple:
Copy code
fun LocalDb.getCountriesList() : List<CountryData> {
    return countriesQueries.getCountriesList(mapper = ::CountryData).executeAsList()
}
k

kevin.cianfarini

04/07/2021, 4:01 PM
That works too!
5 Views