Browse Source

client: add GetOptions.Sort

Brian Waldon 11 years ago
parent
commit
479a17dcbf
2 changed files with 26 additions and 2 deletions
  1. 10 0
      client/keys.go
  2. 16 2
      client/keys_test.go

+ 10 - 0
client/keys.go

@@ -115,6 +115,13 @@ type GetOptions struct {
 	// Recursive defines whether or not all children of the Node
 	// Recursive defines whether or not all children of the Node
 	// should be returned.
 	// should be returned.
 	Recursive bool
 	Recursive bool
+
+	// Sort instructs the server whether or not to sort the Nodes.
+	// If true, the Nodes are sorted alphabetically by key in
+	// ascending order (A to z). If false (default), the Nodes will
+	// not be sorted and the ordering used should not be considered
+	// predictable.
+	Sort bool
 }
 }
 
 
 type DeleteOptions struct {
 type DeleteOptions struct {
@@ -236,6 +243,7 @@ func (k *httpKeysAPI) Get(ctx context.Context, key string, opts *GetOptions) (*R
 
 
 	if opts != nil {
 	if opts != nil {
 		act.Recursive = opts.Recursive
 		act.Recursive = opts.Recursive
+		act.Sorted = opts.Sort
 	}
 	}
 
 
 	resp, body, err := k.client.Do(ctx, act)
 	resp, body, err := k.client.Do(ctx, act)
@@ -297,6 +305,7 @@ type getAction struct {
 	Prefix    string
 	Prefix    string
 	Key       string
 	Key       string
 	Recursive bool
 	Recursive bool
+	Sorted    bool
 }
 }
 
 
 func (g *getAction) HTTPRequest(ep url.URL) *http.Request {
 func (g *getAction) HTTPRequest(ep url.URL) *http.Request {
@@ -304,6 +313,7 @@ func (g *getAction) HTTPRequest(ep url.URL) *http.Request {
 
 
 	params := u.Query()
 	params := u.Query()
 	params.Set("recursive", strconv.FormatBool(g.Recursive))
 	params.Set("recursive", strconv.FormatBool(g.Recursive))
+	params.Set("sorted", strconv.FormatBool(g.Sorted))
 	u.RawQuery = params.Encode()
 	u.RawQuery = params.Encode()
 
 
 	req, _ := http.NewRequest("GET", u.String(), nil)
 	req, _ := http.NewRequest("GET", u.String(), nil)

+ 16 - 2
client/keys_test.go

@@ -99,15 +99,28 @@ func TestGetAction(t *testing.T) {
 
 
 	tests := []struct {
 	tests := []struct {
 		recursive bool
 		recursive bool
+		sorted    bool
 		wantQuery string
 		wantQuery string
 	}{
 	}{
 		{
 		{
 			recursive: false,
 			recursive: false,
-			wantQuery: "recursive=false",
+			sorted:    false,
+			wantQuery: "recursive=false&sorted=false",
 		},
 		},
 		{
 		{
 			recursive: true,
 			recursive: true,
-			wantQuery: "recursive=true",
+			sorted:    false,
+			wantQuery: "recursive=true&sorted=false",
+		},
+		{
+			recursive: false,
+			sorted:    true,
+			wantQuery: "recursive=false&sorted=true",
+		},
+		{
+			recursive: true,
+			sorted:    true,
+			wantQuery: "recursive=true&sorted=true",
 		},
 		},
 	}
 	}
 
 
@@ -115,6 +128,7 @@ func TestGetAction(t *testing.T) {
 		f := getAction{
 		f := getAction{
 			Key:       "/foo/bar",
 			Key:       "/foo/bar",
 			Recursive: tt.recursive,
 			Recursive: tt.recursive,
+			Sorted:    tt.sorted,
 		}
 		}
 		got := *f.HTTPRequest(ep)
 		got := *f.HTTPRequest(ep)