Skip to content

Instantly share code, notes, and snippets.

@bastman
Created February 10, 2019 12:54
Show Gist options
  • Save bastman/df8a7dda8dd30bbc9256845ecbd521ab to your computer and use it in GitHub Desktop.
Save bastman/df8a7dda8dd30bbc9256845ecbd521ab to your computer and use it in GitHub Desktop.
kotlin: isNullable
inline fun <reified T> query(content: String, query: String): T {
try {
val expression: Expression<JsonNode> = compile(query = query)
val resultNode: JsonNode = search(content = content, expression = expression)
val converted: T = converter.convertValue(resultNode)
val isNullable: Boolean = null is T
if (converted == null && !isNullable) {
error("Failed to convert to (non-nullable) instance of ${T::class} from $resultNode .")
}
return converted
} catch (all: Throwable) {
error("JQ Failed! query: $query - reason: ${all.message} ")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment