Skip to content
This repository was archived by the owner on Jul 21, 2021. It is now read-only.

Commit e6b59f6

Browse files
nemithsamuel
authored andcommitted
Allow for trailing '/' for Create with FlagSequence (#172)
@neolf pointed out that Create calls with FlagSequence flag set can have a trailing slash. This replicates what Java check does and allows for a trailing slash for when Create is called with FlagSequence.
1 parent 8ac67fa commit e6b59f6

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

zk/conn.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ func (c *Conn) AddAuth(scheme string, auth []byte) error {
906906
}
907907

908908
func (c *Conn) Children(path string) ([]string, *Stat, error) {
909-
if err := validatePath(path); err != nil {
909+
if err := validatePath(path, false); err != nil {
910910
return nil, nil, err
911911
}
912912

@@ -916,7 +916,7 @@ func (c *Conn) Children(path string) ([]string, *Stat, error) {
916916
}
917917

918918
func (c *Conn) ChildrenW(path string) ([]string, *Stat, <-chan Event, error) {
919-
if err := validatePath(path); err != nil {
919+
if err := validatePath(path, false); err != nil {
920920
return nil, nil, nil, err
921921
}
922922

@@ -934,7 +934,7 @@ func (c *Conn) ChildrenW(path string) ([]string, *Stat, <-chan Event, error) {
934934
}
935935

936936
func (c *Conn) Get(path string) ([]byte, *Stat, error) {
937-
if err := validatePath(path); err != nil {
937+
if err := validatePath(path, false); err != nil {
938938
return nil, nil, err
939939
}
940940

@@ -945,7 +945,7 @@ func (c *Conn) Get(path string) ([]byte, *Stat, error) {
945945

946946
// GetW returns the contents of a znode and sets a watch
947947
func (c *Conn) GetW(path string) ([]byte, *Stat, <-chan Event, error) {
948-
if err := validatePath(path); err != nil {
948+
if err := validatePath(path, false); err != nil {
949949
return nil, nil, nil, err
950950
}
951951

@@ -963,7 +963,7 @@ func (c *Conn) GetW(path string) ([]byte, *Stat, <-chan Event, error) {
963963
}
964964

965965
func (c *Conn) Set(path string, data []byte, version int32) (*Stat, error) {
966-
if err := validatePath(path); err != nil {
966+
if err := validatePath(path, false); err != nil {
967967
return nil, err
968968
}
969969

@@ -973,7 +973,7 @@ func (c *Conn) Set(path string, data []byte, version int32) (*Stat, error) {
973973
}
974974

975975
func (c *Conn) Create(path string, data []byte, flags int32, acl []ACL) (string, error) {
976-
if err := validatePath(path); err != nil {
976+
if err := validatePath(path, flags&FlagSequence == FlagSequence); err != nil {
977977
return "", err
978978
}
979979

@@ -987,7 +987,7 @@ func (c *Conn) Create(path string, data []byte, flags int32, acl []ACL) (string,
987987
// ephemeral node still exists. Therefore, on reconnect we need to check if a node
988988
// with a GUID generated on create exists.
989989
func (c *Conn) CreateProtectedEphemeralSequential(path string, data []byte, acl []ACL) (string, error) {
990-
if err := validatePath(path); err != nil {
990+
if err := validatePath(path, true); err != nil {
991991
return "", err
992992
}
993993

@@ -1032,7 +1032,7 @@ func (c *Conn) CreateProtectedEphemeralSequential(path string, data []byte, acl
10321032
}
10331033

10341034
func (c *Conn) Delete(path string, version int32) error {
1035-
if err := validatePath(path); err != nil {
1035+
if err := validatePath(path, false); err != nil {
10361036
return err
10371037
}
10381038

@@ -1041,7 +1041,7 @@ func (c *Conn) Delete(path string, version int32) error {
10411041
}
10421042

10431043
func (c *Conn) Exists(path string) (bool, *Stat, error) {
1044-
if err := validatePath(path); err != nil {
1044+
if err := validatePath(path, false); err != nil {
10451045
return false, nil, err
10461046
}
10471047

@@ -1056,7 +1056,7 @@ func (c *Conn) Exists(path string) (bool, *Stat, error) {
10561056
}
10571057

10581058
func (c *Conn) ExistsW(path string) (bool, *Stat, <-chan Event, error) {
1059-
if err := validatePath(path); err != nil {
1059+
if err := validatePath(path, false); err != nil {
10601060
return false, nil, nil, err
10611061
}
10621062

@@ -1081,7 +1081,7 @@ func (c *Conn) ExistsW(path string) (bool, *Stat, <-chan Event, error) {
10811081
}
10821082

10831083
func (c *Conn) GetACL(path string) ([]ACL, *Stat, error) {
1084-
if err := validatePath(path); err != nil {
1084+
if err := validatePath(path, false); err != nil {
10851085
return nil, nil, err
10861086
}
10871087

@@ -1090,7 +1090,7 @@ func (c *Conn) GetACL(path string) ([]ACL, *Stat, error) {
10901090
return res.Acl, &res.Stat, err
10911091
}
10921092
func (c *Conn) SetACL(path string, acl []ACL, version int32) (*Stat, error) {
1093-
if err := validatePath(path); err != nil {
1093+
if err := validatePath(path, false); err != nil {
10941094
return nil, err
10951095
}
10961096

@@ -1100,7 +1100,7 @@ func (c *Conn) SetACL(path string, acl []ACL, version int32) (*Stat, error) {
11001100
}
11011101

11021102
func (c *Conn) Sync(path string) (string, error) {
1103-
if err := validatePath(path); err != nil {
1103+
if err := validatePath(path, false); err != nil {
11041104
return "", err
11051105
}
11061106

zk/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func stringShuffle(s []string) {
5555
}
5656

5757
// validatePath will make sure a path is valid before sending the request
58-
func validatePath(path string) error {
58+
func validatePath(path string, isSequential bool) error {
5959
if path == "" {
6060
return ErrInvalidPath
6161
}
@@ -70,7 +70,7 @@ func validatePath(path string) error {
7070
return nil
7171
}
7272

73-
if path[n-1] == '/' {
73+
if !isSequential && path[n-1] == '/' {
7474
return ErrInvalidPath
7575
}
7676

zk/util_test.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,36 @@ func TestFormatServers(t *testing.T) {
1616
func TestValidatePath(t *testing.T) {
1717
tt := []struct {
1818
path string
19+
seq bool
1920
valid bool
2021
}{
21-
{"/this is / a valid/path", true},
22-
{"/", true},
23-
{"", false},
24-
{"not/valid", false},
25-
{"/ends/with/slash/", false},
26-
{"/test\u0000", false},
27-
{"/double//slash", false},
28-
{"/single/./period", false},
29-
{"/double/../period", false},
30-
{"/double/..ok/period", true},
31-
{"/double/alsook../period", true},
32-
{"/double/period/at/end/..", false},
33-
{"/name/with.period", true},
34-
{"/test\u0001", false},
35-
{"/test\u001f", false},
36-
{"/test\u0020", true}, // first allowable
37-
{"/test\u007e", true}, // last valid ascii
38-
{"/test\u007f", false},
39-
{"/test\u009f", false},
40-
{"/test\uf8ff", false},
41-
{"/test\uffef", true},
42-
{"/test\ufff0", false},
22+
{"/this is / a valid/path", false, true},
23+
{"/", false, true},
24+
{"", false, false},
25+
{"not/valid", false, false},
26+
{"/ends/with/slash/", false, false},
27+
{"/sequential/", true, true},
28+
{"/test\u0000", false, false},
29+
{"/double//slash", false, false},
30+
{"/single/./period", false, false},
31+
{"/double/../period", false, false},
32+
{"/double/..ok/period", false, true},
33+
{"/double/alsook../period", false, true},
34+
{"/double/period/at/end/..", false, false},
35+
{"/name/with.period", false, true},
36+
{"/test\u0001", false, false},
37+
{"/test\u001f", false, false},
38+
{"/test\u0020", false, true}, // first allowable
39+
{"/test\u007e", false, true}, // last valid ascii
40+
{"/test\u007f", false, false},
41+
{"/test\u009f", false, false},
42+
{"/test\uf8ff", false, false},
43+
{"/test\uffef", false, true},
44+
{"/test\ufff0", false, false},
4345
}
4446

4547
for _, tc := range tt {
46-
err := validatePath(tc.path)
48+
err := validatePath(tc.path, tc.seq)
4749
if (err != nil) == tc.valid {
4850
t.Errorf("failed to validate path %q", tc.path)
4951
}

0 commit comments

Comments
 (0)