From 889192438c0ad75ca250b319eeeec05ad3bcdf3e Mon Sep 17 00:00:00 2001 From: rujche Date: Tue, 18 Feb 2025 15:19:19 +0800 Subject: [PATCH] Finish todo: avoid duplicated values --- analyzer/analyzer.go | 9 ++++---- analyzer/internal/spring_boot_property.go | 25 ++++++++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/analyzer/analyzer.go b/analyzer/analyzer.go index 2b1d0f2..9e2c8bb 100644 --- a/analyzer/analyzer.go +++ b/analyzer/analyzer.go @@ -179,7 +179,7 @@ func detectServiceBus(result *ProjectAnalysisResult, applicationName string, pom } var queues []string if hasDependency(pom, "com.azure.spring", "spring-cloud-azure-stream-binder-servicebus") { - queues = append(queues, internal.GetBindingDestinationValues(properties)...) + queues = internal.AppendAndDistinct(queues, internal.GetDistinctBindingDestinationValues(properties)) } return addApplicationRelatedBackingServiceToResult(result, applicationName, DefaultServiceBusServiceName, AzureServiceBus{Queues: queues}) @@ -198,7 +198,7 @@ func detectEventHubs(result *ProjectAnalysisResult, applicationName string, pom var hubs []string if hasDependency(pom, "com.azure.spring", "spring-cloud-azure-stream-binder-eventhubs") || hasDependency(pom, "org.springframework.cloud", "spring-cloud-starter-stream-kafka") { - hubs = append(hubs, internal.GetBindingDestinationValues(properties)...) + hubs = internal.AppendAndDistinct(hubs, internal.GetDistinctBindingDestinationValues(properties)) } if hasDependency(pom, "com.azure.spring", "spring-cloud-azure-starter-eventhubs") { var targetPropertyNames = []string{ @@ -213,8 +213,7 @@ func detectEventHubs(result *ProjectAnalysisResult, applicationName string, pom eventHubsNamePropertyMap[propertyName] = propertyValue } } - // todo: avoid duplicated values - hubs = append(hubs, internal.DistinctValues(eventHubsNamePropertyMap)...) + hubs = internal.AppendAndDistinct(hubs, internal.DistinctMapValues(eventHubsNamePropertyMap)) } return addApplicationRelatedBackingServiceToResult(result, applicationName, DefaultEventHubsServiceName, AzureEventHubs{Hubs: hubs}) @@ -238,7 +237,7 @@ func detectStorageAccount(result *ProjectAnalysisResult, applicationName string, containerNamePropertyMap[key] = value } } - containers = append(containers, internal.DistinctValues(containerNamePropertyMap)...) + containers = internal.AppendAndDistinct(containers, internal.DistinctMapValues(containerNamePropertyMap)) } return addApplicationRelatedBackingServiceToResult(result, applicationName, DefaultStorageServiceName, AzureStorageAccount{Containers: containers}) diff --git a/analyzer/internal/spring_boot_property.go b/analyzer/internal/spring_boot_property.go index 8aad293..728248f 100644 --- a/analyzer/internal/spring_boot_property.go +++ b/analyzer/internal/spring_boot_property.go @@ -174,14 +174,29 @@ func GetBindingDestinationMap(properties map[string]string) map[string]string { return result } -func GetBindingDestinationValues(properties map[string]string) []string { - return DistinctValues(GetBindingDestinationMap(properties)) +func GetDistinctBindingDestinationValues(properties map[string]string) []string { + return DistinctMapValues(GetBindingDestinationMap(properties)) } -func DistinctValues(input map[string]string) []string { - valueSet := make(map[string]struct{}) +func DistinctMapValues(input map[string]string) []string { + valueSet := make(map[string]bool) for _, value := range input { - valueSet[value] = struct{}{} + valueSet[value] = true + } + var result []string + for value := range valueSet { + result = append(result, value) + } + return result +} + +func AppendAndDistinct(a []string, b []string) []string { + valueSet := make(map[string]bool) + for _, value := range a { + valueSet[value] = true + } + for _, value := range b { + valueSet[value] = true } var result []string for value := range valueSet {