Browse Source

clientv3: make compare compliant with proposed txn usage

Anthony Romano 10 years ago
parent
commit
4f41d361a8
2 changed files with 17 additions and 16 deletions
  1. 16 14
      clientv3/compare.go
  2. 1 2
      clientv3/txn_test.go

+ 16 - 14
clientv3/compare.go

@@ -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) {
-	return key, pb.Compare_VALUE
+func Value(key string) Cmp {
+	return Cmp{Key: []byte(key), Target: pb.Compare_VALUE}
 }
 }
 
 
-func Version(key string) (string, pb.Compare_CompareTarget) {
-	return key, pb.Compare_VERSION
+func Version(key string) Cmp {
+	return Cmp{Key: []byte(key), Target: pb.Compare_VERSION}
 }
 }
 
 
-func CreatedRevision(key string) (string, pb.Compare_CompareTarget) {
-	return key, pb.Compare_CREATE
+func CreatedRevision(key string) Cmp {
+	return Cmp{Key: []byte(key), Target: pb.Compare_CREATE}
 }
 }
 
 
-func ModifiedRevision(key string) (string, pb.Compare_CompareTarget) {
-	return key, pb.Compare_MOD
+func ModifiedRevision(key string) Cmp {
+	return Cmp{Key: []byte(key), Target: pb.Compare_MOD}
 }
 }
 
 
 func mustInt64(val interface{}) int64 {
 func mustInt64(val interface{}) int64 {

+ 1 - 2
clientv3/txn_test.go

@@ -33,8 +33,7 @@ func TestTxnPanics(t *testing.T) {
 		}
 		}
 	}
 	}
 
 
-	k, tgt := CreatedRevision("foo")
-	cmp := Compare(k, tgt, "=", 0)
+	cmp := Compare(CreatedRevision("foo"), "=", 0)
 	op := OpPut("foo", "bar", 0)
 	op := OpPut("foo", "bar", 0)
 
 
 	tests := []struct {
 	tests := []struct {