Commit b0dc0cd8 authored by ztelur's avatar ztelur

check suffix

parent 9ec34fee
...@@ -20,6 +20,7 @@ package gxetcd ...@@ -20,6 +20,7 @@ package gxetcd
import ( import (
"context" "context"
"log" "log"
"strings"
"sync" "sync"
"time" "time"
) )
...@@ -363,6 +364,10 @@ func (c *Client) GetChildren(k string) ([]string, []string, error) { ...@@ -363,6 +364,10 @@ func (c *Client) GetChildren(k string) ([]string, []string, error) {
return nil, nil, ErrNilETCDV3Client return nil, nil, ErrNilETCDV3Client
} }
if !strings.HasSuffix(k, "/") {
k += "/"
}
resp, err := rawClient.Get(c.ctx, k, clientv3.WithPrefix()) resp, err := rawClient.Get(c.ctx, k, clientv3.WithPrefix())
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
...@@ -491,11 +496,15 @@ func (c *Client) Get(k string) (string, error) { ...@@ -491,11 +496,15 @@ func (c *Client) Get(k string) (string, error) {
// Watch watches on spec key // Watch watches on spec key
func (c *Client) Watch(k string) (clientv3.WatchChan, error) { func (c *Client) Watch(k string) (clientv3.WatchChan, error) {
wc, err := c.watchWithOption(k) wc, err := c.watchWithOption(k)
return wc, perrors.WithMessagef(err, "watch prefix (key %s)", k) return wc, perrors.WithMessagef(err, "watch (key %s)", k)
} }
// WatchWithPrefix watches on spec prefix // WatchWithPrefix watches on spec prefix
func (c *Client) WatchWithPrefix(prefix string) (clientv3.WatchChan, error) { func (c *Client) WatchWithPrefix(prefix string) (clientv3.WatchChan, error) {
if !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
wc, err := c.watchWithOption(prefix, clientv3.WithPrefix()) wc, err := c.watchWithOption(prefix, clientv3.WithPrefix())
return wc, perrors.WithMessagef(err, "watch prefix (key %s)", prefix) return wc, perrors.WithMessagef(err, "watch prefix (key %s)", prefix)
} }
...@@ -503,5 +512,5 @@ func (c *Client) WatchWithPrefix(prefix string) (clientv3.WatchChan, error) { ...@@ -503,5 +512,5 @@ func (c *Client) WatchWithPrefix(prefix string) (clientv3.WatchChan, error) {
// Watch watches on spc key with OpOption // Watch watches on spc key with OpOption
func (c *Client) WatchWithOption(k string, opts ...clientv3.OpOption) (clientv3.WatchChan, error) { func (c *Client) WatchWithOption(k string, opts ...clientv3.OpOption) (clientv3.WatchChan, error) {
wc, err := c.watchWithOption(k, opts...) wc, err := c.watchWithOption(k, opts...)
return wc, perrors.WithMessagef(err, "watch prefix (key %s)", k) return wc, perrors.WithMessagef(err, "watch (key %s)", k)
} }
...@@ -48,15 +48,15 @@ var tests = []struct { ...@@ -48,15 +48,15 @@ var tests = []struct {
{input: struct { {input: struct {
k string k string
v string v string
}{k: "name", v: "scott.wang"}}, }{k: "name/name", v: "scott.wang"}},
{input: struct { {input: struct {
k string k string
v string v string
}{k: "namePrefix", v: "prefix.scott.wang"}}, }{k: "name/namePrefix", v: "prefix.scott.wang"}},
{input: struct { {input: struct {
k string k string
v string v string
}{k: "namePrefix1", v: "prefix1.scott.wang"}}, }{k: "name/namePrefix1", v: "prefix1.scott.wang"}},
{input: struct { {input: struct {
k string k string
v string v string
...@@ -64,7 +64,8 @@ var tests = []struct { ...@@ -64,7 +64,8 @@ var tests = []struct {
} }
// test dataset prefix // test dataset prefix
const prefix = "name" const prefixKey = "name/"
const keyPrefix = "name/name"
type ClientTestSuite struct { type ClientTestSuite struct {
suite.Suite suite.Suite
...@@ -326,7 +327,7 @@ func (suite *ClientTestSuite) TestClientGetChildrenKVList() { ...@@ -326,7 +327,7 @@ func (suite *ClientTestSuite) TestClientGetChildrenKVList() {
k := tc.input.k k := tc.input.k
v := tc.input.v v := tc.input.v
if strings.Contains(k, prefix) { if strings.Contains(k, prefixKey) {
expectKList = append(expectKList, k) expectKList = append(expectKList, k)
expectVList = append(expectVList, v) expectVList = append(expectVList, v)
} }
...@@ -336,7 +337,7 @@ func (suite *ClientTestSuite) TestClientGetChildrenKVList() { ...@@ -336,7 +337,7 @@ func (suite *ClientTestSuite) TestClientGetChildrenKVList() {
} }
} }
kList, vList, err := c.GetChildrenKVList(prefix) kList, vList, err := c.GetChildrenKVList(prefixKey)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -373,7 +374,7 @@ func (suite *ClientTestSuite) TestClientWatch() { ...@@ -373,7 +374,7 @@ func (suite *ClientTestSuite) TestClientWatch() {
c.Close() c.Close()
}() }()
wc, err := c.WatchWithOption(prefix) wc, err := c.WatchWithOption(keyPrefix)
if err != nil { if err != nil {
assert.Error(t, err) assert.Error(t, err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment