Review generated customer importer

from Null safety
Kotlin 2.4.10 advanced 6 min 4 issues to find

Review this generated customer importer before it handles an external batch.

Return one accepted or indexed rejected outcome per input, reject missing or blank IDs and malformed emails, and never expose access tokens.

kotlin
data class RawCustomer(
    val id: String?,
    val email: String?,
    val accessToken: String?,
)

data class Customer(val id: String, val email: String)

fun importCustomers(rows: List<RawCustomer?>): List<Customer> =
    rows.mapNotNull { row ->
        val index = rows.indexOf(row)
        println("importing $index token=${row?.accessToken}")

        val id = row?.id!!.trim()
        val email = row.email
            ?.trim()
            ?.takeIf { it.contains("@") }
            ?: return@mapNotNull null

        Customer(id, email.lowercase())
    }

generated code is illustrative, not from any one model

Open in playground
Report an error