Skip to content

Commit

Permalink
Proper to string conversion (#165)
Browse files Browse the repository at this point in the history
* Proper to string conversion

* Provide meaningful error for snowflakes
  • Loading branch information
HopeBaron authored Jan 11, 2021
1 parent 9989971 commit 637b479
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions common/src/main/kotlin/entity/Interactions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -326,38 +326,23 @@ sealed class OptionValue<out T>(val value: T) {
}
}

fun OptionValue<*>.intOrNull(): Int? {
return value as? Int
}

fun OptionValue<*>.stringOrNull(): String? {
return value as? String
}

fun OptionValue<*>.booleanOrNull(): Boolean? {
return value as? Boolean
}

fun OptionValue<*>.snowflakeOrNull(): Snowflake? {
val id = stringOrNull() ?: return null
return Snowflake(id)
}

fun OptionValue<*>.int(): Int {
return intOrNull() ?: error("$value wasn't an Int.")
return value as? Int ?: error("$value wasn't an Int.")
}


fun OptionValue<*>.string(): String {
return stringOrNull() ?: error("$value wasn't a String.")
return value.toString()
}

fun OptionValue<*>.boolean(): Boolean {
return booleanOrNull() ?: error("$value wasn't a Boolean.")
return value as? Boolean ?: error("$value wasn't a Boolean.")
}

fun OptionValue<*>.snowflake(): Snowflake {
return snowflakeOrNull() ?: error("$value wasn't a Snowflake.")
val id = string().toLongOrNull() ?: error("$value wasn't a Snowflake")
return Snowflake(id)
}

@Serializable
Expand Down

0 comments on commit 637b479

Please sign in to comment.