Browse Source

clientv3: fix txn example code

Gyu-Ho Lee 9 years ago
parent
commit
81f77ee4f3
1 changed files with 11 additions and 6 deletions
  1. 11 6
      clientv3/example_kv_test.go

+ 11 - 6
clientv3/example_kv_test.go

@@ -170,19 +170,23 @@ func ExampleKV_txn() {
 
 	kvc := clientv3.NewKV(cli)
 
-	// TODO: 'if' not working. Add expected output once fixed.
+	_, err = kvc.Put(context.TODO(), "key", "xyz")
+	if err != nil {
+		log.Fatal(err)
+	}
+
 	ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
 	_, err = kvc.Txn(ctx).
-		If(clientv3.Compare(clientv3.Value("1"), ">", "1")).
-		Then(clientv3.OpPut("1", "100")).
-		Else(clientv3.OpPut("1", "-1")).
+		If(clientv3.Compare(clientv3.Value("key"), ">", "abc")). // txn value comparisons are lexical
+		Then(clientv3.OpPut("key", "XYZ")).                      // this runs, since 'xyz' > 'abc'
+		Else(clientv3.OpPut("key", "ABC")).
 		Commit()
 	cancel()
-	if err == nil {
+	if err != nil {
 		log.Fatal(err)
 	}
 
-	gresp, err := kvc.Get(ctx, "1")
+	gresp, err := kvc.Get(context.TODO(), "key")
 	cancel()
 	if err != nil {
 		log.Fatal(err)
@@ -190,4 +194,5 @@ func ExampleKV_txn() {
 	for _, ev := range gresp.Kvs {
 		fmt.Printf("%s : %s\n", ev.Key, ev.Value)
 	}
+	// key : XYZ
 }