Browse Source

client: use options struct for KeysAPI.Get

Brian Waldon 11 years ago
parent
commit
84ede6fbec
3 changed files with 22 additions and 30 deletions
  1. 16 24
      client/keys.go
  2. 2 2
      discovery/discovery.go
  3. 4 4
      discovery/discovery_test.go

+ 16 - 24
client/keys.go

@@ -56,14 +56,12 @@ func NewKeysAPIWithPrefix(c Client, p string) KeysAPI {
 }
 
 type KeysAPI interface {
+	Get(ctx context.Context, key string, opts *GetOptions) (*Response, error)
 	Set(ctx context.Context, key, value string, opts *SetOptions) (*Response, error)
-	Create(ctx context.Context, key, value string) (*Response, error)
-	Update(ctx context.Context, key, value string) (*Response, error)
-
 	Delete(ctx context.Context, key string, opts *DeleteOptions) (*Response, error)
 
-	Get(ctx context.Context, key string) (*Response, error)
-	RGet(ctx context.Context, key string) (*Response, error)
+	Create(ctx context.Context, key, value string) (*Response, error)
+	Update(ctx context.Context, key, value string) (*Response, error)
 
 	Watcher(key string, opts *WatcherOptions) Watcher
 }
@@ -113,6 +111,12 @@ type SetOptions struct {
 	TTL time.Duration
 }
 
+type GetOptions struct {
+	// Recursive defines whether or not all children of the Node
+	// should be returned.
+	Recursive bool
+}
+
 type DeleteOptions struct {
 	// PrevValue specifies what the current value of the Node must
 	// be in order for the Delete operation to succeed.
@@ -224,29 +228,17 @@ func (k *httpKeysAPI) Delete(ctx context.Context, key string, opts *DeleteOption
 	return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
 }
 
-func (k *httpKeysAPI) Get(ctx context.Context, key string) (*Response, error) {
-	get := &getAction{
-		Prefix:    k.prefix,
-		Key:       key,
-		Recursive: false,
-	}
-
-	resp, body, err := k.client.Do(ctx, get)
-	if err != nil {
-		return nil, err
+func (k *httpKeysAPI) Get(ctx context.Context, key string, opts *GetOptions) (*Response, error) {
+	act := &getAction{
+		Prefix: k.prefix,
+		Key:    key,
 	}
 
-	return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
-}
-
-func (k *httpKeysAPI) RGet(ctx context.Context, key string) (*Response, error) {
-	get := &getAction{
-		Prefix:    k.prefix,
-		Key:       key,
-		Recursive: true,
+	if opts != nil {
+		act.Recursive = opts.Recursive
 	}
 
-	resp, body, err := k.client.Do(ctx, get)
+	resp, body, err := k.client.Do(ctx, act)
 	if err != nil {
 		return nil, err
 	}

+ 2 - 2
discovery/discovery.go

@@ -199,7 +199,7 @@ func (d *discovery) checkCluster() ([]*client.Node, int, uint64, error) {
 	configKey := path.Join("/", d.cluster, "_config")
 	ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
 	// find cluster size
-	resp, err := d.c.Get(ctx, path.Join(configKey, "size"))
+	resp, err := d.c.Get(ctx, path.Join(configKey, "size"), nil)
 	cancel()
 	if err != nil {
 		if err == client.ErrKeyNoExist {
@@ -216,7 +216,7 @@ func (d *discovery) checkCluster() ([]*client.Node, int, uint64, error) {
 	}
 
 	ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
-	resp, err = d.c.Get(ctx, d.cluster)
+	resp, err = d.c.Get(ctx, d.cluster, nil)
 	cancel()
 	if err != nil {
 		if err == client.ErrTimeout {

+ 4 - 4
discovery/discovery_test.go

@@ -422,7 +422,7 @@ func (c *clientWithResp) Create(ctx context.Context, key string, value string) (
 	return r, nil
 }
 
-func (c *clientWithResp) Get(ctx context.Context, key string) (*client.Response, error) {
+func (c *clientWithResp) Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error) {
 	if len(c.rs) == 0 {
 		return &client.Response{}, client.ErrKeyNoExist
 	}
@@ -445,7 +445,7 @@ func (c *clientWithErr) Create(ctx context.Context, key string, value string) (*
 	return &client.Response{}, c.err
 }
 
-func (c *clientWithErr) Get(ctx context.Context, key string) (*client.Response, error) {
+func (c *clientWithErr) Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error) {
 	return &client.Response{}, c.err
 }
 
@@ -490,12 +490,12 @@ func (c *clientWithRetry) Create(ctx context.Context, key string, value string)
 	return c.clientWithResp.Create(ctx, key, value)
 }
 
-func (c *clientWithRetry) Get(ctx context.Context, key string) (*client.Response, error) {
+func (c *clientWithRetry) Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error) {
 	if c.failCount < c.failTimes {
 		c.failCount++
 		return nil, client.ErrTimeout
 	}
-	return c.clientWithResp.Get(ctx, key)
+	return c.clientWithResp.Get(ctx, key, opts)
 }
 
 // watcherWithRetry will timeout all requests up to failTimes