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 { 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) { ent.BySlug[r.Slug] = r if r.Category == "" { ent.Children = append(ent.Children, &Entry{ Name: r.Name, LastCommit: r.LastCommit, Repositories: []*Repository{r}, }) return } cat, ok := ent.Categories[r.Category] if !ok { t := &Entry{ Name: r.Category, LastCommit: r.LastCommit, Repositories: []*Repository{r}, } ent.Categories[r.Category] = t return } if cat.LastCommit.IsZero() || cat.LastCommit.Before(r.LastCommit) { cat.LastCommit = r.LastCommit } cat.Repositories = append(cat.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.Categories { sort.Slice(entries.Repositories, func(i, j int) bool { return entries.Repositories[i].LastCommit.After(entries.Repositories[j].LastCommit) }) } }