client: add GetOptions.Sort

This commit is contained in:
Brian Waldon 2015-01-28 12:45:05 -08:00 committed by Yicheng Qin
parent 84ede6fbec
commit 479a17dcbf
2 changed files with 26 additions and 2 deletions

View File

@ -115,6 +115,13 @@ type GetOptions struct {
// Recursive defines whether or not all children of the Node
// should be returned.
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 {
@ -236,6 +243,7 @@ func (k *httpKeysAPI) Get(ctx context.Context, key string, opts *GetOptions) (*R
if opts != nil {
act.Recursive = opts.Recursive
act.Sorted = opts.Sort
}
resp, body, err := k.client.Do(ctx, act)
@ -297,6 +305,7 @@ type getAction struct {
Prefix string
Key string
Recursive bool
Sorted bool
}
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.Set("recursive", strconv.FormatBool(g.Recursive))
params.Set("sorted", strconv.FormatBool(g.Sorted))
u.RawQuery = params.Encode()
req, _ := http.NewRequest("GET", u.String(), nil)

View File

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