Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
b0dc0cd8
Commit
b0dc0cd8
authored
May 01, 2021
by
ztelur
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
check suffix
parent
9ec34fee
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
9 deletions
+19
-9
client.go
database/kv/etcd/v3/client.go
+11
-2
client_test.go
database/kv/etcd/v3/client_test.go
+8
-7
No files found.
database/kv/etcd/v3/client.go
View file @
b0dc0cd8
...
...
@@ -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
)
}
database/kv/etcd/v3/client_test.go
View file @
b0dc0cd8
...
...
@@ -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
/name
Prefix"
,
v
:
"prefix.scott.wang"
}},
{
input
:
struct
{
k
string
v
string
}{
k
:
"namePrefix1"
,
v
:
"prefix1.scott.wang"
}},
}{
k
:
"name
/name
Prefix1"
,
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
,
prefix
Key
)
{
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
(
prefix
Key
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -373,7 +374,7 @@ func (suite *ClientTestSuite) TestClientWatch() {
c
.
Close
()
}()
wc
,
err
:=
c
.
WatchWithOption
(
p
refix
)
wc
,
err
:=
c
.
WatchWithOption
(
keyP
refix
)
if
err
!=
nil
{
assert
.
Error
(
t
,
err
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment