kotlin-kafka icon indicating copy to clipboard operation
kotlin-kafka copied to clipboard

[feature request] convenience function for creating settings fully from properties

Open lsafer-meemer opened this issue 1 year ago • 1 comments

Sometimes settings are given as Properties. Having to read, decode and pass individual parameters to the any Settings class is too much work.

My request is to add a function for each Settings class that has a single Properties parameters and get the required Settings parameters from that single Properties parameter and if any required parameter is missing or invalid, throws an IllegalArgumentException

Here is the functions I'm proposing:

fun createAdminSettings(
    properties: Properties
): AdminSettings {
    val bootstrapServers = properties.getProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG)

    return AdminSettings(
        bootstrapServer = bootstrapServers,
        props = properties,
    )
}

fun <K, V> createReceiverSettings(
    properties: Properties,
    keyDeserializer: Deserializer<K>,
    valueDeserializer: Deserializer<V>,
): ReceiverSettings<K, V> {
    val bootstrapServers = properties.getProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG)
    val groupId = properties.getProperty(ConsumerConfig.GROUP_ID_CONFIG)

    return ReceiverSettings(
        bootstrapServers = bootstrapServers,
        keyDeserializer = keyDeserializer,
        valueDeserializer = valueDeserializer,
        groupId = groupId,
        properties = properties
    )
}

fun <K, V> createPublisherSettings(
    properties: Properties,
    keySerializer: Serializer<K>,
    valueSerializer: Serializer<V>,
): PublisherSettings<K, V> {
    val bootstrapServers = properties.getProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG)

    return PublisherSettings(
        bootstrapServers = bootstrapServers,
        keySerializer = keySerializer,
        valueSerializer = valueSerializer,
    )
}

why the create prefix? to make it more obvious that this is a convenience function and not a constructor. Thus, implies that providing properties alone does not satisfies all the requirements of creating an instance of the class but extra implicit steps will be done to actually create the class.

lsafer-meemer avatar Jul 01 '24 08:07 lsafer-meemer

Hey @lsafer-meemer,

Great request, I've been thinking about this a lot as well. I think would be gerat to have this kind-of utility!

nomisRev avatar Jul 02 '24 06:07 nomisRev