-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
flag.go
101 lines (75 loc) · 1.64 KB
/
flag.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
package goriak
import (
"encoding/json"
"errors"
riak "github.com/basho/riak-go-client"
)
func NewFlag() *Flag {
return &Flag{}
}
type Flag struct {
helper
val bool
}
func (f *Flag) Value() bool {
return f.val
}
func (f *Flag) Set(val bool) *Flag {
f.val = val
return f
}
func (f *Flag) Exec(client *Session) error {
if f == nil {
return errors.New("Nil Flag")
}
if f.name == "" {
return errors.New("Unknown path to Flag. Retrieve Flag with Get or Set before updating the Flag")
}
// Validate s.key
if f.key.bucket == "" || f.key.bucketType == "" || f.key.key == "" {
return errors.New("Invalid key in Flag Exec()")
}
op := &riak.MapOperation{}
outerOp := op
// Traverse c.path so that we increment the correct counter in nested maps
for _, subMapName := range f.path {
op = op.Map(subMapName)
}
op.SetFlag(f.name, f.val)
cmd, err := riak.NewUpdateMapCommandBuilder().
WithBucket(f.key.bucket).
WithBucketType(f.key.bucketType).
WithKey(f.key.key).
WithMapOperation(outerOp).
WithContext(f.context).
Build()
if err != nil {
return err
}
err = client.riak.Execute(cmd)
if err != nil {
return err
}
res, ok := cmd.(*riak.UpdateMapCommand)
if !ok {
return errors.New("Could not convert")
}
if !res.Success() {
return errors.New("Not successful")
}
return nil
}
// MarshalJSON satisfies the JSON interface
func (f Flag) MarshalJSON() ([]byte, error) {
return json.Marshal(f.val)
}
// UnmarshalJSON satisfies the JSON interface
func (f *Flag) UnmarshalJSON(data []byte) error {
var value bool
err := json.Unmarshal(data, &value)
if err != nil {
return err
}
f.val = value
return nil
}