-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems.go
58 lines (48 loc) · 1.17 KB
/
items.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
package launchbar
import (
"encoding/json"
"fmt"
)
// Items represents the collection of items
type Items []*Item
// NewItems creates an empty Items collection
func NewItems() *Items {
return &Items{}
}
// Add adds passes Items to the collection and returns the collection.
func (items *Items) Add(i ...*Item) *Items {
for _, item := range i {
*items = append(*items, item)
}
return items
}
func (i *Items) setItems(items []*item) {
for _, item := range items {
i.Add(newItem(item))
}
}
func (items *Items) getItems() []*item {
a := make([]*item, len(*items))
for i, item := range *items {
a[i] = item.item
}
return a
}
type itemsByOrder Items
func (o itemsByOrder) Len() int { return len(o) }
func (o itemsByOrder) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o itemsByOrder) Less(i, j int) bool { return o[i].item.Order < o[j].item.Order }
// Compile returns items collection as a json string.
func (items *Items) Compile() string {
if items == nil {
return ""
}
if len(*items) == 0 {
return ""
}
b, err := json.Marshal(items.getItems())
if err != nil {
return fmt.Sprintf(`[{"title": "%v","subtitle":"error"}]`, err)
}
return string(b)
}