A little optimisation of newPath() func.

This commit is contained in:
Alexander Andreev 2023-09-05 05:58:29 +04:00
parent 92692454da
commit cc2cd72df8
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -9,9 +9,14 @@ import (
type path []string type path []string
// newPath splits a path and ensures that it starts with a slash (/) and doesn't // newPath ensures that a path provided is correct and splits it.
// have more than 1 catch-all parameter.
func newPath(path string) (path, error) { func newPath(path string) (path, error) {
pathLen := len(path)
if pathLen == 0 {
return nil, errors.New("empty path is not allowed")
}
if path[0] != '/' { if path[0] != '/' {
return nil, errors.New("path should start with a slash symbol \"/\"") return nil, errors.New("path should start with a slash symbol \"/\"")
} }
@ -20,6 +25,17 @@ func newPath(path string) (path, error) {
return nil, errors.New("path can have only one catch-all parameter \"*\"") return nil, errors.New("path can have only one catch-all parameter \"*\"")
} }
if path[pathLen-1] == '/' {
path = path[:pathLen-1]
}
parts := strings.Split(path, "/")
parts[0] = "/"
return parts, nil
}
path = strings.ReplaceAll(path, "//", "/") path = strings.ReplaceAll(path, "//", "/")
parts := strings.Split(strings.TrimSuffix(path, "/"), "/") parts := strings.Split(strings.TrimSuffix(path, "/"), "/")