Created
October 4, 2020 17:42
-
-
Save AdamMc331/87ef24df6bcc0aea7bc79af347fd6f3a to your computer and use it in GitHub Desktop.
Shows how we can map a network response object to a UI model & manually check for required fields.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class PatientDTO( | |
@field:Json(name = "id") | |
val id: String? = null, | |
@field:Json(name = "firstName") | |
val firstName: String? = null, | |
@field:Json(name = "lastName") | |
val lastName: String? = null | |
) { | |
fun toPatient(): Patient { | |
val id = this.id.orEmpty() | |
val firstName = this.firstName.orEmpty() | |
val lastName = this.lastName.orEmpty() | |
if (id.isNotEmpty() && firstName.isNotEmpty() && lastName.isNotEmpty()) { | |
return Patient( | |
id = id, | |
firstName = firstName, | |
lastName = lastName | |
) | |
} else { | |
throw InvalidJSONResponseError(this::class) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment