nomisRev / kotlin-kafka

Kafka bindings for Kotlin `suspend`, and Kafka streaming operators for KotlinX Flow.
https://nomisRev.github.io/kotlin-kafka/
Apache License 2.0
106 stars 10 forks source link

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

Open lsafer-meemer opened 4 months ago

lsafer-meemer commented 4 months ago

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.

nomisRev commented 4 months ago

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!