package formattools import ( "fmt" "sort" "strings" ) func CompactIDList(ids []string) []string { sort.Strings(ids) currentGroup := "" currentIDs := make([]string, 0, len(ids)) groups := make([]string, 0, 2) for _, id := range ids { if id == "" { continue } split := strings.SplitN(id, ":", 3) if len(split) < 3 { continue } group := id[:len(split[0])+len(split[1])+1] if group != currentGroup { if len(currentIDs) > 0 { if len(currentIDs) == 1 { groups = append(groups, fmt.Sprintf("%s:%s", currentGroup, currentIDs[0])) } else { groups = append(groups, fmt.Sprintf("%s:{%s}", currentGroup, strings.Join(currentIDs, ","))) } currentIDs = currentIDs[:0] } currentGroup = group } currentIDs = append(currentIDs, split[2]) } if len(currentIDs) > 0 { if len(currentIDs) == 1 { groups = append(groups, fmt.Sprintf("%s:%s", currentGroup, currentIDs[0])) } else { groups = append(groups, fmt.Sprintf("%s:{%s}", currentGroup, strings.Join(currentIDs, ","))) } } return groups }