Skip to content

Commit

Permalink
Finish todo: avoid duplicated values
Browse files Browse the repository at this point in the history
  • Loading branch information
rujche committed Feb 18, 2025
1 parent 3386529 commit 8891924
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
9 changes: 4 additions & 5 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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{
Expand All @@ -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})
Expand All @@ -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})
Expand Down
25 changes: 20 additions & 5 deletions analyzer/internal/spring_boot_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8891924

Please sign in to comment.