-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Persson
committed
Oct 5, 2015
1 parent
0c62007
commit 90863ec
Showing
13 changed files
with
1,008 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.DS_Store | ||
pkg | ||
bin | ||
/pkg | ||
/bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2009 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package pathutil | ||
|
||
import "path" | ||
|
||
// CanonicalURLPath returns the canonical url path for p, which follows the rules: | ||
// 1. the path always starts with "/" | ||
// 2. replace multiple slashes with a single slash | ||
// 3. replace each '.' '..' path name element with equivalent one | ||
// 4. keep the trailing slash | ||
// The function is borrowed from stdlib http.cleanPath in server.go. | ||
func CanonicalURLPath(p string) string { | ||
if p == "" { | ||
return "/" | ||
} | ||
if p[0] != '/' { | ||
p = "/" + p | ||
} | ||
np := path.Clean(p) | ||
// path.Clean removes trailing slash except for root, | ||
// put the trailing slash back if necessary. | ||
if p[len(p)-1] == '/' && np != "/" { | ||
np += "/" | ||
} | ||
return np | ||
} |
38 changes: 38 additions & 0 deletions
38
vendor/src/github.com/coreos/etcd/pkg/pathutil/path_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2015 CoreOS, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pathutil | ||
|
||
import "testing" | ||
|
||
func TestCanonicalURLPath(t *testing.T) { | ||
tests := []struct { | ||
p string | ||
wp string | ||
}{ | ||
{"/a", "/a"}, | ||
{"", "/"}, | ||
{"a", "/a"}, | ||
{"//a", "/a"}, | ||
{"/a/.", "/a"}, | ||
{"/a/..", "/"}, | ||
{"/a/", "/a/"}, | ||
{"/a//", "/a/"}, | ||
} | ||
for i, tt := range tests { | ||
if g := CanonicalURLPath(tt.p); g != tt.wp { | ||
t.Errorf("#%d: canonical path = %s, want %s", i, g, tt.wp) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2015 CoreOS, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package types | ||
|
||
import ( | ||
"strconv" | ||
) | ||
|
||
// ID represents a generic identifier which is canonically | ||
// stored as a uint64 but is typically represented as a | ||
// base-16 string for input/output | ||
type ID uint64 | ||
|
||
func (i ID) String() string { | ||
return strconv.FormatUint(uint64(i), 16) | ||
} | ||
|
||
// IDFromString attempts to create an ID from a base-16 string. | ||
func IDFromString(s string) (ID, error) { | ||
i, err := strconv.ParseUint(s, 16, 64) | ||
return ID(i), err | ||
} | ||
|
||
// IDSlice implements the sort interface | ||
type IDSlice []ID | ||
|
||
func (p IDSlice) Len() int { return len(p) } | ||
func (p IDSlice) Less(i, j int) bool { return uint64(p[i]) < uint64(p[j]) } | ||
func (p IDSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2015 CoreOS, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package types | ||
|
||
import ( | ||
"reflect" | ||
"sort" | ||
"testing" | ||
) | ||
|
||
func TestIDString(t *testing.T) { | ||
tests := []struct { | ||
input ID | ||
want string | ||
}{ | ||
{ | ||
input: 12, | ||
want: "c", | ||
}, | ||
{ | ||
input: 4918257920282737594, | ||
want: "444129853c343bba", | ||
}, | ||
} | ||
|
||
for i, tt := range tests { | ||
got := tt.input.String() | ||
if tt.want != got { | ||
t.Errorf("#%d: ID.String failure: want=%v, got=%v", i, tt.want, got) | ||
} | ||
} | ||
} | ||
|
||
func TestIDFromString(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
want ID | ||
}{ | ||
{ | ||
input: "17", | ||
want: 23, | ||
}, | ||
{ | ||
input: "612840dae127353", | ||
want: 437557308098245459, | ||
}, | ||
} | ||
|
||
for i, tt := range tests { | ||
got, err := IDFromString(tt.input) | ||
if err != nil { | ||
t.Errorf("#%d: IDFromString failure: err=%v", i, err) | ||
continue | ||
} | ||
if tt.want != got { | ||
t.Errorf("#%d: IDFromString failure: want=%v, got=%v", i, tt.want, got) | ||
} | ||
} | ||
} | ||
|
||
func TestIDFromStringFail(t *testing.T) { | ||
tests := []string{ | ||
"", | ||
"XXX", | ||
"612840dae127353612840dae127353", | ||
} | ||
|
||
for i, tt := range tests { | ||
_, err := IDFromString(tt) | ||
if err == nil { | ||
t.Fatalf("#%d: IDFromString expected error, but err=nil", i) | ||
} | ||
} | ||
} | ||
|
||
func TestIDSlice(t *testing.T) { | ||
g := []ID{10, 500, 5, 1, 100, 25} | ||
w := []ID{1, 5, 10, 25, 100, 500} | ||
sort.Sort(IDSlice(g)) | ||
if !reflect.DeepEqual(g, w) { | ||
t.Errorf("slice after sort = %#v, want %#v", g, w) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// Copyright 2015 CoreOS, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package types | ||
|
||
import ( | ||
"reflect" | ||
"sort" | ||
"sync" | ||
) | ||
|
||
type Set interface { | ||
Add(string) | ||
Remove(string) | ||
Contains(string) bool | ||
Equals(Set) bool | ||
Length() int | ||
Values() []string | ||
Copy() Set | ||
Sub(Set) Set | ||
} | ||
|
||
func NewUnsafeSet(values ...string) *unsafeSet { | ||
set := &unsafeSet{make(map[string]struct{})} | ||
for _, v := range values { | ||
set.Add(v) | ||
} | ||
return set | ||
} | ||
|
||
func NewThreadsafeSet(values ...string) *tsafeSet { | ||
us := NewUnsafeSet(values...) | ||
return &tsafeSet{us, sync.RWMutex{}} | ||
} | ||
|
||
type unsafeSet struct { | ||
d map[string]struct{} | ||
} | ||
|
||
// Add adds a new value to the set (no-op if the value is already present) | ||
func (us *unsafeSet) Add(value string) { | ||
us.d[value] = struct{}{} | ||
} | ||
|
||
// Remove removes the given value from the set | ||
func (us *unsafeSet) Remove(value string) { | ||
delete(us.d, value) | ||
} | ||
|
||
// Contains returns whether the set contains the given value | ||
func (us *unsafeSet) Contains(value string) (exists bool) { | ||
_, exists = us.d[value] | ||
return | ||
} | ||
|
||
// ContainsAll returns whether the set contains all given values | ||
func (us *unsafeSet) ContainsAll(values []string) bool { | ||
for _, s := range values { | ||
if !us.Contains(s) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// Equals returns whether the contents of two sets are identical | ||
func (us *unsafeSet) Equals(other Set) bool { | ||
v1 := sort.StringSlice(us.Values()) | ||
v2 := sort.StringSlice(other.Values()) | ||
v1.Sort() | ||
v2.Sort() | ||
return reflect.DeepEqual(v1, v2) | ||
} | ||
|
||
// Length returns the number of elements in the set | ||
func (us *unsafeSet) Length() int { | ||
return len(us.d) | ||
} | ||
|
||
// Values returns the values of the Set in an unspecified order. | ||
func (us *unsafeSet) Values() (values []string) { | ||
values = make([]string, 0) | ||
for val := range us.d { | ||
values = append(values, val) | ||
} | ||
return | ||
} | ||
|
||
// Copy creates a new Set containing the values of the first | ||
func (us *unsafeSet) Copy() Set { | ||
cp := NewUnsafeSet() | ||
for val := range us.d { | ||
cp.Add(val) | ||
} | ||
|
||
return cp | ||
} | ||
|
||
// Sub removes all elements in other from the set | ||
func (us *unsafeSet) Sub(other Set) Set { | ||
oValues := other.Values() | ||
result := us.Copy().(*unsafeSet) | ||
|
||
for _, val := range oValues { | ||
if _, ok := result.d[val]; !ok { | ||
continue | ||
} | ||
delete(result.d, val) | ||
} | ||
|
||
return result | ||
} | ||
|
||
type tsafeSet struct { | ||
us *unsafeSet | ||
m sync.RWMutex | ||
} | ||
|
||
func (ts *tsafeSet) Add(value string) { | ||
ts.m.Lock() | ||
defer ts.m.Unlock() | ||
ts.us.Add(value) | ||
} | ||
|
||
func (ts *tsafeSet) Remove(value string) { | ||
ts.m.Lock() | ||
defer ts.m.Unlock() | ||
ts.us.Remove(value) | ||
} | ||
|
||
func (ts *tsafeSet) Contains(value string) (exists bool) { | ||
ts.m.RLock() | ||
defer ts.m.RUnlock() | ||
return ts.us.Contains(value) | ||
} | ||
|
||
func (ts *tsafeSet) Equals(other Set) bool { | ||
ts.m.RLock() | ||
defer ts.m.RUnlock() | ||
return ts.us.Equals(other) | ||
} | ||
|
||
func (ts *tsafeSet) Length() int { | ||
ts.m.RLock() | ||
defer ts.m.RUnlock() | ||
return ts.us.Length() | ||
} | ||
|
||
func (ts *tsafeSet) Values() (values []string) { | ||
ts.m.RLock() | ||
defer ts.m.RUnlock() | ||
return ts.us.Values() | ||
} | ||
|
||
func (ts *tsafeSet) Copy() Set { | ||
ts.m.RLock() | ||
defer ts.m.RUnlock() | ||
usResult := ts.us.Copy().(*unsafeSet) | ||
return &tsafeSet{usResult, sync.RWMutex{}} | ||
} | ||
|
||
func (ts *tsafeSet) Sub(other Set) Set { | ||
ts.m.RLock() | ||
defer ts.m.RUnlock() | ||
usResult := ts.us.Sub(other).(*unsafeSet) | ||
return &tsafeSet{usResult, sync.RWMutex{}} | ||
} |
Oops, something went wrong.