Cannot bind a nullable property with tornadofx viewmodel - kotlin

I'm trying to bind a nullable property (String?) from my domain model to a tornadofx view model. But I get a compiler error.
The nullable properties are: idCard, phone, and discharge
My code:
class Patient(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Patient>(Patients)
var patientId: Int by Patients.patientId
var name: String by Patients.name
var lastName: String by Patients.lastName
var recordNumber: Int by Patients.recordNumber
var idCard: String? by Patients.idCard
var phone: String? by Patients.phone
var age: Int by Patients.age
var gender: Char by Patients.gender
var admission: DateTime by Patients.admission
var discharge: DateTime? by Patients.discharge
var specialty: String by Patients.specialty
}
class PatientViewModel(patient: Patient) : ViewModel() {
val patientId = bind { patient.observable(Patient::patientId) }
val name = bind { patient.observable(Patient::name)}
val lastName = bind { patient.observable(Patient::lastName) }
val recordNumber = bind { patient.observable(Patient::recordNumber) }
val idCard = bind { patient.observable(Patient::idCard) }
val phone = bind { patient.observable(Patient::phone)}
val age = bind { patient.observable(Patient::age) }
val gender = bind { patient.observable(Patient::gender) }
val admission = bind { patient.observable(Patient::admission) }
val discharge = bind { patient.observable(Patient::discharge) }
val specialty = bind { patient.observable(Patient::specialty) }
}
Compiler error:
e: D:\projects\manager\src\main\kotlin\manager\model\Patient.kt: (49, 18): Type inference failed: Cannot infer type parameter T in inline fun <reified PropertyType : Property<T>, reified T : Any, ResultType : PropertyType> bind(autocommit: Boolean = ..., forceObjectProperty: Boolean = ..., defaultValue: T? = ..., noinline propertyProducer: () -> PropertyType?): ResultType
None of the following substitutions
(Boolean,Boolean,String?,() -> Property<String?>?)
(Boolean,Boolean,Any?,() -> Property<String?>?)
can be applied to
(() -> ObjectProperty<String?>)

Try doing this with your model bindings:
val patientId = bind(Patient::patientId)

Related

Save a class created with the Builder pattern in Room

I need to create a table in my db where I can save Filters so I created this class like this:
#Entity
class Filter private constructor(
val name: String?,
#TypeConverters(Converters::class)
val sortBy: Sort?,
val withGenres: List<Int>?,
val withCast: List<Int>?,
) : Serializable {
#PrimaryKey(autoGenerate = true)
var id: Int? = null
data class Builder(
var name: String? = null,
var sortBy: Sort? = null,
var withGenres: List<Int> = listOf(),
var withCast: List<Int> = listOf(),
) {
fun name(name: String) = apply { this.name = name }
fun sortBy(sort: Sort?) = apply { this.sortBy = sort }
fun withGenres(genresId: List<Int>) = apply { this.withGenres = genresId }
fun withCast(castIds: List<Int>) = apply { this.withCast = castIds }
fun build() = Filter(name, sortBy, withGenres, withCast)
}
/**
* Sorting options, default order is Descendant (desc)
*/
enum class Sort {
POPULARITY, RELEASE_DATE, REVENUE, VOTE_AVERAGE;
private var order: Order = Order.DESC
override fun toString(): String {
return this.name.lowercase() + "." + this.order.name.lowercase()
}
fun setOrder(order: Order) {
this.order = order
}
enum class Order {
ASC, DESC
}
}
override fun toString(): String {
return "sortBy: ${sortBy.toString()}, withGenres: $withGenres"
}
}
But upon compilation I get the following errors:
Cannot configure how to save field withGenres into database
Cannot configure how to save field withCast into database
Entities and POJOs must have public constructor
Cannot find setter for field (all the fields)
How can I modify the code so that it works, what are the problems?

Kotlin - Ktor - How to handle Optional API resource fields in PATCH calls?

When implementing a REST API with Ktor (and Kotlin), it supports the optional field handling of Kotlin. Which works for POST and GET, but what about PATCH (update)?
For example, you have the following resource:
#Serializable
data class MyAddress(
var line1: String? = null,
var line2: String? = null,
var city: String? = null,
var postal_code: String? = null,
var state: String? = null,
var country: String? = null
)
So all MyAddress fields are optional (with a default value).
When you create an address with POST:
"line1" : "line1",
"country" : "XX"
and you than want to remove the country with a PATCH:
"country" : null
the end result of the resource should be:
"line1" : "line1"
But how can you determine this by parsing the json of the PATCH request? Because there is no way, as far as I know, to determine if it was null by default, or submitted.
Furthermore, the default null value for the MyAddress is required, because else the parsing will not work.
Code example:
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
#kotlinx.serialization.Serializable
data class MyAddress(
var line1: String? = null,
var line2: String? = null,
var city: String? = null,
var postal_code: String? = null,
var state: String? = null,
var country: String? = null
)
fun main() {
val jsonStringPOST = "{\"line1\":\"street\",\"country\":\"GB\"}"
println("JSON string is: $jsonStringPOST")
val myAddressPost = Json.decodeFromString<MyAddress>(jsonStringPOST)
println("MyAddress object: $myAddressPost")
val jsonStringPATCH = "{\"country\":null}"
println("JSON string is: $jsonStringPATCH")
val myAddressPatch = Json.decodeFromString<MyAddress>(jsonStringPATCH)
println("MyAddress object: $myAddressPatch")
}
I tried to add Optional<String>? as well, but it complains about missing serialization of Optional, and preferably I do not want to make all my data var's Optionals.
Note: I am looking for a more structured solution, that also works with all other resources in the api (10+ classes).
A second solution, based on Aleksei's example:
#Serializable
data class Address2(val line1: OptionalValue<String> = Undefined, val country: OptionalValue<String> = Undefined)
#Serializable(with = OptionalValueSerializer::class)
sealed interface OptionalValue<out T>
object Undefined: OptionalValue<Nothing> {
override fun toString(): String = "Undefined"
}
object Absent: OptionalValue<Nothing> {
override fun toString(): String = "Absent"
}
class WithValue<T>(val value: T): OptionalValue<T> {
override fun toString(): String = value.toString()
}
open class OptionalValueSerializer<T>(private val valueSerializer: KSerializer<T>) : KSerializer<OptionalValue<T>> {
override val descriptor: SerialDescriptor = valueSerializer.descriptor
override fun deserialize(decoder: Decoder): OptionalValue<T> {
return try {
WithValue(valueSerializer.deserialize(decoder))
} catch (cause: SerializationException) {
Absent
}
}
override fun serialize(encoder: Encoder, value: OptionalValue<T>) {
when (value) {
is Undefined -> {}
is Absent -> { encoder.encodeNull() }
is WithValue -> valueSerializer.serialize(encoder, value.value)
}
}
}
fun main() {
val jsonStringPOST = "{\"line1\":\"street\",\"country\":\"GB\"}"
println("JSON string is: $jsonStringPOST")
val myAddressPost = Json.decodeFromString<Address2>(jsonStringPOST)
println("MyAddress object: $myAddressPost")
val jsonStringUPDATE = "{\"country\":null}"
println("JSON string is: $jsonStringUPDATE")
val myAddressUpdate = Json.decodeFromString<Address2>(jsonStringUPDATE)
println("MyAddress object: $myAddressUpdate")
if(myAddressUpdate.country is Absent || myAddressUpdate.country is WithValue) {
println("Update country: ${myAddressUpdate.country}")
} else {
println("No update for country: ${myAddressUpdate.country}")
}
}
Output is:
JSON string is: {"line1":"street","country":"GB"}
MyAddress object: Address2(line1=street, country=GB)
JSON string is: {"country":null}
MyAddress object: Address2(line1=Undefined, country=Absent)
Update country: Absent
You can use a sealed interface for a part of an address to represent undefined value, absence of value, and actual value. For this interface, you need to write a serializer that will encode and decode values accordingly to your logic. I'm not good at the kotlinx.serialization framework so I wrote an example as simple as possible.
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.request.*
import io.ktor.server.routing.*
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
fun main() {
embeddedServer(Netty, port = 4444) {
install(ContentNegotiation) {
json()
}
routing {
post {
val address = call.receive<Address>()
println(address)
}
}
}.start()
}
#Serializable
data class Address(val line1: MyValue = Undefined, val country: MyValue = Undefined)
#Serializable(with = AddressValueSerializer::class)
sealed interface MyValue
object Undefined: MyValue {
override fun toString(): String = "Undefined"
}
object Absent: MyValue {
override fun toString(): String = "Absent"
}
class WithValue(val value: String): MyValue {
override fun toString(): String = value
}
object AddressValueSerializer: KSerializer<MyValue> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("AddressValue", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): MyValue {
return try {
WithValue(decoder.decodeString())
} catch (cause: SerializationException) {
Absent
}
}
#OptIn(ExperimentalSerializationApi::class)
override fun serialize(encoder: Encoder, value: MyValue) {
when (value) {
is Undefined -> {}
is Absent -> { encoder.encodeNull() }
is WithValue -> { encoder.encodeString(value.value) }
}
}
}
With some help from medium.com, I came to the following solution:
#Serializable(with = OptionalPropertySerializer::class)
open class OptionalProperty<out T> {
object NotPresent : OptionalProperty<Nothing>()
data class Present<T>(val value: T) : OptionalProperty<T>() {
override fun toString(): String {
return value.toString()
}
}
fun isPresent() : Boolean {
return this is Present
}
fun isNotPresent(): Boolean {
return this is NotPresent
}
fun isEmpty(): Boolean {
return (this is Present) && this.value == null
}
fun hasValue(): Boolean {
return (this is Present) && this.value != null
}
override fun toString(): String {
if(this is NotPresent) {
return "<NotPresent>"
}
return super.toString()
}
}
open class OptionalPropertySerializer<T>(private val valueSerializer: KSerializer<T>) : KSerializer<OptionalProperty<T>> {
override val descriptor: SerialDescriptor = valueSerializer.descriptor
override fun deserialize(decoder: Decoder): OptionalProperty<T> =
OptionalProperty.Present(valueSerializer.deserialize(decoder))
override fun serialize(encoder: Encoder, value: OptionalProperty<T>) {
when (value) {
is OptionalProperty.NotPresent -> {}
is OptionalProperty.Present -> valueSerializer.serialize(encoder, value.value)
}
}
}
#Serializable
private data class MyAddressNew(
var line1: OptionalProperty<String?> = OptionalProperty.NotPresent,
var line2: OptionalProperty<String?> = OptionalProperty.NotPresent,
var city: OptionalProperty<String?> = OptionalProperty.NotPresent,
var postal_code: OptionalProperty<String?> = OptionalProperty.NotPresent,
var state: OptionalProperty<String?> = OptionalProperty.NotPresent,
var country: OptionalProperty<String?> = OptionalProperty.NotPresent,
)
fun main() {
val jsonStringPOST = "{\"line1\":\"street\",\"country\":\"GB\"}"
println("JSON string is: $jsonStringPOST")
val myAddressPost = Json.decodeFromString<MyAddressNew>(jsonStringPOST)
println("MyAddress object: $myAddressPost")
val jsonStringUPDATE = "{\"country\":null}"
println("JSON string is: $jsonStringUPDATE")
val myAddressUpdate = Json.decodeFromString<MyAddressNew>(jsonStringUPDATE)
println("MyAddress object: $myAddressUpdate")
if(myAddressUpdate.country.isPresent()) {
println("Update country: ${myAddressUpdate.country}")
} else {
println("No update for country: ${myAddressUpdate.country}")
}
}
This prints:
JSON string is: {"line1":"street","country":"GB"}
MyAddress object: MyAddressNew(line1=street, line2=<NotPresent>, city=<NotPresent>, postal_code=<NotPresent>, state=<NotPresent>, country=GB)
JSON string is: {"country":null}
MyAddress object: MyAddressNew(line1=<NotPresent>, line2=<NotPresent>, city=<NotPresent>, postal_code=<NotPresent>, state=<NotPresent>, country=null)
Update country: null

Get value from annotation failed

This is annotation definition:
#Target(AnnotationTarget.PROPERTY)
#Retention(AnnotationRetention.RUNTIME)
#MustBeDocumented
annotation class MyAnno(val desc: String, val comment: String) { }
And below is where the MyAnno used:
class MyAnnoUser {
#MyAnno(desc = "name", comment = "name comment")
lateinit var name: String
#MyAnno(desc = "age", comment = "age comment")
var age: Int = 0
#MyAnno(desc = "money", comment = "money comment")
var money: Double = 0.0
#MyAnno(desc = "gender", comment = "gender comment")
var gender: Boolean = false
override fun toString(): String {
return "(name: $name; age: $age; money: $money; gender: ${if (gender) "men" else "women"})"
}
}
Here's code to read the value in MyAnno:
class MyAnnoExpression(val obj: Any, val context: Context) {
val numTypeSet = setOf("Int", "Double", "Byte")
fun expression() {
val clazz = obj::class
clazz.declaredMemberProperties.forEach { prop ->
val mutableProp = try {
prop as KMutableProperty<*>
} catch (e: Exception) {
null
} ?: return#forEach
val desc = mutableProp.findAnnotation<MyAnno>()
desc?.let {
val propClassName = mutableProp.returnType.toString().removePrefix("kotlin.")
when (propClassName) {
in numTypeSet -> mutableProp.setter.call(obj, (readProp(it, context) as kotlin.String).toNum(propClassName))
"String" -> mutableProp.setter.call(obj, (readProp(it, context) as kotlin.String))
"Boolean" -> mutableProp.setter.call(obj, (readProp(it, context) as kotlin.String).toBoolean())
}
}
}
}
private fun readProp(value: MyAnno, context: Context): Any? {
val prop = Properties()
val input = context.assets.open("app.properties")
prop.load(InputStreamReader(input, "utf-8"))
return prop.get(value.desc)
}
}
Now the Debugger shows me following info of value in readProp(...) function:
#com.demo.basekotlin.MyAnno(comment=age comment, desc=age)
But i got exception when read desc from value:
An exception occurs during Evaluate Expression Action : org.jetbrains.eval4j.VOID_VALUE cannot be cast to org.jetbrains.eval4j.AbstractValue
I can't find any thing wrong in my code, is there another program setting needed?
As I understand you just want to see annotation value for given property.
First, let's declare an annotation.
#Target(PROPERTY)
#Retention(AnnotationRetention.RUNTIME)
annotation class PropertyAnnotation(val desc: String)
Container:
class Container {
#PropertyAnnotation("Name")
var name: String? = null
#PropertyAnnotation("Age")
var age: Int = -1
var notAnnotatedProperty: String = "not annotated"
}
And finally, code responsible for get all declared properties, then find a properties annotated as PropertyAnnotation, cast it to it, and get value from it.
fun main() {
val container = Container()
container::class.declaredMemberProperties.forEach { property ->
(property.annotations.find {
it is PropertyAnnotation
} as? PropertyAnnotation)?.let {
println("Property: `$property` is ${it.desc}")
}
}
}
Output:
Property: `var Container.age: kotlin.Int` is Age
Property: `var Container.name: kotlin.String?` is Name
Kind ugly. But, let's use more Kotlin pro-dev-features.
Let's create extension function for any not-null type which returns all member property of given type:
inline fun <reified T : Any> Any.getMemberProperty(): List<T> {
return this::class.declaredMemberProperties.mapNotNull { prop ->
(prop.annotations.find { ann -> ann is T }) as? T
}
}
And now usage:
fun main() {
val container = Container()
container.getMemberProperty<PropertyAnnotation>().forEach {
println(it.desc)
}
}

Kotlin DSL for collections

This is a simple data structure House which contains a (nullable) list of Person :
data class Person(val name: String,
val militaryYear: Int? = null,
val firstPregnantYear: Int? = null)
data class House(val persons: List<Person>?)
Person maybe male or female. Male has militaryYear value , Female has firstPregnantYear value.
I want to write a DSL to build the House object , like this :
val house = house {
person("John") {
militaryYear = 2003
}
person("Anne") {
firstPregnantYear = 2010
}
}
This is what I have done :
data class Person(val name: String,
val militaryYear: Int? = null,
val firstPregnantYear: Int? = null) {
class Builder(private val name: String) {
var militaryYear : Int? = null
var firstPregnantYear : Int? = null
fun build(): Person = Person(name, militaryYear, firstPregnantYear)
}
}
fun person(name: String , block: Person.Builder.() -> Unit) = Person.Builder(name).apply(block).build()
data class House(val persons: List<Person>?) {
class Builder {
var persons = mutableListOf<Person>()
fun person(name: String , block: Person.Builder.() -> Unit) {
persons.add(Person.Builder(name).apply(block).build())
}
fun build(): House = House(persons = persons.takeIf { it.isNotEmpty() })
}
}
It seems works , but it doesn't forbid both militaryYear & firstPregnantYear , for example :
val house = house {
person("John") {
militaryYear = 2003
}
person("Anne") {
firstPregnantYear = 2010
}
person("Alien") {
militaryYear = 2005
firstPregnantYear = 2009
}
}
println(house)
The result :
House(persons=[
Person(name=John, militaryYear=2003, firstPregnantYear=null)
, Person(name=Anne, militaryYear=null, firstPregnantYear=2010)
, Person(name=Alien, militaryYear=2005, firstPregnantYear=2009)
])
The Alien has militaryYear & firstPregnantYear , which is incorrect.
So , I add another MaleBuilder and FemaleBuilder :
class MaleBuilder(private val name: String) {
var militaryYear = 0
fun build(): Person = Person(name, militaryYear, null)
}
class FemaleBuilder(private val name: String) {
var firstPregnantYear = 0
fun build() : Person = Person(name , null , firstPregnantYear)
}
fun male(name: String , block: Person.MaleBuilder.() -> Unit) = Person.MaleBuilder(name).apply(block).build()
fun female(name: String , block: Person.FemaleBuilder.() -> Unit) = Person.FemaleBuilder(name).apply(block).build()
And the DSL becomes :
val house = house {
male("John") {
militaryYear = 2003
}
female("Anne") {
firstPregnantYear = 2010
}
}
println(house)
OK , here is the problem , male("John") and female("Anne") are not inserted into persons . It outputs :
House(persons=null)
I think the problem comes from House's person() function :
fun person(name: String , block: Person.Builder.() -> Unit) {
persons.add(Person.Builder(name).apply(block).build())
}
The defined receiver is Person.Builder , but it is MaleBuilder and FemaleBuilder in the code.
How to solve this problem ?
Full code : https://pastebin.com/1Q6D8rzx
------- updated -------
It can be solved by introducing House.Builder.male() and House.Builder.female() functions :
fun male(name: String , block: Person.MaleBuilder.() -> Unit) {
persons.add(Person.MaleBuilder(name).apply(block).build())
}
fun female(name: String , block: Person.FemaleBuilder.() -> Unit) {
persons.add(Person.FemaleBuilder(name).apply(block).build())
}
But I think it is not a good solution. In real world example , there maybe a lot of properties , and a lot of restrictions. Is there a way to use one function to receive it ?

How to bind ItemViewModel to the combobox and to the entire form?

I have class User and ItemViewModel for it.
class User(name: String, type: Int, isAdmin: Boolean) {
var name by property<String>(name)
fun nameProperty() = getProperty(User::name)
var type by property<Int>(type)
fun typeProperty() = getProperty(User::type)
var isAdmin by property<Boolean>(isAdmin)
fun isAdminProperty() = getProperty(User::isAdmin)
}
class UserModel : ItemViewModel<User>() {
val name = bind { item?.nameProperty() }
val type = bind { item?.typeProperty() }
val isAdmin = bind { item?.isAdminProperty() }
}
In addition i have View with Form and Controller
class ChooseUserView : View() {
val ctrl: ChooseUserCtrl by inject()
override val root = form {
fieldset("Choose user") {
field("Name") {
combobox<User> {
items = ctrl.users
selectionModel.select(0)
}
}
field("Psw") {
textfield {
whenVisible {
ctrl.model.isAdmin
}
}
}
}
}
}
class ChooseUserCtrl : Controller() {
val view: ChooseUserView by inject()
val users = FXCollections.observableArrayList<User>()
val model = UserModel()
init {
users.add(User("disp", 1, false))
users.add(User("admin", 2, true))
}
}
I'd like to bind user list to form so
In combobox i wanna see names, not addresses like these
When combobox index is changing i'd like to see bind enabling of textfield("Psw") based on boolean property isAdmin.
Here is the answer from tornadofx author:
Here you go:
class User() {
constructor(name: String, type: Int, isAdmin: Boolean): this() {
this.name = name
this.type = type
this.isAdmin = isAdmin
}
val nameProperty = SimpleStringProperty()
var name by nameProperty
val typeProperty = SimpleIntegerProperty()
var type by typeProperty
val isAdminProperty = SimpleBooleanProperty()
var isAdmin by isAdminProperty
val passwordProperty = SimpleStringProperty()
var password by passwordProperty
}
class UserModel(user: User? = null) : ItemViewModel<User>(user) {
val name = bind(User::nameProperty)
val type = bind(User::typeProperty)
val isAdmin = bind(User::isAdminProperty)
val password = bind(User::passwordProperty)
}
class ChooseUserView : View() {
val ctrl: ChooseUserCtrl by inject()
val selectedUser = UserModel(ctrl.users.first())
override val root = form {
fieldset("Choose user") {
field("Name") {
combobox(selectedUser.itemProperty, ctrl.users) {
cellFormat {
text = it.name
}
}
}
field("Psw") {
visibleWhen(selectedUser.isAdmin)
textfield(selectedUser.password)
}
}
}
}
class ChooseUserCtrl : Controller() {
val users = FXCollections.observableArrayList<User>()
init {
users.add(User("disp", 1, false))
users.add(User("admin", 2, true))
}
}