micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6k stars 1.04k forks source link

Support inject a list of values in expression language #10928

Open yibo-long opened 6 days ago

yibo-long commented 6 days ago

Feature description

I am looking for a way to inject an array of String with the expression language. My main goal is to manage the Kafka subscription in a more structural way with the expression language, but when I almost hit my goal I realized it could only resolve to a single string.

I would be able to construct the topics with the following beans:

@Context
@Singleton
class SubscribedTopics(
    val topics: List<SubscribedTopic>,
) {
    fun constructTopics(): List<String> {
        val values = topics.map { it.getTopic() }
        return values
    }
}

@EachProperty("application.topics")
data class SubscribedTopic @ConfigurationInject constructor(
    @Parameter val name: String,
    val id1: Int,
    val id2: Int,
) {
    fun getTopic(): String = "topic_v${id1}_t$id2"
}

and then use the following for creating the Kafka listener:

@KafkaListener
class KafkaConsumer {
    @Topic("#{ ctx[SubscribedTraits].constructTopics() }")
    fun consumeTraitMessage(
        @KafkaKey resourceId: String,
        message: ByteArray,
    ) {
      // run
    }
}

however the logs indicate the properties are resolved into a single string instead of an array of strings:

20:56:04.333 [Test worker] INFO  o.a.k.clients.consumer.KafkaConsumer - [Consumer clientId=foo-consumer, groupId=foo] Subscribed to topic(s): [topic_v1_t22, topic_v1_t18, topic_v1_t19, topic_v1_t3]
20:56:04.333 [Test worker] INFO  i.m.c.k.p.KafkaConsumerProcessor - Kafka listener [KafkaTraitConsumer#consumeTraitMessage] subscribed to topics: [[topic_v1_t22, topic_v1_t18, topic_v1_t19, topic_v1_t3]]

I checked micronaut-kafka is resolving it as stringValues. Is there any way for the expression language to support resolving from List to stringValues?