// Fetch returns a group from the collection with the given path. func (l *Library) Fetch(c index.Collection, path []string) (group, error) { if len(path) == 0 { return build(c, index.Key("Root")), nil } var g index.Group = c k := index.Key(path[0]) g = c.Get(k) if g == nil { return group{}, fmt.Errorf("invalid path: near '%v'", path[0]) } index.Sort(g.Tracks(), index.MultiSort(index.SortByInt("DiscNumber"), index.SortByInt("TrackNumber"))) g = index.Transform(g, index.SplitList("Artist", "AlbumArtist", "Composer")) c = index.Collect(g, index.ByPrefix("Name")) g = index.SubTransform(c, index.TrimEnumPrefix) g = index.SumGroupIntAttr("TotalTime", g) commonFields := []attr.Interface{ attr.String("Album"), attr.Strings("Artist"), attr.Strings("AlbumArtist"), attr.Strings("Composer"), attr.Int("Year"), attr.Int("BitRate"), attr.Int("DiscNumber"), } g = index.CommonGroupAttr(commonFields, g) g = index.RemoveEmptyCollections(g) for i, p := range path[1:] { var ok bool c, ok = g.(index.Collection) if !ok { return group{}, fmt.Errorf("retrieved Group is not a Collection") } k = index.Key(p) g = c.Get(k) if g == nil { return group{}, fmt.Errorf("invalid path near '%v'", path[1:][i]) } if _, ok = g.(index.Collection); !ok { if i == len(path[1:])-1 { break } return group{}, fmt.Errorf("retrieved Group isn't a Collection: %v", p) } } if g == nil { return group{}, fmt.Errorf("could not find group") } g = index.FirstTrackAttr(attr.String("ID"), g) return build(g, k), nil }
func build(g index.Group, key index.Key) group { h := group{ Name: g.Name(), Key: key, TotalTime: g.Field("TotalTime"), Artist: g.Field("Artist"), AlbumArtist: g.Field("AlbumArtist"), Composer: g.Field("Composer"), Year: g.Field("Year"), BitRate: g.Field("BitRate"), DiscNumber: g.Field("DiscNumber"), ListStyle: g.Field("ListStyle"), ID: g.Field("ID"), } if c, ok := g.(index.Collection); ok { return buildCollection(h, c) } getString := func(t index.Track, field string) string { if g.Field(field) != nil { return "" } return t.GetString(field) } getStrings := func(t index.Track, field string) []string { if g.Field(field) != nil { return nil } return t.GetStrings(field) } getInt := func(t index.Track, field string) int { if g.Field(field) != nil { return 0 } return t.GetInt(field) } for _, t := range g.Tracks() { h.Tracks = append(h.Tracks, track{ ID: t.GetString("ID"), Name: t.GetString("Name"), TotalTime: t.GetInt("TotalTime"), // Potentially common fields (don't want to re-transmit everything) Artist: getStrings(t, "Artist"), AlbumArtist: getStrings(t, "AlbumArtist"), Composer: getStrings(t, "Composer"), Album: getString(t, "Album"), Year: getInt(t, "Year"), DiscNumber: getInt(t, "DiscNumber"), BitRate: getInt(t, "BitRate"), }) } return h }