Skip to content

Commit

Permalink
fix: summary api bug (#421)
Browse files Browse the repository at this point in the history
## What type of PR is this?

/kind bug

## What this PR does / why we need it:

Fix summary api bug:


![image](https://github.com/KusionStack/karpor/assets/9360247/36c54a58-e046-4051-bbdf-bf30c2ca9b0c)
  • Loading branch information
elliotxx authored May 11, 2024
1 parent f97bbad commit 0b8a8fa
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions pkg/core/entity/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,24 +165,8 @@ func NewResourceGroupFromQuery(r *http.Request) (ResourceGroup, error) {
}

// Convert the raw query parameters to maps.
var labels map[string]string
var annotations map[string]string
if len(labelsRaw) > 0 {
labels = make(map[string]string)
// Each label is expected to be in the format "key=value".
parts := strings.SplitN(labelsRaw, "=", 2)
if len(parts) == 2 {
labels[parts[0]] = parts[1]
}
}
if len(annotationsRaw) > 0 {
annotations = make(map[string]string)
// Each annotation is expected to be in the format "key=value".
parts := strings.SplitN(annotationsRaw, "=", 2)
if len(parts) == 2 {
annotations[parts[0]] = parts[1]
}
}
labels := parseKeyValuePairs(labelsRaw)
annotations := parseKeyValuePairs(annotationsRaw)

// Construct a resource group instance.
rg := ResourceGroup{
Expand All @@ -197,3 +181,21 @@ func NewResourceGroupFromQuery(r *http.Request) (ResourceGroup, error) {

return rg, nil
}

// parseKeyValuePairs parses a comma-separated key-value pair string and returns
// a map.
func parseKeyValuePairs(raw string) map[string]string {
result := make(map[string]string)
if len(raw) > 0 {
pairs := strings.Split(raw, ",")
for _, pair := range pairs {
parts := strings.SplitN(pair, "=", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
result[key] = value
}
}
}
return result
}

0 comments on commit 0b8a8fa

Please sign in to comment.