Skip to content

Commit

Permalink
add update function
Browse files Browse the repository at this point in the history
  • Loading branch information
lwj1994 committed May 14, 2021
1 parent c4d2bfd commit 2d3f8e2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/lwjlol/ormkv/demo/UserModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class UserModel(
@ColumnInfo(defValue = "false")
val isMan: Boolean,
@ColumnInfo(defValue = "0F", enableReset = false)
private val temperature: Float
val temperature: Float

) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import com.lwjlol.ormkv.annotation.ColumnInfo
import com.lwjlol.ormkv.annotation.Entity
import com.lwjlol.ormkv.annotation.Ignore
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
import com.squareup.kotlinpoet.metadata.specs.toTypeSpec
import com.squareup.kotlinpoet.metadata.toImmutableKmClass
import java.io.File
import javax.annotation.processing.*
import javax.lang.model.SourceVersion
Expand Down Expand Up @@ -87,6 +84,7 @@ class OrmkvProcessor : AbstractProcessor() {
val clearCode = StringBuilder()
val toStringCode = StringBuilder()
val toModelCode = StringBuilder()
val updateCode = StringBuilder()

var toModelError = false
allMembers.forEachIndexed { _, member ->
Expand Down Expand Up @@ -192,6 +190,7 @@ class OrmkvProcessor : AbstractProcessor() {
)
toStringCode.append("|$propertyName = $$propertyName\n")
toModelCode.append("|$propertyName = $propertyName, \n")
updateCode.append("|$propertyName = model.$propertyName\n")
if (clear) {
if (typeName.toString().contains("String")) {
clearCode.append("$propertyName = \"\"\"$defValue\"\"\" \n")
Expand All @@ -211,8 +210,8 @@ class OrmkvProcessor : AbstractProcessor() {
""".trimMargin()
)
.build()
).addToStringFun("return \"\"\"$toStringCode\"\"\".trimMargin()")

).addToString("return \"\"\"$toStringCode\"\"\".trimMargin()")
.addUpdate(updateCode.toString().trimMargin(), ClassName(packageName, className))
if (!toModelError) {
typeSpec.addToModel(toModelCode.toString(), "$packageName.$className")
}
Expand All @@ -223,7 +222,7 @@ class OrmkvProcessor : AbstractProcessor() {
file.writeTo(File(kaptKotlinGeneratedDir, END_FIX))
}

private fun TypeSpec.Builder.addToStringFun(code: String): TypeSpec.Builder {
private fun TypeSpec.Builder.addToString(code: String): TypeSpec.Builder {
return addFunction(
FunSpec.builder("toString")
.returns(String::class)
Expand All @@ -247,6 +246,15 @@ class OrmkvProcessor : AbstractProcessor() {
)
}

private fun TypeSpec.Builder.addUpdate(code: String, typeName: TypeName): TypeSpec.Builder {
return addFunction(
FunSpec.builder("update")
.addParameter(ParameterSpec.builder("model",typeName).build())
.addCode(code)
.build()
)
}

companion object {
const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated"
private const val TAG = "OrmkvProcessor"
Expand Down

0 comments on commit 2d3f8e2

Please sign in to comment.