Created
December 31, 2021 14:23
-
-
Save myungpyo/ae2ce889eee99c5b1a33d5ff9cec2f82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class StickyStateVisitor( | |
| private val saveCodeList: MutableList<String>, | |
| private val restoreCodeList: MutableList<String>, | |
| private val logger: KSPLogger, | |
| ): KSVisitorVoid() { | |
| override fun visitPropertyDeclaration(property: KSPropertyDeclaration, data: Unit) { | |
| val propertyName = property.simpleName.asString() | |
| val resolvedType = property.type.resolve() | |
| val propertyType = resolvedType.declaration.simpleName.asString() | |
| // Generate put code | |
| var saveCode = "$STATE_STORE.put$propertyType(\"${makePropertyStoreKey(propertyName)}\", $STATE_HOLDER.$propertyName)" | |
| if (resolvedType.nullability == Nullability.NULLABLE) { | |
| saveCode = "$propertyName?.let{ $saveCode }" | |
| } | |
| saveCodeList.add(saveCode) | |
| // Generate restore code | |
| var restoreCode = "$STATE_HOLDER.$propertyName = $STATE_STORE.get$propertyType(\"${makePropertyStoreKey(propertyName)}\")" | |
| if (resolvedType.nullability != Nullability.NULLABLE && propertyType == "String") { | |
| restoreCode = "$restoreCode ?: $STATE_HOLDER.$propertyName" | |
| } | |
| restoreCodeList.add(restoreCode) | |
| } | |
| private fun makePropertyStoreKey(propertyName: String): String { | |
| return "StickyState_$propertyName" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment