Browse Source

client: support PrevValue in SetOptions & DeleteOptions

Brian Waldon 11 years ago
parent
commit
7ccf5eb476
2 changed files with 31 additions and 0 deletions
  1. 8 0
      client/keys.go
  2. 23 0
      client/keys_test.go

+ 8 - 0
client/keys.go

@@ -75,10 +75,12 @@ type KeysAPI interface {
 }
 
 type SetOptions struct {
+	PrevValue string
 	PrevExist PrevExistType
 }
 
 type DeleteOptions struct {
+	PrevValue string
 	Recursive bool
 }
 
@@ -282,6 +284,9 @@ func (a *setAction) HTTPRequest(ep url.URL) *http.Request {
 	u := v2KeysURL(ep, a.Prefix, a.Key)
 
 	params := u.Query()
+	if a.Options.PrevValue != "" {
+		params.Set("prevValue", a.Options.PrevValue)
+	}
 	if a.Options.PrevExist != PrevIgnore {
 		params.Set("prevExist", string(a.Options.PrevExist))
 	}
@@ -308,6 +313,9 @@ func (a *deleteAction) HTTPRequest(ep url.URL) *http.Request {
 	u := v2KeysURL(ep, a.Prefix, a.Key)
 
 	params := u.Query()
+	if a.Options.PrevValue != "" {
+		params.Set("prevValue", a.Options.PrevValue)
+	}
 	if a.Options.Recursive {
 		params.Set("recursive", "true")
 	}

+ 23 - 0
client/keys_test.go

@@ -289,6 +289,18 @@ func TestSetAction(t *testing.T) {
 			wantURL:  "http://example.com/foo?prevExist=false",
 			wantBody: "value=",
 		},
+
+		// PrevValue is urlencoded
+		{
+			act: setAction{
+				Key: "foo",
+				Options: SetOptions{
+					PrevValue: "bar baz",
+				},
+			},
+			wantURL:  "http://example.com/foo?prevValue=bar+baz",
+			wantBody: "value=",
+		},
 	}
 
 	for i, tt := range tests {
@@ -375,6 +387,17 @@ func TestDeleteAction(t *testing.T) {
 			},
 			wantURL: "http://example.com/foo?recursive=true",
 		},
+
+		// PrevValue is urlencoded
+		{
+			act: deleteAction{
+				Key: "foo",
+				Options: DeleteOptions{
+					PrevValue: "bar baz",
+				},
+			},
+			wantURL: "http://example.com/foo?prevValue=bar+baz",
+		},
 	}
 
 	for i, tt := range tests {