Commit b0dc0cd8 authored by ztelur's avatar ztelur

check suffix

parent 9ec34fee
......@@ -20,6 +20,7 @@ package gxetcd
import (
"context"
"log"
"strings"
"sync"
"time"
)
......@@ -363,6 +364,10 @@ func (c *Client) GetChildren(k string) ([]string, []string, error) {
return nil, nil, ErrNilETCDV3Client
}
if !strings.HasSuffix(k, "/") {
k += "/"
}
resp, err := rawClient.Get(c.ctx, k, clientv3.WithPrefix())
if err != nil {
return nil, nil, err
......@@ -491,11 +496,15 @@ func (c *Client) Get(k string) (string, error) {
// Watch watches on spec key
func (c *Client) Watch(k string) (clientv3.WatchChan, error) {
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
func (c *Client) WatchWithPrefix(prefix string) (clientv3.WatchChan, error) {
if !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
wc, err := c.watchWithOption(prefix, clientv3.WithPrefix())
return wc, perrors.WithMessagef(err, "watch prefix (key %s)", prefix)
}
......@@ -503,5 +512,5 @@ func (c *Client) WatchWithPrefix(prefix string) (clientv3.WatchChan, error) {
// Watch watches on spc key with OpOption
func (c *Client) WatchWithOption(k string, opts ...clientv3.OpOption) (clientv3.WatchChan, error) {
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 {
{input: struct {
k string
v string
}{k: "name", v: "scott.wang"}},
}{k: "name/name", v: "scott.wang"}},
{input: struct {
k string
v string
}{k: "namePrefix", v: "prefix.scott.wang"}},
}{k: "name/namePrefix", v: "prefix.scott.wang"}},
{input: struct {
k string
v string
}{k: "namePrefix1", v: "prefix1.scott.wang"}},
}{k: "name/namePrefix1", v: "prefix1.scott.wang"}},
{input: struct {
k string
v string
......@@ -64,7 +64,8 @@ var tests = []struct {
}
// test dataset prefix
const prefix = "name"
const prefixKey = "name/"
const keyPrefix = "name/name"
type ClientTestSuite struct {
suite.Suite
......@@ -326,7 +327,7 @@ func (suite *ClientTestSuite) TestClientGetChildrenKVList() {
k := tc.input.k
v := tc.input.v
if strings.Contains(k, prefix) {
if strings.Contains(k, prefixKey) {
expectKList = append(expectKList, k)
expectVList = append(expectVList, v)
}
......@@ -336,7 +337,7 @@ func (suite *ClientTestSuite) TestClientGetChildrenKVList() {
}
}
kList, vList, err := c.GetChildrenKVList(prefix)
kList, vList, err := c.GetChildrenKVList(prefixKey)
if err != nil {
t.Fatal(err)
}
......@@ -373,7 +374,7 @@ func (suite *ClientTestSuite) TestClientWatch() {
c.Close()
}()
wc, err := c.WatchWithOption(prefix)
wc, err := c.WatchWithOption(keyPrefix)
if err != nil {
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