Juan B
03/23/2021, 11:12 PMclass InputExample(val map: Map<String, Any?>) {
val name: String? by map
val age: Int? by map
val lastname: String? by map
fun KProperty<Any>.isPresent(): Boolean {
//return map.containsKey(this.name)
return true
}
}
I was trying to get to a interface like this:
inputType.lastname.isPresent()
ephemient
03/23/2021, 11:23 PM::lastName.isPresent()
could work, but ... did you mean to write val name: String? by map
or val name: String by map.withDefault { "?" }
instead of something that will throw up on non-present value?Juan B
03/23/2021, 11:26 PMval name: String? by map
(I just updated the code)
I tried to use it like this:
assert(!inputType::lastname.isPresent())
but I got a "Unresolved reference: isPresent"Juan B
03/23/2021, 11:27 PMNir
03/24/2021, 2:01 AMNir
03/24/2021, 2:01 AMNir
03/24/2021, 2:02 AMNir
03/24/2021, 2:03 AMJuan B
03/24/2021, 2:04 AMJuan B
03/24/2021, 2:05 AMRoukanken
03/24/2021, 9:32 AMthis
- one of type InputExample
, and one of type KProperty<Any>
so if you wanted to call your function, you would have to do smth like this:
val test = InputExample(mapOf(
"name" to null,
"age" to 4,
"lastname" to "Lastname",
))
with(test) {
::name.isPresent() // true
}
(after you edit the isPresent to be callable on KProperty<Any?>, because KProperty<String?> is not subtype of non nullable KProperty<Any>)Roukanken
03/24/2021, 9:33 AMtest::name.isPresent()
then this
is only one - test::name
which is KProperty<String?>
but the InputExample
this is missing, that's why you can't call the functionTimmy
03/24/2021, 2:45 PMval name : Optional<String?>
) doesn't work because Optional cannot contain "null". So your are left with Optional<String>?
which seems rather weird.
Instead we can create our own Option class that does support this use case:
sealed interface Maybe<out T>
object Missing : Maybe<Nothing> {
override fun toString(): String = "Missing"
}
data class Present<T>(val value : T) : Maybe<T>
Arrow kt probably has an implementation of this somewhere.
So now the example is:
data class Example(
val name : Maybe<String?>,
val age : Maybe<Int?>,
val lastName : Maybe<String?>
)
val foo = Example(Missing, Present(null), Present("bar"))
and
inputType.lastname is Present
With a few helper methods (Maybe<T>.get() : T
, Maybe<T>.getOrNull() : T?
, etc.) it becomes manageable.
This is the solution I picked for the scenario of PATCH requests where missing and null are different.
I also have a module for Jackson to get the JSON correct (i.e. absent is not written, null is written as null). If you want I can share that with you, but it is quite hairyYoussef Shoaib [MOD]
03/24/2021, 3:45 PMclass InputExample(name: String?, age: Int?, lastname: String?) {
val nameIsPresent: Boolean = false
val name: String? get() = field
set(value) {
nameIsPresent = true
field = value
}
...
}
Nir
03/24/2021, 5:29 PMNir
03/24/2021, 5:30 PMNir
03/24/2021, 5:31 PMclass Optional<T>(val isValid: Boolean, val t: T?)
basicallyNir
03/24/2021, 5:31 PMNir
03/24/2021, 5:32 PMNir
03/24/2021, 5:33 PMJuan B
03/24/2021, 5:51 PMTimmy
03/25/2021, 12:46 PMJuan B
03/26/2021, 12:03 AM