mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
clientv3: make compare compliant with proposed txn usage
This commit is contained in:
parent
70006da092
commit
4f41d361a8
@ -30,7 +30,7 @@ const (
|
|||||||
|
|
||||||
type Cmp pb.Compare
|
type Cmp pb.Compare
|
||||||
|
|
||||||
func Compare(key string, t pb.Compare_CompareTarget, result string, v interface{}) Cmp {
|
func Compare(cmp Cmp, result string, v interface{}) Cmp {
|
||||||
var r pb.Compare_CompareResult
|
var r pb.Compare_CompareResult
|
||||||
|
|
||||||
switch result {
|
switch result {
|
||||||
@ -44,38 +44,40 @@ func Compare(key string, t pb.Compare_CompareTarget, result string, v interface{
|
|||||||
panic("Unknown result op")
|
panic("Unknown result op")
|
||||||
}
|
}
|
||||||
|
|
||||||
switch t {
|
cmp.Result = r
|
||||||
|
switch cmp.Target {
|
||||||
case pb.Compare_VALUE:
|
case pb.Compare_VALUE:
|
||||||
val, ok := v.(string)
|
val, ok := v.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("bad compare value")
|
panic("bad compare value")
|
||||||
}
|
}
|
||||||
return Cmp{Key: []byte(key), Result: r, Target: t, TargetUnion: &pb.Compare_Value{Value: []byte(val)}}
|
cmp.TargetUnion = &pb.Compare_Value{Value: []byte(val)}
|
||||||
case pb.Compare_VERSION:
|
case pb.Compare_VERSION:
|
||||||
return Cmp{Key: []byte(key), Result: r, Target: t, TargetUnion: &pb.Compare_Version{Version: mustInt64(v)}}
|
cmp.TargetUnion = &pb.Compare_Version{Version: mustInt64(v)}
|
||||||
case pb.Compare_CREATE:
|
case pb.Compare_CREATE:
|
||||||
return Cmp{Key: []byte(key), Result: r, Target: t, TargetUnion: &pb.Compare_CreateRevision{CreateRevision: mustInt64(v)}}
|
cmp.TargetUnion = &pb.Compare_CreateRevision{CreateRevision: mustInt64(v)}
|
||||||
case pb.Compare_MOD:
|
case pb.Compare_MOD:
|
||||||
return Cmp{Key: []byte(key), Result: r, Target: t, TargetUnion: &pb.Compare_ModRevision{ModRevision: mustInt64(v)}}
|
cmp.TargetUnion = &pb.Compare_ModRevision{ModRevision: mustInt64(v)}
|
||||||
default:
|
default:
|
||||||
panic("Unknown compare type")
|
panic("Unknown compare type")
|
||||||
}
|
}
|
||||||
|
return cmp
|
||||||
}
|
}
|
||||||
|
|
||||||
func Value(key string) (string, pb.Compare_CompareTarget) {
|
func Value(key string) Cmp {
|
||||||
return key, pb.Compare_VALUE
|
return Cmp{Key: []byte(key), Target: pb.Compare_VALUE}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Version(key string) (string, pb.Compare_CompareTarget) {
|
func Version(key string) Cmp {
|
||||||
return key, pb.Compare_VERSION
|
return Cmp{Key: []byte(key), Target: pb.Compare_VERSION}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreatedRevision(key string) (string, pb.Compare_CompareTarget) {
|
func CreatedRevision(key string) Cmp {
|
||||||
return key, pb.Compare_CREATE
|
return Cmp{Key: []byte(key), Target: pb.Compare_CREATE}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ModifiedRevision(key string) (string, pb.Compare_CompareTarget) {
|
func ModifiedRevision(key string) Cmp {
|
||||||
return key, pb.Compare_MOD
|
return Cmp{Key: []byte(key), Target: pb.Compare_MOD}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustInt64(val interface{}) int64 {
|
func mustInt64(val interface{}) int64 {
|
||||||
|
@ -33,8 +33,7 @@ func TestTxnPanics(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
k, tgt := CreatedRevision("foo")
|
cmp := Compare(CreatedRevision("foo"), "=", 0)
|
||||||
cmp := Compare(k, tgt, "=", 0)
|
|
||||||
op := OpPut("foo", "bar", 0)
|
op := OpPut("foo", "bar", 0)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user