This is less clean than the original slice of strings. However, it
removes unnecessary code as the caller requires a map instead of a
slice.
// TransitivelyDetectedGroups returns all groups which depend on "detected"
// groups, either directly or by depending on groups which transitively depend
// on "detected" groups.
-func TransitivelyDetectedGroups(groups map[string][]string) []string {
+func TransitivelyDetectedGroups(groups map[string][]string) map[string]bool {
work := make(map[string][]string)
for k, v := range groups {
work[k] = v
break
}
}
-
- var res []string
- for x := range detected {
- res = append(res, x)
- }
- sort.Strings(res)
- return res
+ return detected
}
tests := []struct {
name string
groups map[string][]string
- exp []string
+ exp map[string]bool
}{
{
"d",
},
},
- nil,
+ map[string]bool{},
},
{
"d",
},
},
- []string{
- "group-a",
+ map[string]bool{
+ "group-a": true,
},
},
"d",
},
},
- []string{
- "group-a",
+ map[string]bool{
+ "group-a": true,
},
},
"b",
},
},
- []string{
- "group-a",
- "group-b",
- "group-c",
- "group-d",
- "group-e",
+ map[string]bool{
+ "group-a": true,
+ "group-b": true,
+ "group-c": true,
+ "group-d": true,
+ "group-e": true,
},
},
"b",
},
},
- []string{
- "group-a",
- "group-b",
- "group-c",
- "group-d",
- "group-e",
+ map[string]bool{
+ "group-a": true,
+ "group-b": true,
+ "group-c": true,
+ "group-d": true,
+ "group-e": true,
},
},
}
func hostsToSync(names []string, allHosts *config.Hosts,
allGroups map[string][]string) ([]*config.Host, error) {
- detectedMap := make(map[string]bool)
- for _, x := range config.TransitivelyDetectedGroups(allGroups) {
- detectedMap[x] = true
- }
+ detectedMap := config.TransitivelyDetectedGroups(allGroups)
const detectedErr = `