kotlin member reference - grabbing child object data - kotlin-js

(using kotlin JS btw)
if i have a class that looks like this
data class AppUserDTO(
override val email:String,
override val client: DTOMin? = null
)
and the DTOmin looks like
data class DTOMin(
override val id: Int,
override val display: String)
is there any way to access client.display via a member reference?
i can access AppUserDTO::email
would love to basically do something like AppUserDTO::client::display
I am trying to build out a generic data table with sorting/filtering and it is working great for AppUserDTO::email but i need something similar to AppUserDTO::client::display

Related

Add the Instance of the class to constructor parameter property

I have the following class structure. The CarModel Class has a defects List which is of Type CarDefects. And I wanted to add the instance of the CarDefects class into this list of defects of carModel which is passed as a parameter for the CarDefects constructor.
However i cannot the use the add method and the error message says the following:
Unresolved reference: add
class CarModel(val brand: Brand, val modelName: String, val version: Int){
var defects: List<CarDefects>? = null
inner class Car(val model: CarModel, val manufactureYear: Int, val engineSerialNum: String){
}
inner class CarDefects(var carModel: CarModel, val affectedYears: Array<Int>, val defectCode: String ) {
init{
carModel.defects.add(//instance of this class)
}
}
}
You have a List as the type of defects. List is immutable, so you can't add more elements to it. You need to use a mutableList to be able to do this.
Here you have more info on this
A generic ordered collection of elements. Methods in this interface support only read-only access to the list; read/write access is supported through the MutableList interface.
Alternatively, you can try to create a new mutable List and add it each time, then convert to list. Something like this.
defects = defects?.toMutableList()?.add(//your car instance).toList()

How to pass the current instance into the constructor of another class withon class declaration?

I have a service that returns me a list of entities, for example:
data class TypeDto(
val type: Type,
val stages: List<StageDto>)
After applying several filter and flatMap operations, I get the desired data that has the following structure:
data class CustomerDto(
val id: String,
val name: String)
In order to extract the logic of filtering and mapping, I made a CustomerDtoWrapper class that takes List<TypeDto> as a constructor argument and does all the collection manipulation. So, in the end it looks as follows:
val types = service.getTypes()
val customers = CustomerDtoWrapper(types).filteredCustomers()
But I would like to make it more fluent and easy to read. Is it possible to call a certain function after getTypes(), so that types will be of the CustomerDtoWrapper type and look as follows:
val types = service.getTypes().someFun { ... }
val customers = types.filteredCustomers()
You can write for example extension function for this:
fun List<TypeDto>.toFilteredCustomers() = CustomerDtoWrapper(this).filteredCustomers()
and use it like this:
val customers = service.getTypes().toFilteredCustomers()

Retrieve data class members

I need to check if any variables inside of my data class are null. To do this I need retrieve them first but I can't access them directly (e.g. myDataClass.name) because I need it to be generic. Is there a way to access these variables without directly naming them. For example, like accessing a member of an array (myArray[0]).
The mechanism you're looking for is called "reflection" and it allows to introspect objects at runtime. You'll find a lot of information on the internet, but just to give you a link you may want to check this answer.
In your case you could do something like this:
data class MyDataClass(
val first: String?,
val second: String?,
val third: Int?
)
fun main() {
val a = MyDataClass("firstValue", "secondValue", 1)
val b = MyDataClass("firstValue", null, null)
printProperties(a)
printProperties(b)
}
fun printProperties(target: MyDataClass) {
val properties = target::class.memberProperties
for (property in properties) {
val value = property.getter.call(target)
val propertyName = property.name
println("$propertyName=$value")
}
}
Note that for this code to work you must add kotlin-reflect package as a dependency.

Kotlin data classes with Java super class

I have a Java class that holds generic information on databse entities (i.e. their id).
#Data
public class DbEntity {
protected final String id;
public DbEntity(String id) {
this.id = id;
}
}
We use Lombok #Data to generate getters, toString, equals...
In Java I would simply extend this class and add #Data once again.
#Data
class JavaSubClass extends DbEntity {
public JavaSubClass(String id) {
super(id);
}
}
In a newer service we use Kotlin but would like to reuse standard classes such as DbEntity.
My first approach was to simply declare a data class such as
data class SubClass1(val id: String, val name: String) : DbEntity(id)
Accidental override: The following declarations have the same JVM signature (getId()Ljava/lang/String;):
fun <get-id>(): String defined in com.demo.SubClass1
fun getId(): String! defined in com.demo.SubClass1
After some reading I found several solutions, all of which I'm not super happy with.
Don't use data classes. This works but leaves me with the task of implementing equals etc.
class SubClass4(id: String, val name: String) : DbEntity(id)
Duplicate the field. This works but we end up with two fields that could go out of sync.
data class SubClass3(val subId: String, val name: String) : DbEntity(subId)
Assign a different name to the getter. This fundamentally also duplicates the field, but hides the getter.
data class SubClass2(#get:JvmName("getId_") val id: String, val name: String) : DbEntity(id)
As I said, I'm not happy with any of the solution presented above. Having an abstract super class or an interface instead would certainly be more appropriate. However the Entity class resides in a library that primarily Java projects depend on. I'm hesitant to change it just because of a new Kotlin dependnecy.
Did anyone encounter similar issues and has advice on how to solve them?
As a workaround, until KT-6653 - Kotlin properties do not override Java-style getters and setters is fixed, I would go for a variant of your point 3, i.e.:
data class SubClass(#get:JvmName("bogusId") private val id: String, val name: String) : DbEntity(id)
The benefit of this variant is, that you always access the "original" getId-function. You will not use the bogusId()-function as it is not visible/accessible (accessing it via reflection makes no sense... you are only interested in the actual id-field). This works and looks similar for both sides: from Java as also from Kotlin. Still, under the hood this variant uses 2 fields, but in the best case you can just replace it in future with something like:
data class SubClass(override val id: String, val name : String) : DbEntity(id)

Jackson deserialization - Kotlin data classes - Defaults for missing fields per mapper

Given this data class:
data class MyPojo(val notInJson: Int, val inJson: Int)
Assume I want to implement a function of the form:
fun deserialize(jsonString: String, valueForFieldNotInJson: Int): MyPojo
Where jsonString does not include a field named notInJson. Assume also, that I have no control over MyPojo class definition.
How could I use Jackson library to deserialize MyPojo from jsonString and augment the missing field (notInJson) from valueForFieldNotInJson parameter?
Notes:
Basically, the question is about deserializing a Immutable class, where some fields come from Json and others are supplied at runtime.
Using custom deserializers or builders will not work because missing values are unknow at compile time.
This can be achieved by combining MinInAnnotations and ValueInjection.
Complete solution as follows:
data class MyPojo(val notInJson: Int, val inJson: Int)
class MyPojoMixIn {
#JacksonInject("notInJson") val notInJson: Int = 0
}
fun deserialize(jsonString: String, valueForFieldNotInJson: Int): MyPojo {
val injectables = InjectableValues.Std().addValue("notInJson", valueForFieldNotInJson)
val reader = jacksonObjectMapper()
.addMixIn(MyPojo::class.java, MyPojoMixIn::class.java)
.readerFor(MyPojo::class.java)
.with(injectables)
return reader.readValue(jsonString)
}