Skip to content

Instantly share code, notes, and snippets.

@AdamMc331
Created October 4, 2020 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamMc331/87ef24df6bcc0aea7bc79af347fd6f3a to your computer and use it in GitHub Desktop.
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.
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