Skip to content

Commit 2c6e08b

Browse files
committed
feat(serverHandler): add pathInts type
1 parent 0210ca4 commit 2c6e08b

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/serverHandler/pathValues.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,54 @@ package serverHandler
44

55
type prefixFilter func(whole, prefix string) bool
66

7+
// pathInts
8+
9+
type pathInts struct {
10+
path string
11+
ints []int
12+
}
13+
14+
type pathIntsList []pathInts
15+
16+
func (list pathIntsList) mergePrefixMatched(mergeWith []int, matchPrefix prefixFilter, refPath string) []int {
17+
var result []int
18+
if mergeWith != nil {
19+
result = make([]int, len(mergeWith))
20+
copy(result, mergeWith)
21+
}
22+
23+
for i := range list {
24+
if matchPrefix(refPath, list[i].path) {
25+
if result == nil {
26+
result = []int{}
27+
}
28+
result = append(result, list[i].ints...)
29+
}
30+
}
31+
32+
if mergeWith != nil && len(mergeWith) == len(result) {
33+
return mergeWith
34+
} else {
35+
return result
36+
}
37+
}
38+
39+
func (list pathIntsList) filterSuccessor(matchPrefix prefixFilter, refPath string) pathIntsList {
40+
var result pathIntsList
41+
42+
for i := range list {
43+
if len(list[i].path) > len(refPath) && matchPrefix(list[i].path, refPath) {
44+
result = append(result, list[i])
45+
}
46+
}
47+
48+
if len(list) == len(result) {
49+
return list
50+
} else {
51+
return result
52+
}
53+
}
54+
755
// pathStrings
856

957
type pathStrings struct {

src/serverHandler/pathValues_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ import (
55
"testing"
66
)
77

8+
func TestPathInts(t *testing.T) {
9+
ps := pathIntsList{
10+
{"/a", []int{1}},
11+
{"/a/b", []int{2}},
12+
{"/a/b/c", []int{3}},
13+
{"/foo/bar", []int{99}},
14+
}
15+
16+
mergeWith := []int{199, 299}
17+
merged := ps.mergePrefixMatched(mergeWith, util.HasUrlPrefixDir, "/a/b")
18+
if len(mergeWith) != 2 {
19+
t.Error()
20+
}
21+
if len(merged) != 4 || merged[2] != 1 || merged[3] != 2 {
22+
t.Error(merged)
23+
}
24+
25+
merged = ps.mergePrefixMatched(nil, util.HasUrlPrefixDir, "/lorem/ipsum")
26+
if merged != nil {
27+
t.Error(merged)
28+
}
29+
30+
successors := ps.filterSuccessor(util.HasUrlPrefixDir, "/a/b")
31+
if len(successors) != 1 || successors[0].path != "/a/b/c" {
32+
t.Error(successors)
33+
}
34+
}
35+
836
func TestPathStrings(t *testing.T) {
937
ps := pathStringsList{
1038
{"/a", []string{"a"}},

0 commit comments

Comments
 (0)