From 587ac99d26a026c4441ce224f52105d2dbd3751b Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Sun, 19 Apr 2026 00:53:33 +0200 Subject: run `go fix ./...` --- iterator.go | 2 +- object.go | 15 +++------------ request.go | 26 ++++++++++---------------- tests/object_iterator_test.go | 7 +++---- util/render_template.go | 4 ++-- 5 files changed, 19 insertions(+), 35 deletions(-) diff --git a/iterator.go b/iterator.go index ba5e6f6..3b26a1d 100644 --- a/iterator.go +++ b/iterator.go @@ -144,7 +144,7 @@ func (b *iteratorBase) nextPage(ctx context.Context, limit int) ([]string, error return result, b.i.putHeader(resp.Header) } -func (b *iteratorBase) nextPageDetailed(ctx context.Context, limit int, data interface{}) error { +func (b *iteratorBase) nextPageDetailed(ctx context.Context, limit int, data any) error { if b.eof { return nil } diff --git a/object.go b/object.go index 28c33da..223c1a3 100644 --- a/object.go +++ b/object.go @@ -32,6 +32,7 @@ import ( "io" "net/http" "net/url" + "slices" "strings" "time" ) @@ -617,16 +618,6 @@ func (o *Object) URL() (string, error) { }.URL(o.c.a.backend, nil) } -// Returns true if string is contained in slice -func contains(s []string, e string) bool { - for _, a := range s { - if a == e { - return true - } - } - return false -} - // TempURL is like Object.URL, but includes a token with a limited lifetime (as // specified by the `expires` argument) that permits anonymous access to this // object using the given HTTP method. This works only when the tempurl @@ -671,9 +662,9 @@ func (o *Object) TempURL(ctx context.Context, key, method string, expires time.T var mac hash.Hash switch { - case contains(allowedDigest, "sha256"): + case slices.Contains(allowedDigest, "sha256"): mac = hmac.New(sha256.New, []byte(key)) - case contains(allowedDigest, "sha1"): + case slices.Contains(allowedDigest, "sha1"): mac = hmac.New(sha1.New, []byte(key)) default: return "", fmt.Errorf("schwift supports sha1 and sha256 digests but the Swift server only supports: %s", strings.Join(allowedDigest, ", ")) diff --git a/request.go b/request.go index ce9e8ac..52b457d 100644 --- a/request.go +++ b/request.go @@ -21,8 +21,10 @@ package schwift import ( "context" "io" + "maps" "net/http" "net/url" + "slices" "strings" ) @@ -48,16 +50,10 @@ func cloneRequestOptions(orig *RequestOptions, additional Headers) *RequestOptio Values: make(url.Values), } if orig != nil { - for k, v := range orig.Headers { - result.Headers[k] = v - } - for k, v := range orig.Values { - result.Values[k] = v - } - } - for k, v := range additional { - result.Headers[k] = v + maps.Copy(result.Headers, orig.Headers) + maps.Copy(result.Values, orig.Values) } + maps.Copy(result.Headers, additional) return &result } @@ -140,14 +136,12 @@ func (r Request) Do(ctx context.Context, backend Backend) (*http.Response, error // check disabled -> return response unaltered return resp, nil } - for _, code := range r.ExpectStatusCodes { - if code == resp.StatusCode { - var err error - if r.DrainResponseBody || resp.StatusCode == http.StatusNoContent { - err = drainResponseBody(resp) - } - return resp, err + if slices.Contains(r.ExpectStatusCodes, resp.StatusCode) { + var err error + if r.DrainResponseBody || resp.StatusCode == http.StatusNoContent { + err = drainResponseBody(resp) } + return resp, err } // unexpected status code -> generate error diff --git a/tests/object_iterator_test.go b/tests/object_iterator_test.go index ac10dbc..592d896 100644 --- a/tests/object_iterator_test.go +++ b/tests/object_iterator_test.go @@ -246,8 +246,7 @@ func expectObjectInfos(t *testing.T, actualInfos []schwift.ObjectInfo, expectedN } for idx, info := range actualInfos { // case 1: pseudo-directory - if strings.HasPrefix(expectedNames[idx], "subdir:") { - expectedSubdir := strings.TrimPrefix(expectedNames[idx], "subdir:") + if expectedSubdir, ok := strings.CutPrefix(expectedNames[idx], "subdir:"); ok { if expectedSubdir != info.SubDirectory { t.Errorf("expected objects[%d] subdir = %q, got %q", idx, expectedSubdir, info.SubDirectory) @@ -264,8 +263,8 @@ func expectObjectInfos(t *testing.T, actualInfos []schwift.ObjectInfo, expectedN } // case 2: symlink - if strings.HasPrefix(expectedNames[idx], "symlink:") { - fields := strings.SplitN(strings.TrimPrefix(expectedNames[idx], "symlink:"), ">", 2) + if after, ok := strings.CutPrefix(expectedNames[idx], "symlink:"); ok { + fields := strings.SplitN(after, ">", 2) expectedName, expectedTargetName := fields[0], fields[1] if info.Object == nil { diff --git a/util/render_template.go b/util/render_template.go index 03a4628..64ef939 100755 --- a/util/render_template.go +++ b/util/render_template.go @@ -39,7 +39,7 @@ func main() { } dataStr, templateStr := sections[0], sections[1] - data := make(map[string]interface{}) + data := make(map[string]any) failIfError(json.Unmarshal([]byte(dataStr), &data)) tmpl, err := template.New("tmpl").Parse(templateStr) @@ -54,7 +54,7 @@ func failIfError(err error) { } } -func fail(msg string, args ...interface{}) { +func fail(msg string, args ...any) { if len(args) > 0 { msg = fmt.Sprintf(msg, args...) } -- cgit v1.2.3