gooogle
gooogle

Reputation: 395

What is the better way to transform one object to another in Kotlin?

I've multiple object classes in my project and I need to transform one object to another.

I tried creating a common util and start converting them the code started growing and was not able to manage in a single file.

Then I started creating individual transformer class something similar to this,

/** This transforms [Square] to [Rectangle] **/
object SquareToRectangleTransformer {
   fun transform(square: Square): Rectangle {
       checkNotNull(square.side) { missing ("side") }
       val length = square.side
       val breadth = square.side
       return Rectangle(length, breadth
   }
}

Now my project has multiple transformer classes something like the above and now I find this approach as an anti pattern since it's creating class explosion problem.

I searched for other alternatives, like a plugin or dependency library which could do this job for me with less code and fount this answer interesting .

Is there any similar libraries available in Kotlin which could do this job for me?

Upvotes: 5

Views: 8436

Answers (4)

Eric
Eric

Reputation: 4413

You can use let for object transformations:

val rectangle = square.let {
    Rectangle(it.side, it.side)
}

Upvotes: 3

Kevin Perez
Kevin Perez

Reputation: 799

Taking Rinat Suleimanov answer this what I did using extensions

data class SquareResponse(val side: Int, name: String)

data class SquareEntity(val side: Int, name: String)

// transforms a single object
fun SquareResponse.transform(): SquareEntity {
    this.apply {
        return SquareEntity(
            side, name
        )
    }
}

// transforming lists
fun List<SquareResponse>.transform(): List<SquareEntity> {
    return this.map {
        it.transform()
    }
}

// Example use
fun listTransform(squares: List<SquareResponse>): List<SquareEntity> {
    return squares.transform()
}
 

fun singleTranform(squar: SquareResponse): SquareEntity {
    return square.transform()
}

Upvotes: 1

Loveneesh Singh
Loveneesh Singh

Reputation: 173

In my project, we were also facing the same problem where we have to transform one object into another. So, we took this approach where we added a from() method as a companion object. Example if I want to convert a square to a rectangle, I will just write Rectangle.from(square)

class Rectangle(){
  companion object {
    fun from(square: Square): Reactangle{
       return Rectangle(square.side,square.side)
    }
  }
}

With this, we can transform one object into others and also it keeps your code cleaner.

Upvotes: 0

Rinat Suleimanov
Rinat Suleimanov

Reputation: 517

I'd suggest you to create an extension functions for every transformation that you going to have. For me it's very clear and nice way. Also almost all kotlin stdlib written in the same way. Example:

   fun Square.transform(): Rectangle {
       val length = square.side
       val breadth = square.side
       return Rectangle(length, breadth)
   }
}

You'll be able to call this function as member function:

val square = Square(4)
val rectangle = square.transform()

You also could avoid checkNotNull(square.side) { missing ("side") } if you made type, represented side non nullable:

data class Square(
    val side: Int
)

Upvotes: 2

Related Questions