/
meta.go
129 lines (111 loc) · 3.07 KB
/
meta.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2015, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"tchaik.com/index"
"tchaik.com/index/checklist"
"tchaik.com/index/cursor"
"tchaik.com/index/favourite"
"tchaik.com/index/history"
"tchaik.com/index/playlist"
)
// Meta is a container for extra metadata which wraps the central media library.
// TODO: Refactor meta to be user-specific.
type Meta struct {
history history.Store
favourites favourite.Store
checklist checklist.Store
playlists playlist.Store
cursors cursor.Store
}
func loadLocalMeta() (*Meta, error) {
fmt.Printf("Loading play history...")
playHistoryStore, err := history.NewStore(playHistoryPath)
if err != nil {
return nil, fmt.Errorf("error loading play history: %v", err)
}
fmt.Println("done.")
fmt.Printf("Loading favourites...")
favouriteStore, err := favourite.NewStore(favouritesPath)
if err != nil {
return nil, fmt.Errorf("\nerror loading favourites: %v", err)
}
fmt.Println("done.")
fmt.Printf("Loading checklist...")
checklistStore, err := checklist.NewStore(checklistPath)
if err != nil {
return nil, fmt.Errorf("\nerror loading checklist: %v", err)
}
fmt.Println("done.")
fmt.Printf("Loading playlists...")
playlistStore, err := playlist.NewStore(playlistPath)
if err != nil {
return nil, fmt.Errorf("\nerror loading playlists: %v", err)
}
// TODO(dhowden): remove this once we can better intialise the "Default" playlist
if p := playlistStore.Get("Default"); p == nil {
playlistStore.Set("Default", &playlist.Playlist{})
}
fmt.Println("done")
fmt.Printf("Loading cursors...")
cursorStore, err := cursor.NewStore(cursorPath)
if err != nil {
return nil, fmt.Errorf("\nerror loading cursor: %v", err)
}
fmt.Println("done")
return &Meta{
history: playHistoryStore,
favourites: favouriteStore,
checklist: checklistStore,
playlists: playlistStore,
cursors: cursorStore,
}, nil
}
type metaFieldGrp struct {
index.Group
field string
value interface{}
}
func (mfg metaFieldGrp) Field(f string) interface{} {
if f == mfg.field {
return mfg.value
}
return mfg.Group.Field(f)
}
type metaFieldCol struct {
index.Collection
field string
value interface{}
}
func (mfc metaFieldCol) Field(f string) interface{} {
if f == mfc.field {
return mfc.value
}
return mfc.Collection.Field(f)
}
func newMetaField(g index.Group, field string, value bool) index.Group {
if !value {
return g
}
if c, ok := g.(index.Collection); ok {
// TODO(dhowden): currently need to maintain the underlying interface type
// so that it can be correctly transmitted, need a better way of doing this.
return metaFieldCol{
Collection: c,
field: field,
value: value,
}
}
return metaFieldGrp{
Group: g,
field: field,
value: value,
}
}
// Annotate adds any meta information to the Group (identified by Path).
func (m *Meta) Annotate(p index.Path, g index.Group) index.Group {
g = newMetaField(g, "Favourite", m.favourites.Get(p))
return newMetaField(g, "Checklist", m.checklist.Get(p))
}