Skip to content

Commit

Permalink
[Database] - Add failsafes for floor conversion (AIC-597)
Browse files Browse the repository at this point in the history
I think something in the Moshi 1.7.0 upgrade broke this....on the
plus side, the fix I include here is pretty concise.
  • Loading branch information
Cliabhach committed Oct 31, 2018
1 parent faa70f4 commit e0087df
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion db/src/main/kotlin/edu/artic/db/MoshiAdapters.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.artic.db

import android.annotation.SuppressLint
import android.support.annotation.Nullable
import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonQualifier
Expand Down Expand Up @@ -64,13 +65,43 @@ class ZonedDateTimeAdapter {
}

/**
* Parses floor as an integer. We default to [INVALID_FLOOR] as 0 and negative numbers are valid floors.
* Parses floor as an integer. We default to [INVALID_FLOOR] as 0 and
* negative numbers are valid floors.
*
*
* In the past, bugs related to this class have arisen (from Retrofit2) as
*
* `Unable to create converter for class edu.artic.db.models.ArticAppData for method AppDataApi.getBlob`
*
* or (from Moshi)
*
* `Non-null value 'floor' was null at $.some.path.`
*/
class FloorAdapter {

@ToJson
fun toText(@Floor floor: Int): String = floor.toString()

/**
* Due to a bug in Moshi 1.7.0, we require two overloads of this int converter.
*/
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
@ToJson
fun toTextBoxed(@Floor floor: java.lang.Integer): String = floor.toString()

/**
* Due to a bug in Moshi 1.7.0, we require two overloads of this int converter.
*
* Due to a bug in Kotlin 1.2.71, we must use the `java.lang.Integer` constructor.
*/
@SuppressLint("UseValueOf")
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
@FromJson
@Floor
fun fromTextBoxed(@Nullable text: String?): java.lang.Integer {
return java.lang.Integer(fromText(text))
}

@FromJson
@Floor
fun fromText(@Nullable text: String?): Int {
Expand Down

0 comments on commit e0087df

Please # to comment.