Skip to content

Commit

Permalink
Merge pull request #497 from danielgtaylor/go-1.20-slices-fix
Browse files Browse the repository at this point in the history
fix: remove slices dependency to better support Go 1.20
  • Loading branch information
danielgtaylor authored Jul 10, 2024
2 parents 6d80377 + ca97cdc commit b8be41a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
21 changes: 18 additions & 3 deletions adapters/humaflow/flow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,25 @@ import (
"net/http"
"net/url"
"regexp"
"slices"
"strings"
)

// slicesIndex returns the index of the first occurrence of v in s,
// or -1 if not present.
func slicesIndex[E comparable](s []E, v E) int {
for i := range s {
if v == s[i] {
return i
}
}
return -1
}

// slicesContains reports whether v is present in s.
func slicesContains[E comparable](s []E, v E) bool {
return slicesIndex(s, v) >= 0
}

// AllMethods is a slice containing all HTTP request methods.
var AllMethods = []string{http.MethodGet, http.MethodHead, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodConnect, http.MethodOptions, http.MethodTrace}

Expand Down Expand Up @@ -113,7 +128,7 @@ func New() *Mux {
// Handle registers a new handler for the given request path pattern and HTTP
// methods.
func (m *Mux) Handle(pattern string, handler http.Handler, methods ...string) {
if slices.Contains(methods, http.MethodGet) && !slices.Contains(methods, http.MethodHead) {
if slicesContains(methods, http.MethodGet) && !slicesContains(methods, http.MethodHead) {
methods = append(methods, http.MethodHead)
}

Expand Down Expand Up @@ -176,7 +191,7 @@ func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
route.handler.ServeHTTP(w, r.WithContext(ctx))
return
}
if !slices.Contains(allowedMethods, route.method) {
if !slicesContains(allowedMethods, route.method) {
allowedMethods = append(allowedMethods, route.method)
}
}
Expand Down
18 changes: 10 additions & 8 deletions formdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"mime/multipart"
"net/http"
"reflect"
"slices"
"strings"
)

Expand Down Expand Up @@ -55,19 +54,22 @@ func (v MimeTypeValidator) Validate(fh *multipart.FileHeader, location string) (
file.Seek(int64(0), io.SeekStart)
mimeType = http.DetectContentType(buffer)
}
accept := slices.ContainsFunc(v.accept, func(m string) bool {
accept := false
for _, m := range v.accept {
if m == "text/plain" || m == "application/octet-stream" {
return true
accept = true
break
}
if strings.HasSuffix(m, "/*") &&
strings.HasPrefix(mimeType, strings.TrimRight(m, "*")) {
return true
accept = true
break
}
if mimeType == m {
return true
accept = true
break
}
return false
})
}

if accept {
return mimeType, nil
Expand Down Expand Up @@ -178,7 +180,7 @@ func (m *MultipartFormFiles[T]) Decode(opMediaType *MediaType) []error {
case field.Type() == reflect.TypeOf([]FormFile{}):
files, errs := m.readMultipleFiles(key, opMediaType)
if errs != nil {
errors = slices.Concat(errors, errs)
errors = append(errors, errs...)
continue
}
field.Set(reflect.ValueOf(files))
Expand Down

0 comments on commit b8be41a

Please sign in to comment.