Skip to content

Commit

Permalink
Introduce MatchAndExpandPlaceholders, allowing urlpath usage to be hi…
Browse files Browse the repository at this point in the history
…dden from callers
  • Loading branch information
Justin Johnson committed Aug 12, 2022
1 parent 37f4710 commit de15213
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions redirects.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,41 @@ func (r *Rule) IsProxy() bool {
return u.Host != ""
}

// MatchAndExpandPlaceholders expands placeholders in `r.To` and returns true if the provided path matches.
// Otherwise it returns false.
func (r *Rule) MatchAndExpandPlaceholders(urlPath string) bool {
// get rule.From, trim trailing slash, ...
fromPath := urlpath.New(strings.TrimSuffix(r.From, "/"))
match, ok := fromPath.Match(urlPath)

if !ok {
return false
}

// We have a match! Perform substitution and return the updated rule
toPath := r.To
toPath = replacePlaceholders(toPath, match)
toPath = replaceSplat(toPath, match)

r.To = toPath

return true
}

func replacePlaceholders(to string, match urlpath.Match) string {
if len(match.Params) > 0 {
for key, value := range match.Params {
to = strings.ReplaceAll(to, ":"+key, value)
}
}

return to
}

func replaceSplat(to string, match urlpath.Match) string {
return strings.ReplaceAll(to, ":splat", match.Trailing)
}

// Must parse utility.
func Must(v []Rule, err error) []Rule {
if err != nil {
Expand Down Expand Up @@ -125,19 +160,3 @@ func parseStatus(s string) (code int, err error) {
code, err = strconv.Atoi(s)
return code, err
}

// ReplacePlaceholders replaces all placeholders from the match in the provided string.
func ReplacePlaceholders(to string, match urlpath.Match) string {
if len(match.Params) > 0 {
for key, value := range match.Params {
to = strings.ReplaceAll(to, ":"+key, value)
}
}

return to
}

// ReplaceSplat replaces all splats from the match in the provided string.
func ReplaceSplat(to string, match urlpath.Match) string {
return strings.ReplaceAll(to, ":splat", match.Trailing)
}

0 comments on commit de15213

Please sign in to comment.