MaxSizeSubjectsListForLog returns the keys in the map as a slice of maximum length maxToDisplay. It is useful for logging domains being managed, for example, since a map is typically needed for quick lookup, but a slice is needed for logging, and this can be quite a doozy since there may be a huge a
(subjects map[string]struct{}, maxToDisplay int)
| 7 | // map is typically needed for quick lookup, but a slice is needed for logging, and this |
| 8 | // can be quite a doozy since there may be a huge amount (hundreds of thousands). |
| 9 | func MaxSizeSubjectsListForLog(subjects map[string]struct{}, maxToDisplay int) []string { |
| 10 | numberOfNamesToDisplay := min(len(subjects), maxToDisplay) |
| 11 | domainsToDisplay := make([]string, 0, numberOfNamesToDisplay) |
| 12 | for domain := range subjects { |
| 13 | domainsToDisplay = append(domainsToDisplay, domain) |
| 14 | if len(domainsToDisplay) >= numberOfNamesToDisplay { |
| 15 | break |
| 16 | } |
| 17 | } |
| 18 | if len(subjects) > maxToDisplay { |
| 19 | domainsToDisplay = append(domainsToDisplay, fmt.Sprintf("(and %d more...)", len(subjects)-maxToDisplay)) |
| 20 | } |
| 21 | return domainsToDisplay |
| 22 | } |
no outgoing calls
no test coverage detected