-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
79 lines (60 loc) · 1.28 KB
/
status.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
package main
import "github.com/stewart/cmus"
// given two cmus statuses, generates a map of the difference between them
func diffStatus(a, b *cmus.Status) map[string]interface{} {
diff := make(map[string]interface{})
var hashDiff = func(x, y map[string]string) map[string]string {
z := make(map[string]string)
for k, v := range x {
w, ok := y[k]
if !ok {
z[k] = ""
}
if v != w {
z[k] = w
}
}
for k, v := range y {
w, ok := x[k]
if !ok {
z[k] = ""
}
if v != w {
z[k] = v
}
}
return z
}
if a.Playing != b.Playing {
diff["playing"] = b.Playing
}
if a.File != b.File {
diff["file"] = b.File
}
if a.Duration != b.Duration {
diff["duration"] = b.Duration
}
if a.Position != b.Position {
diff["position"] = b.Position
}
tags := hashDiff(a.Tags, b.Tags)
settings := hashDiff(a.Settings, b.Settings)
if len(tags) > 0 {
diff["tags"] = tags
}
if len(settings) > 0 {
diff["settings"] = settings
}
return diff
}
// converts a status into a map that Gin can serialize
func serializeStatus(s *cmus.Status) map[string]interface{} {
return map[string]interface{}{
"playing": s.Playing,
"file": s.File,
"duration": s.Duration,
"position": s.Position,
"tags": s.Tags,
"settings": s.Settings,
}
}