Skip to content

Commit 1e00902

Browse files
authored
Merge pull request #85 from RoseSecurity/fix-sorting-error
refactor: use type any and sort unused attrs
2 parents fb8d26e + 22e9b51 commit 1e00902

File tree

2 files changed

+64
-58
lines changed

2 files changed

+64
-58
lines changed

internal/analyzer.go

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package internal
66
import (
77
"fmt"
88
"path/filepath"
9+
"sort"
910
"strings"
1011
)
1112

@@ -67,35 +68,37 @@ func printDiff(resources []Resource, schema ProviderSchema, model, tool, prompt,
6768
return nil
6869
}
6970

70-
func findUnusedAttributes(usedAttrs map[string]string, possibleAttrs map[string]interface{}) []string {
71-
validNames := make(map[string]struct{})
71+
func findUnusedAttributes(usedAttrs map[string]string, possibleAttrs map[string]any) []string {
72+
validNames := make(map[string]struct{})
7273

73-
if blockAny, ok := possibleAttrs["block"]; ok {
74-
if block, ok := blockAny.(map[string]interface{}); ok {
75-
//Attributes at the current block level
76-
if attrsAny, ok := block["attributes"]; ok {
77-
if attrsMap, ok := attrsAny.(map[string]interface{}); ok {
78-
for name := range attrsMap {
79-
validNames[name] = struct{}{}
80-
}
81-
}
82-
}
83-
// Nested block types at this level (their names appear as top-level blocks in HCL)
84-
if blockTypesAny, ok := block["block_types"]; ok {
85-
if btMap, ok := blockTypesAny.(map[string]interface{}); ok {
86-
for name := range btMap {
87-
validNames[name] = struct{}{}
88-
}
89-
}
90-
}
91-
}
92-
}
74+
if blockAny, ok := possibleAttrs["block"]; ok {
75+
if block, ok := blockAny.(map[string]any); ok {
76+
// Attributes at the current block level
77+
if attrsAny, ok := block["attributes"]; ok {
78+
if attrsMap, ok := attrsAny.(map[string]any); ok {
79+
for name := range attrsMap {
80+
validNames[name] = struct{}{}
81+
}
82+
}
83+
}
84+
// Nested block types at this level (their names appear as top-level blocks in HCL)
85+
if blockTypesAny, ok := block["block_types"]; ok {
86+
if btMap, ok := blockTypesAny.(map[string]any); ok {
87+
for name := range btMap {
88+
validNames[name] = struct{}{}
89+
}
90+
}
91+
}
92+
}
93+
}
94+
95+
var unused []string
96+
for name := range validNames {
97+
if _, used := usedAttrs[name]; !used {
98+
unused = append(unused, name)
99+
}
100+
}
101+
sort.Strings(unused)
93102

94-
var unused []string
95-
for name := range validNames {
96-
if _, used := usedAttrs[name]; !used {
97-
unused = append(unused, name)
98-
}
99-
}
100-
return unused
103+
return unused
101104
}

internal/dry_run.go

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package internal
66
import (
77
"fmt"
88
"path/filepath"
9+
"sort"
910
"strings"
1011
)
1112

@@ -65,35 +66,37 @@ func testPossibleAttributes(resources []Resource, schema ProviderSchema, tool st
6566
}
6667

6768
// testFindUnusedAttributes identifies unused attributes by comparing used and possible attributes.
68-
func testFindUnusedAttributes(usedAttrs map[string]string, possibleAttrs map[string]interface{}) []string {
69-
// Mirror logic in analyzer's findUnusedAttributes: only consider names
70-
// under block.attributes and block.block_types.
71-
validNames := make(map[string]struct{})
69+
func testFindUnusedAttributes(usedAttrs map[string]string, possibleAttrs map[string]any) []string {
70+
// Mirror logic in analyzer's findUnusedAttributes: only consider names
71+
// under block.attributes and block.block_types.
72+
validNames := make(map[string]struct{})
7273

73-
if blockAny, ok := possibleAttrs["block"]; ok {
74-
if block, ok := blockAny.(map[string]interface{}); ok {
75-
if attrsAny, ok := block["attributes"]; ok {
76-
if attrsMap, ok := attrsAny.(map[string]interface{}); ok {
77-
for name := range attrsMap {
78-
validNames[name] = struct{}{}
79-
}
80-
}
81-
}
82-
if blockTypesAny, ok := block["block_types"]; ok {
83-
if btMap, ok := blockTypesAny.(map[string]interface{}); ok {
84-
for name := range btMap {
85-
validNames[name] = struct{}{}
86-
}
87-
}
88-
}
89-
}
90-
}
74+
if blockAny, ok := possibleAttrs["block"]; ok {
75+
if block, ok := blockAny.(map[string]any); ok {
76+
if attrsAny, ok := block["attributes"]; ok {
77+
if attrsMap, ok := attrsAny.(map[string]any); ok {
78+
for name := range attrsMap {
79+
validNames[name] = struct{}{}
80+
}
81+
}
82+
}
83+
if blockTypesAny, ok := block["block_types"]; ok {
84+
if btMap, ok := blockTypesAny.(map[string]any); ok {
85+
for name := range btMap {
86+
validNames[name] = struct{}{}
87+
}
88+
}
89+
}
90+
}
91+
}
92+
93+
var unused []string
94+
for name := range validNames {
95+
if _, used := usedAttrs[name]; !used {
96+
unused = append(unused, name)
97+
}
98+
}
99+
sort.Strings(unused)
91100

92-
var unused []string
93-
for name := range validNames {
94-
if _, used := usedAttrs[name]; !used {
95-
unused = append(unused, name)
96-
}
97-
}
98-
return unused
101+
return unused
99102
}

0 commit comments

Comments
 (0)