data/entries.go (view raw)
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 | package data
import (
"sort"
"time"
)
type Repository struct {
Name string
Category string
Path string
Slug string
Description string
LastCommit time.Time
}
type Entry struct {
Name string
LastCommit time.Time
Repositories []*Repository
}
type Entries struct {
Children []*Entry
Map map[string]*Entry
}
func (ent *Entries) Add(r Repository) {
if r.Category == "" {
ent.Children = append(ent.Children, &Entry{
Name: r.Name,
LastCommit: r.LastCommit,
Repositories: []*Repository{&r},
})
return
}
t, ok := ent.Map[r.Category]
if !ok {
t := &Entry{
Name: r.Category,
LastCommit: r.LastCommit,
Repositories: []*Repository{&r},
}
ent.Map[r.Category] = t
return
}
if t.LastCommit.IsZero() || t.LastCommit.Before(r.LastCommit) {
t.LastCommit = r.LastCommit
}
t.Repositories = append(t.Repositories, &r)
}
func (ent *Entries) Sort() {
sort.Slice(ent.Children, func(i, j int) bool {
return ent.Children[i].LastCommit.After(ent.Children[j].LastCommit)
})
for _, entries := range ent.Map {
sort.Slice(entries.Repositories, func(i, j int) bool {
return entries.Repositories[i].LastCommit.After(entries.Repositories[j].LastCommit)
})
}
}
|