generated from openacid/gotmpl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd.go
89 lines (74 loc) · 1.33 KB
/
cmd.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
package traft
import (
fmt "fmt"
"strconv"
"strings"
)
func NewCmdI64(op, key string, v int64) *Cmd {
cmd := &Cmd{
Op: op,
Key: key,
Value: &Cmd_VI64{v},
}
return cmd
}
func cmdValueShortStr(v isCmd_Value) string {
switch vv := v.(type) {
case *Cmd_VI64:
return fmt.Sprintf("%d", vv.VI64)
case *Cmd_VStr:
return vv.VStr
// TODO Cluster
default:
return fmt.Sprintf("%s", vv)
}
}
func (c *Cmd) ShortStr() string {
if c == nil {
return "()"
}
return fmt.Sprintf("%s(%s, %s)",
c.Op, c.Key, cmdValueShortStr(c.Value))
}
// Interfering check if a command interferes with another one,
// i.e. they change the same key.
func (a *Cmd) Interfering(b *Cmd) bool {
if a == nil || b == nil {
return false
}
if a.Op == "set" && b.Op == "set" {
if a.Key == b.Key {
return true
}
}
return false
}
type toCmder interface {
ToCmd() *Cmd
}
type cstr string
func (c *cstr) ToCmd() *Cmd {
if *c == "" {
return nil
}
kv := strings.Split(string(*c), "=")
k := kv[0]
v, err := strconv.ParseInt(kv[1], 10, 64)
if err != nil {
panic(string(*c) + " convert to Cmd")
}
return NewCmdI64("set", k, v)
}
func toCmd(x interface{}) *Cmd {
if x == nil {
return nil
}
switch v := x.(type) {
case string:
s := cstr(v)
return s.ToCmd()
case *Cmd:
return v
}
panic("invalid type to convert to cmd")
}