Why does Kotlin for Android Developers (the book) need to add extensions parseList again?
Asked Answered
M

2

5

I know Anko provides the functions parseSingle, parseOpt and parseList , I don't understand why the code of Android Developers (the book) need to design extensions parseList again.

Could you tell me? Thanks!

https://github.com/antoniolg/Kotlin-for-Android-Developers/blob/master/app/src/main/java/com/antonioleiva/weatherapp/data/db/ForecastDb.kt

override fun requestForecastByZipCode(zipCode: Long, date: Long) = forecastDbHelper.use {

        val dailyRequest = "${DayForecastTable.CITY_ID} = ? AND ${DayForecastTable.DATE} >= ?"
        val dailyForecast = select(DayForecastTable.NAME)
                .whereSimple(dailyRequest, zipCode.toString(), date.toString())
                .parseList { DayForecast(HashMap(it)) }

}

https://github.com/antoniolg/Kotlin-for-Android-Developers/blob/master/app/src/main/java/com/antonioleiva/weatherapp/extensions/DatabaseExtensions.kt

fun <T : Any> SelectQueryBuilder.parseList(parser: (Map<String, Any?>) -> T): List<T> =
        parseList(object : MapRowParser<T> {
            override fun parseRow(columns: Map<String, Any?>): T = parser(columns)
})
Malagasy answered 2/11, 2017 at 7:45 Comment(0)
C
3

Anko's parseList takes a MapRowParser, not a function. This simplifies usage. With Anko version you'd write

.parseList { mapRowParser { DayForecast(HashMap(it)) } }

instead. That's assuming there is a constructor function like mapRowParser which I can't find in their sources; otherwise, you could write it quite trivially.

Or rather, it's already written for you in the example code, just not as a separate function:

fun <T> mapRowParser(parser: (Map<String, Any?>) -> T): MapRowParser<T> = 
    object : MapRowParser<T> {
        override fun parseRow(columns: Map<String, Any?>): T = parser(columns)
    }

I am honestly really surprised if this function doesn't exist already (maybe called something else, but what?). OTOH, if it does exist, Leiva should have used it.

Carbonic answered 2/11, 2017 at 13:35 Comment(6)
Thanks! What does HashMap(it) mean?Malagasy
It's how constructors are called in Kotlin. It copies the input map into a new HashMap (I don't know why, instead of just DayForecast(it)).Carbonic
Thanks! but .parseList { mapRowParser { DayForecast(HashMap(it)) } } don't work, and .parseList { MapRowParser { DayForecast(HashMap(it)) } } don't work too! Could you give me a full code?Malagasy
Edited to insert the function.Carbonic
Sorry, but your code don't work yet, I get the error Error:(28, 19) None of the following functions can be called with the arguments supplied: public final inline fun <T : Any> parseList(parser: MapRowParser<???>): List<???> defined in org.jetbrains.anko.db.SelectQueryBuilder public final inline fun <T : Any> parseList(parser: RowParser<???>): List<???> defined in org.jetbrains.anko.db.SelectQueryBuilderMalagasy
I guest that there are some problems with your codre fun <T> mapRowParser(parser: (Map<String, Any?>) -> T): MapRowParser<T> = ...Malagasy
E
0

If you expected simple single column rows result, you can use ready-made Anko parsers:

  • ShortParser
  • IntParser
  • LongParser
  • FloatParser
  • DoubleParser
  • StringParser
  • BlobParser

Your code might be:

val dailyForecast = select(DayForecastTable.NAME)
                .whereSimple(dailyRequest, zipCode.toString(), date.toString())
                .parseList(StringParser)

Or you can implement your own parser. Here below sample for three columns result (Int, String, String):

val userParser = rowParser { id: Int, name: String, email: String ->
    Triple(id, name, email)
}
Emulsify answered 17/9, 2018 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.