This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
/
group.go
85 lines (72 loc) · 1.77 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package index
import (
"sort"
)
type LibList []*Library
func (l LibList) Len() int {
return len(l)
}
func (l LibList) Swap(i int, j int) {
x := l[i]
l[i] = l[j]
l[j] = x
}
func (l LibList) Less(i int, j int) bool {
if i == 0 {
return true
}
if j == 0 {
return false
}
return l[i].Stars > l[j].Stars
}
type GroupedIndex struct {
Libraries map[string]LibList
}
func makeGroupedIndex() GroupedIndex {
return GroupedIndex{
Libraries: map[string]LibList{},
}
}
func (i Index) Group(disamb map[string]string) GroupedIndex {
ret := makeGroupedIndex()
for _, lib := range i.Libraries {
list, exists := ret.Libraries[lib.Name]
if exists {
// Check to see if this library is disambiguated
curi, selection := disamb[lib.Name]
if selection && curi == lib.URI {
// If this is the selected version, put it at the
// front of the list
ret.Libraries[lib.Name] = append(LibList{lib}, list...)
} else {
// Otherwise, just add it to the list
ret.Libraries[lib.Name] = append(list, lib)
}
} else {
ret.Libraries[lib.Name] = LibList{lib}
}
}
return ret
}
// This sorts **all but the first** match by rating. This is because the
// first match is the one that is first in the install path so it should
// always be listed first. Among those not in the search path, this
// rearranges them by rating.
func (g GroupedIndex) SortByRating() GroupedIndex {
ret := makeGroupedIndex()
for libname, libs := range g.Libraries {
sort.Sort(libs)
ret.Libraries[libname] = libs
}
return ret
}
// Return and index that has no "extra" libraries in it, only the ones that
// can possibly be chosen.
func (g GroupedIndex) Selected() *Index {
ret := NewIndex()
for _, libs := range g.Libraries {
ret.Libraries = append(ret.Libraries, libs[0])
}
return ret
}