...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package clientv3
16
17 import (
18 pb "go.etcd.io/etcd/api/v3/etcdserverpb"
19 )
20
21 type CompareTarget int
22 type CompareResult int
23
24 const (
25 CompareVersion CompareTarget = iota
26 CompareCreated
27 CompareModified
28 CompareValue
29 )
30
31 type Cmp pb.Compare
32
33 func Compare(cmp Cmp, result string, v interface{}) Cmp {
34 var r pb.Compare_CompareResult
35
36 switch result {
37 case "=":
38 r = pb.Compare_EQUAL
39 case "!=":
40 r = pb.Compare_NOT_EQUAL
41 case ">":
42 r = pb.Compare_GREATER
43 case "<":
44 r = pb.Compare_LESS
45 default:
46 panic("Unknown result op")
47 }
48
49 cmp.Result = r
50 switch cmp.Target {
51 case pb.Compare_VALUE:
52 val, ok := v.(string)
53 if !ok {
54 panic("bad compare value")
55 }
56 cmp.TargetUnion = &pb.Compare_Value{Value: []byte(val)}
57 case pb.Compare_VERSION:
58 cmp.TargetUnion = &pb.Compare_Version{Version: mustInt64(v)}
59 case pb.Compare_CREATE:
60 cmp.TargetUnion = &pb.Compare_CreateRevision{CreateRevision: mustInt64(v)}
61 case pb.Compare_MOD:
62 cmp.TargetUnion = &pb.Compare_ModRevision{ModRevision: mustInt64(v)}
63 case pb.Compare_LEASE:
64 cmp.TargetUnion = &pb.Compare_Lease{Lease: mustInt64orLeaseID(v)}
65 default:
66 panic("Unknown compare type")
67 }
68 return cmp
69 }
70
71 func Value(key string) Cmp {
72 return Cmp{Key: []byte(key), Target: pb.Compare_VALUE}
73 }
74
75 func Version(key string) Cmp {
76 return Cmp{Key: []byte(key), Target: pb.Compare_VERSION}
77 }
78
79 func CreateRevision(key string) Cmp {
80 return Cmp{Key: []byte(key), Target: pb.Compare_CREATE}
81 }
82
83 func ModRevision(key string) Cmp {
84 return Cmp{Key: []byte(key), Target: pb.Compare_MOD}
85 }
86
87
88
89 func LeaseValue(key string) Cmp {
90 return Cmp{Key: []byte(key), Target: pb.Compare_LEASE}
91 }
92
93
94 func (cmp *Cmp) KeyBytes() []byte { return cmp.Key }
95
96
97 func (cmp *Cmp) WithKeyBytes(key []byte) { cmp.Key = key }
98
99
100 func (cmp *Cmp) ValueBytes() []byte {
101 if tu, ok := cmp.TargetUnion.(*pb.Compare_Value); ok {
102 return tu.Value
103 }
104 return nil
105 }
106
107
108 func (cmp *Cmp) WithValueBytes(v []byte) { cmp.TargetUnion.(*pb.Compare_Value).Value = v }
109
110
111 func (cmp Cmp) WithRange(end string) Cmp {
112 cmp.RangeEnd = []byte(end)
113 return cmp
114 }
115
116
117 func (cmp Cmp) WithPrefix() Cmp {
118 cmp.RangeEnd = getPrefix(cmp.Key)
119 return cmp
120 }
121
122
123 func mustInt64(val interface{}) int64 {
124 if v, ok := val.(int64); ok {
125 return v
126 }
127 if v, ok := val.(int); ok {
128 return int64(v)
129 }
130 panic("bad value")
131 }
132
133
134
135 func mustInt64orLeaseID(val interface{}) int64 {
136 if v, ok := val.(LeaseID); ok {
137 return int64(v)
138 }
139 return mustInt64(val)
140 }
141
View as plain text