Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove slices dependency to better support Go 1.20 #497

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading