Last active
May 19, 2020 16:44
-
-
Save milechainsaw/988a8bca428abfd7fda0f93ebdb43f6e to your computer and use it in GitHub Desktop.
ObjectBox converter for List<String> properties in your Kotlin data model. http://objectbox.io/
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
package com.test.objectbox.models | |
import io.objectbox.annotation.Convert | |
import io.objectbox.annotation.Entity | |
import io.objectbox.annotation.Id | |
@Entity data class Article( | |
@Id var id: Long = 0, | |
@Convert(converter = StringListConverter::class, dbType = String::class) | |
var strings: List<String>? = null, | |
) |
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
package com.test.objectbox.converters | |
import io.objectbox.converter.PropertyConverter | |
class StringListConverter : PropertyConverter<List<String>, String> { | |
override fun convertToEntityProperty(databaseValue: String?): List<String> { | |
if (databaseValue == null) return ArrayList() | |
return databaseValue.split(",") | |
} | |
override fun convertToDatabaseValue(entityProperty: List<String>?): String { | |
if (entityProperty == null) return "" | |
if (entityProperty.isEmpty()) return "" | |
val builder = StringBuilder() | |
entityProperty.forEach { builder.append(it).append(",") } | |
builder.deleteCharAt(builder.length - 1) | |
return builder.toString() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's what I use.