mardi 9 février 2021

How to get lambda/function signature in Kotlin?

I have developed the code below:

import com.google.gson.Gson
import com.rabbitmq.client.Delivery

typealias MessageHandler<T> = (payload: T, args: HashMap<String, String>) -> Unit
class MessageRouter {
    private var handlers: HashMap<String, Function<Unit>> = hashMapOf()

    fun <T> bind(routingKey: String, handler: MessageHandler<T>) : MessageRouter {
        handlers[routingKey] = handler
        return this
    }


    fun consume(message: Delivery) {
        if(message.envelope.routingKey in handlers) {
            val currentHandler = handlers[message.envelope.routingKey]
            if(message.properties.contentType == "application/json") {
                ///// TODO: Get function signature!
                val gson = Gson()
                //// TODO: Deserialize data and call the handler function 
            }
        }
    }
}

To be used as follows:

val messageRouter = MessageRouter()
    messageRouter.bind(EventSubscriberConstants.REGISTRATION_ROUTING_KEY) { user: UserDTO, _ -> register(user) }
    basicConsume(
        queueName, true,
        { _, message ->
            messageRouter.consume(message)
8")), UserDTO::class.java))
        }, null, null
)
      

I need the input lambda signature to extract UserDTO class and use Gson to deserialize it and process. I can not find any way to get function signature in Kotlin!





Aucun commentaire:

Enregistrer un commentaire