-
Notifications
You must be signed in to change notification settings - Fork 32
/
rep.go
63 lines (50 loc) · 1.17 KB
/
rep.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
// 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 playlist
import (
"fmt"
"tchaik.com/index"
)
// Action is a type used to represent a playlist mutation action.
type Action string
const (
ActionCreate Action = "create"
ActionDelete = "delete"
ActionAddItem = "addItem"
ActionRemoveItem = "deleteItem"
)
var actionToAction = map[string]Action{
"ADD_ITEM": ActionAddItem,
"REMOVE": ActionRemoveItem,
}
type RepAction struct {
Name string `json:"name"`
Action Action `json:"action"`
Path index.Path `json:"path"`
Index int `json:"index"`
}
func (a RepAction) Apply(s Store) error {
if a.Action == ActionCreate {
s.Set(a.Name, &Playlist{})
return nil
}
action, ok := actionToAction[string(a.Action)]
if !ok {
return fmt.Errorf("unknown action: %v", a.Action)
}
p := s.Get(a.Name)
if p == nil {
return fmt.Errorf("invalid playlist name: '%v'", a.Name)
}
switch action {
case ActionDelete:
s.Delete(a.Name)
case ActionAddItem:
p.Add(a.Path)
case ActionRemoveItem:
p.Remove(a.Index, a.Path)
}
s.Set(a.Name, p)
return nil
}