diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-09-15 17:00:08 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-09-15 17:00:08 -0600 |
commit | 0ab036d34c5526e8c39da798bec2bc5d4a70a6b2 (patch) | |
tree | e678a16418f61ee433ecf41708c85ad4af1fb2f2 | |
parent | 73f18bcd3a754e8a59100dd165d048a749aab1fe (diff) |
allow slash-ending paths in pathTree
-rw-r--r-- | internal/pathtree.go | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/internal/pathtree.go b/internal/pathtree.go index 25d000d..0ab6b81 100644 --- a/internal/pathtree.go +++ b/internal/pathtree.go @@ -197,39 +197,39 @@ func (st subtree[V]) Match(segments []string, m *match[V]) *match[V] { } func (st *subtree[V]) Add(pattern []string, value V) { - if len(pattern[0]) == 0 { - panic("invalid pattern") - } - - switch pattern[0][0] { - case '*': - if len(pattern) > 1 { - panic("invalid pattern: segments after *remainder") - } - if st.remainder != nil { - panic("pattern already exists") + if len(pattern[0]) > 0 { + switch pattern[0][0] { + case '*': + if len(pattern) > 1 { + panic("invalid pattern: segments after *remainder") + } + if st.remainder != nil { + panic("pattern already exists") + } + + st.remainder = &remainderNode[V]{ + param: pattern[0][1:], + value: value, + } + return + case ':': + child := st.wildcards.Find(pattern[0][1:]) + if child != nil { + child.Add(pattern[1:], value) + } else { + st.wildcards = append(st.wildcards, makeWildcard(pattern, value)) + sort.Sort(st.wildcards) + } + return } + } - st.remainder = &remainderNode[V]{ - param: pattern[0][1:], - value: value, - } - case ':': - child := st.wildcards.Find(pattern[0][1:]) - if child != nil { - child.Add(pattern[1:], value) - } else { - st.wildcards = append(st.wildcards, makeWildcard(pattern, value)) - sort.Sort(st.wildcards) - } - default: - child := st.segments.Find(pattern[0]) - if child != nil { - child.Add(pattern[1:], value) - } else { - st.segments = append(st.segments, makeSegment(pattern, value)) - sort.Sort(st.segments) - } + child := st.segments.Find(pattern[0]) + if child != nil { + child.Add(pattern[1:], value) + } else { + st.segments = append(st.segments, makeSegment(pattern, value)) + sort.Sort(st.segments) } } |