store repos in entries indexed by slug
1 file changed, 22 insertions(+), 11 deletions(-)
changed files
M data/entries.go → data/entries.go
@@ -21,37 +21,48 @@ Repositories []*Repository } type Entries struct { - Children []*Entry - Map map[string]*Entry + BySlug map[string]*Repository + Children []*Entry + Categories map[string]*Entry +} + +func NewEntries() Entries { + return Entries{ + BySlug: map[string]*Repository{}, + Children: []*Entry{}, + Categories: map[string]*Entry{}, + } } -func (ent *Entries) Add(r Repository) { +func (ent *Entries) Add(r *Repository) { + ent.BySlug[r.Slug] = r + if r.Category == "" { ent.Children = append(ent.Children, &Entry{ Name: r.Name, LastCommit: r.LastCommit, - Repositories: []*Repository{&r}, + Repositories: []*Repository{r}, }) return } - t, ok := ent.Map[r.Category] + cat, ok := ent.Categories[r.Category] if !ok { t := &Entry{ Name: r.Category, LastCommit: r.LastCommit, - Repositories: []*Repository{&r}, + Repositories: []*Repository{r}, } - ent.Map[r.Category] = t + ent.Categories[r.Category] = t return } - if t.LastCommit.IsZero() || t.LastCommit.Before(r.LastCommit) { - t.LastCommit = r.LastCommit + if cat.LastCommit.IsZero() || cat.LastCommit.Before(r.LastCommit) { + cat.LastCommit = r.LastCommit } - t.Repositories = append(t.Repositories, &r) + cat.Repositories = append(cat.Repositories, r) } func (ent *Entries) Sort() {@@ -59,7 +70,7 @@ sort.Slice(ent.Children, func(i, j int) bool { return ent.Children[i].LastCommit.After(ent.Children[j].LastCommit) }) - for _, entries := range ent.Map { + for _, entries := range ent.Categories { sort.Slice(entries.Repositories, func(i, j int) bool { return entries.Repositories[i].LastCommit.After(entries.Repositories[j].LastCommit) })