Skip to content

Instantly share code, notes, and snippets.

@dilrajsingh1997
Last active November 21, 2021 13:14
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 dilrajsingh1997/6392f8e0aefd37dee6c7850b5454992e to your computer and use it in GitHub Desktop.
Save dilrajsingh1997/6392f8e0aefd37dee6c7850b5454992e to your computer and use it in GitHub Desktop.
file addLine "fun parseArguments(backStackEntry: NavBackStackEntry): $dataClassName {"
tabs++
file addLine "return "
file addPhrase "$dataClassName("
tabs++
properties.forEach { property ->
val argumentName = property.simpleName.asString()
val resolvedType: KSType = property.type.resolve()
fun getParsedElement(): String {
return try {
when (resolver.getClassDeclarationByName(resolvedType.declaration.qualifiedName!!)
?.toString()) {
"Boolean" -> "backStackEntry.arguments?.getBoolean(\"$argumentName\") ?: false
"String" -> "backStackEntry.arguments?.getString(\"$argumentName\") ?: \"\""
"Float" -> "backStackEntry.arguments?.getFloat(\"$argumentName\") ?: 0F"
"Int" -> "backStackEntry.arguments?.getInt(\"$argumentName\") ?: 0"
"Long" -> "backStackEntry.arguments?.getLong(\"$argumentName\") ?: 0L"
else -> {
logger.error("complex data types not yet supported", property)
""
}
}
} catch (e: Exception) {
""
}
}
file addLine "$argumentName = ${getParsedElement()}"
file addPhrase ", "
}
tabs--
file addLine ")"
tabs--
file addLine "}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment