chore: Use slices package where possible (#6585)

* chore: Use slices package where possible

* More, mostly using ContainsFunc

* Even more slice operations
This commit is contained in:
Francis Lavoie 2024-09-25 16:30:56 -04:00 committed by GitHub
parent 9dda8fbf84
commit 2faeac0a10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 142 additions and 268 deletions

View file

@ -18,6 +18,7 @@ import (
"fmt"
"net/http"
"regexp"
"slices"
"strings"
"github.com/caddyserver/caddy/v2"
@ -126,7 +127,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt
// defer work until a variable is actually evaluated by using replacer's Map callback
repl.Map(func(key string) (any, bool) {
// return early if the variable is not even a configured destination
destIdx := h.destinationIndex(key)
destIdx := slices.Index(h.Destinations, key)
if destIdx < 0 {
return nil, false
}
@ -170,17 +171,6 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt
return next.ServeHTTP(w, r)
}
// destinationIndex returns the positional index of the destination
// is name is a known destination; otherwise it returns -1.
func (h Handler) destinationIndex(name string) int {
for i, dest := range h.Destinations {
if dest == name {
return i
}
}
return -1
}
// Mapping describes a mapping from input to outputs.
type Mapping struct {
// The input value to match. Must be distinct from other mappings.