-
Notifications
You must be signed in to change notification settings - Fork 10
/
site.go
100 lines (85 loc) · 2.25 KB
/
site.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
package magopie
import (
"encoding/json"
"fmt"
)
// Site defines a site that serves torrent files.
type Site struct {
ID string
Name string
URL string
Enabled bool
}
// SiteCollection is a collection of sites because gomobile can't
// handle slices
type SiteCollection struct {
list []Site
}
// Length returns how many sites are in the collection
func (sc *SiteCollection) Length() int {
return len(sc.list)
}
// Get returns the site at idx or a nil site
func (sc *SiteCollection) Get(idx int) *Site {
if idx <= sc.Length() {
return &sc.list[idx]
}
return nil
}
// Clear empties the list of sites
func (sc *SiteCollection) Clear() {
sc.list = sc.list[:0]
}
// Index finds the index of a site or -1 if not found
func (sc *SiteCollection) Index(s *Site) int {
for i, tst := range sc.list {
if tst == *s {
return i
}
}
return -1
}
// Insert inserts a site into the collection at i
func (sc *SiteCollection) Insert(i int, s *Site) {
if i < 0 || i > sc.Length() {
fmt.Printf("Magopie-go:: Attempted to insert a site at an invalid index")
return
}
sc.list = append(sc.list, Site{})
copy(sc.list[i+1:], sc.list[i:])
sc.list[i] = *s
}
// Remove a site from the collection at i
func (sc *SiteCollection) Remove(i int) {
if i < 0 || i > sc.Length() {
fmt.Printf("Magopie-go:: Attempted to remove a site from an invalid index")
return
}
copy(sc.list[i:], sc.list[i+1:])
sc.list[len(sc.list)-1] = Site{}
sc.list = sc.list[:len(sc.list)-1]
}
// Push adds an element to the end of the collection
func (sc *SiteCollection) Push(s *Site) {
sc.Insert(sc.Length(), s)
}
// Pop removes the last element from the collection
func (sc *SiteCollection) Pop(s *Site) {
sc.Remove(sc.Length() - 1)
}
// Unshift adds an element to the front of the collection
func (sc *SiteCollection) Unshift(s *Site) {
sc.Insert(0, s)
}
// Shift removes an element from the front of the collection
func (sc *SiteCollection) Shift(s *Site) {
sc.Remove(0)
}
// MarshalJSON returns JSON from the collection
func (sc *SiteCollection) MarshalJSON() ([]byte, error) {
return json.Marshal(sc.list)
}
// UnmarshalJSON replaces the collection with the results from a JSON []byte
func (sc *SiteCollection) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &sc.list)
}