I am looking for a way to neatly filter the values from the event and create a map based on them. What we need to know:
Event: The event contains the dictionary data structure (its a map of <String, String> where key is a product parameter name.
For example: a)
Key: Brand,
Value: Nike
b)
Key: Producer,
Value: Audi
- Filter:
Filter is a Map<String, Set<String>>
and its configured in yml.
Example of values:
Key: Manufacturer,
Value: [Brand, Producer, Creator, Author]
- Entity:
data class Product( val id: String, val parameters: Map<String, Parameter> = emptyMap() ) data class Parameter( val parameterId: String?, val parameterValue: String)
How flow looks like:
1)An event comes with a list of parameters, e.g.
mapOf(
"brand" to "Nike",
"size" to "42",
"color" to "black
)
- Values are filtered. Lets pretend we have got only one filter - its key = Manufacturer, its values = [Brand, Producer, Creator, Author]. So from this event we have got only one position - its mapOf("brand" to "Nike") During filtering, a map is created in the domain object [Product] with filled field- parameters: Map <String, Parameter> e.g.
mapOf("Manufacturer" to Parameter(null, "Nike"))
As you can see - Manufacturer, not Brand, was selected as the key in the map in the Product object (i.e. the key of this parameter, not the value).
GOAL:
What should the method of filtering the event look like?How to do it in clever, Kotlin way (filter the data using the Map <String, Set > for the incoming map and create the target object based on it.)
Thanks in advance for help/advice!
EDIT:
More information:
Sample input:
Incoming event that appears on the domain side:
ProductCreatedEvent( id = "1234", mapOf("brand" to "Nike", "color" to "black, "size" to "46"))
Sample output:
Product(id = "1234", mapOf("Manufacturer" to "Nike"))
The incoming event was filtered by a filter with the structure Map <String, Set <String>>
The structure of the filter is dictated by the fact that the data comes from various sources and thus, for example, the "manufacturer" key on the supplier's side may be called "brand" or "producer". On the side of my service it is to be standardized and therefore the key value per key is selected in the parameter map in the domain object.
CodePudding user response:
This should work
val event = ProductCreatedEvent( id = "1234", mapOf("Brand" to "Nike", "Color" to "Black", "Size" to "46"))
val filters = mapOf("Manufacturer" to setOf("Brand", "Producer", "Creator", "Author"))
val parameters = event.params.mapNotNull { param ->
filters.entries.find { it.value.contains(param.key) }
?.let { it.key to param.value }
}.toMap()
val product = Product(event.id, parameters)