Skip to content

Commit

Permalink
Use slices package from the stdlib when possible (#5360)
Browse files Browse the repository at this point in the history
* Use slices from the stdlib when possible

* Add some unit tests

* More small tweaks + add benchmark func
  • Loading branch information
its-josh4 authored Oct 29, 2024
1 parent 093de3b commit c6bcdd8
Show file tree
Hide file tree
Showing 38 changed files with 200 additions and 110 deletions.
8 changes: 4 additions & 4 deletions internal/api/resolver_query_find_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package api

import (
"context"
"slices"
"strconv"

"github.com/99designs/gqlgen/graphql"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/sliceutil"
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
)

Expand Down Expand Up @@ -95,11 +95,11 @@ func (r *queryResolver) FindImages(
result, err = qb.Query(ctx, models.ImageQueryOptions{
QueryOptions: models.QueryOptions{
FindFilter: filter,
Count: sliceutil.Contains(fields, "count"),
Count: slices.Contains(fields, "count"),
},
ImageFilter: imageFilter,
Megapixels: sliceutil.Contains(fields, "megapixels"),
TotalSize: sliceutil.Contains(fields, "filesize"),
Megapixels: slices.Contains(fields, "megapixels"),
TotalSize: slices.Contains(fields, "filesize"),
})
if err == nil {
images, err = result.Resolve(ctx)
Expand Down
14 changes: 7 additions & 7 deletions internal/api/resolver_query_find_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package api

import (
"context"
"slices"
"strconv"

"github.com/99designs/gqlgen/graphql"

"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/scene"
"github.com/stashapp/stash/pkg/sliceutil"
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
)

Expand Down Expand Up @@ -119,11 +119,11 @@ func (r *queryResolver) FindScenes(
result, err = r.repository.Scene.Query(ctx, models.SceneQueryOptions{
QueryOptions: models.QueryOptions{
FindFilter: filter,
Count: sliceutil.Contains(fields, "count"),
Count: slices.Contains(fields, "count"),
},
SceneFilter: sceneFilter,
TotalDuration: sliceutil.Contains(fields, "duration"),
TotalSize: sliceutil.Contains(fields, "filesize"),
TotalDuration: slices.Contains(fields, "duration"),
TotalSize: slices.Contains(fields, "filesize"),
})
if err == nil {
scenes, err = result.Resolve(ctx)
Expand Down Expand Up @@ -174,11 +174,11 @@ func (r *queryResolver) FindScenesByPathRegex(ctx context.Context, filter *model
result, err := r.repository.Scene.Query(ctx, models.SceneQueryOptions{
QueryOptions: models.QueryOptions{
FindFilter: queryFilter,
Count: sliceutil.Contains(fields, "count"),
Count: slices.Contains(fields, "count"),
},
SceneFilter: sceneFilter,
TotalDuration: sliceutil.Contains(fields, "duration"),
TotalSize: sliceutil.Contains(fields, "filesize"),
TotalDuration: slices.Contains(fields, "duration"),
TotalSize: slices.Contains(fields, "filesize"),
})
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions internal/api/resolver_query_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"errors"
"fmt"
"slices"
"sort"
"strings"

"github.com/99designs/gqlgen/graphql"
"github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/pkg"
"github.com/stashapp/stash/pkg/sliceutil"
)

var ErrInvalidPackageType = errors.New("invalid package type")
Expand Down Expand Up @@ -166,7 +166,7 @@ func (r *queryResolver) InstalledPackages(ctx context.Context, typeArg PackageTy

var ret []*Package

if sliceutil.Contains(graphql.CollectAllFields(ctx), "source_package") {
if slices.Contains(graphql.CollectAllFields(ctx), "source_package") {
ret, err = r.getInstalledPackagesWithUpgrades(ctx, pm)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions internal/autotag/gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package autotag

import (
"context"
"slices"

"github.com/stashapp/stash/pkg/gallery"
"github.com/stashapp/stash/pkg/match"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/sliceutil"
)

type GalleryFinderUpdater interface {
Expand Down Expand Up @@ -53,7 +53,7 @@ func GalleryPerformers(ctx context.Context, s *models.Gallery, rw GalleryPerform
}
existing := s.PerformerIDs.List()

if sliceutil.Contains(existing, otherID) {
if slices.Contains(existing, otherID) {
return false, nil
}

Expand Down Expand Up @@ -91,7 +91,7 @@ func GalleryTags(ctx context.Context, s *models.Gallery, rw GalleryTagUpdater, t
}
existing := s.TagIDs.List()

if sliceutil.Contains(existing, otherID) {
if slices.Contains(existing, otherID) {
return false, nil
}

Expand Down
6 changes: 3 additions & 3 deletions internal/autotag/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package autotag

import (
"context"
"slices"

"github.com/stashapp/stash/pkg/image"
"github.com/stashapp/stash/pkg/match"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/sliceutil"
)

type ImageFinderUpdater interface {
Expand Down Expand Up @@ -44,7 +44,7 @@ func ImagePerformers(ctx context.Context, s *models.Image, rw ImagePerformerUpda
}
existing := s.PerformerIDs.List()

if sliceutil.Contains(existing, otherID) {
if slices.Contains(existing, otherID) {
return false, nil
}

Expand Down Expand Up @@ -82,7 +82,7 @@ func ImageTags(ctx context.Context, s *models.Image, rw ImageTagUpdater, tagRead
}
existing := s.TagIDs.List()

if sliceutil.Contains(existing, otherID) {
if slices.Contains(existing, otherID) {
return false, nil
}

Expand Down
8 changes: 4 additions & 4 deletions internal/autotag/performer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package autotag

import (
"context"
"slices"

"github.com/stashapp/stash/pkg/gallery"
"github.com/stashapp/stash/pkg/image"
"github.com/stashapp/stash/pkg/match"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/scene"
"github.com/stashapp/stash/pkg/sliceutil"
"github.com/stashapp/stash/pkg/txn"
)

Expand Down Expand Up @@ -63,7 +63,7 @@ func (tagger *Tagger) PerformerScenes(ctx context.Context, p *models.Performer,
}
existing := o.PerformerIDs.List()

if sliceutil.Contains(existing, p.ID) {
if slices.Contains(existing, p.ID) {
return false, nil
}

Expand Down Expand Up @@ -92,7 +92,7 @@ func (tagger *Tagger) PerformerImages(ctx context.Context, p *models.Performer,
}
existing := o.PerformerIDs.List()

if sliceutil.Contains(existing, p.ID) {
if slices.Contains(existing, p.ID) {
return false, nil
}

Expand Down Expand Up @@ -121,7 +121,7 @@ func (tagger *Tagger) PerformerGalleries(ctx context.Context, p *models.Performe
}
existing := o.PerformerIDs.List()

if sliceutil.Contains(existing, p.ID) {
if slices.Contains(existing, p.ID) {
return false, nil
}

Expand Down
6 changes: 3 additions & 3 deletions internal/autotag/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package autotag

import (
"context"
"slices"

"github.com/stashapp/stash/pkg/match"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/scene"
"github.com/stashapp/stash/pkg/sliceutil"
)

type SceneFinderUpdater interface {
Expand Down Expand Up @@ -44,7 +44,7 @@ func ScenePerformers(ctx context.Context, s *models.Scene, rw ScenePerformerUpda
}
existing := s.PerformerIDs.List()

if sliceutil.Contains(existing, otherID) {
if slices.Contains(existing, otherID) {
return false, nil
}

Expand Down Expand Up @@ -82,7 +82,7 @@ func SceneTags(ctx context.Context, s *models.Scene, rw SceneTagUpdater, tagRead
}
existing := s.TagIDs.List()

if sliceutil.Contains(existing, otherID) {
if slices.Contains(existing, otherID) {
return false, nil
}

Expand Down
8 changes: 4 additions & 4 deletions internal/autotag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package autotag

import (
"context"
"slices"

"github.com/stashapp/stash/pkg/gallery"
"github.com/stashapp/stash/pkg/image"
"github.com/stashapp/stash/pkg/match"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/scene"
"github.com/stashapp/stash/pkg/sliceutil"
"github.com/stashapp/stash/pkg/txn"
)

Expand Down Expand Up @@ -61,7 +61,7 @@ func (tagger *Tagger) TagScenes(ctx context.Context, p *models.Tag, paths []stri
}
existing := o.TagIDs.List()

if sliceutil.Contains(existing, p.ID) {
if slices.Contains(existing, p.ID) {
return false, nil
}

Expand Down Expand Up @@ -90,7 +90,7 @@ func (tagger *Tagger) TagImages(ctx context.Context, p *models.Tag, paths []stri
}
existing := o.TagIDs.List()

if sliceutil.Contains(existing, p.ID) {
if slices.Contains(existing, p.ID) {
return false, nil
}

Expand Down Expand Up @@ -119,7 +119,7 @@ func (tagger *Tagger) TagGalleries(ctx context.Context, p *models.Tag, paths []s
}
existing := o.TagIDs.List()

if sliceutil.Contains(existing, p.ID) {
if slices.Contains(existing, p.ID) {
return false, nil
}

Expand Down
4 changes: 2 additions & 2 deletions internal/dlna/cds.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"os"
"path"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
Expand All @@ -40,7 +41,6 @@ import (
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/scene"
"github.com/stashapp/stash/pkg/sliceutil"
)

var pageSize = 100
Expand Down Expand Up @@ -521,7 +521,7 @@ func (me *contentDirectoryService) getPageVideos(sceneFilter *models.SceneFilter
}

func getPageFromID(paths []string) *int {
i := sliceutil.Index(paths, "page")
i := slices.Index(paths, "page")
if i == -1 || i+1 >= len(paths) {
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions internal/dlna/whitelist.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package dlna

import (
"slices"
"sync"
"time"

"github.com/stashapp/stash/pkg/sliceutil"
)

// only keep the 10 most recent IP addresses
Expand All @@ -30,7 +29,7 @@ func (m *ipWhitelistManager) addRecent(addr string) bool {
m.mutex.Lock()
defer m.mutex.Unlock()

i := sliceutil.Index(m.recentIPAddresses, addr)
i := slices.Index(m.recentIPAddresses, addr)
if i != -1 {
if i == 0 {
// don't do anything if it's already at the start
Expand Down
3 changes: 2 additions & 1 deletion internal/identify/identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"strconv"

"github.com/stashapp/stash/pkg/logger"
Expand Down Expand Up @@ -333,7 +334,7 @@ func (t *SceneIdentifier) addTagToScene(ctx context.Context, s *models.Scene, ta
}
existing := s.TagIDs.List()

if sliceutil.Contains(existing, tagID) {
if slices.Contains(existing, tagID) {
// skip if the scene was already tagged
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/identify/identify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"
"errors"
"reflect"
"slices"
"strconv"
"testing"

"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/models/mocks"
"github.com/stashapp/stash/pkg/scraper"
"github.com/stashapp/stash/pkg/sliceutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand All @@ -23,7 +23,7 @@ type mockSceneScraper struct {
}

func (s mockSceneScraper) ScrapeScenes(ctx context.Context, sceneID int) ([]*scraper.ScrapedScene, error) {
if sliceutil.Contains(s.errIDs, sceneID) {
if slices.Contains(s.errIDs, sceneID) {
return nil, errors.New("scrape scene error")
}
return s.results[sceneID], nil
Expand Down
5 changes: 3 additions & 2 deletions pkg/gallery/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gallery
import (
"context"
"fmt"
"slices"
"strings"

"github.com/stashapp/stash/pkg/models"
Expand Down Expand Up @@ -153,7 +154,7 @@ func (i *Importer) populatePerformers(ctx context.Context) error {
}

missingPerformers := sliceutil.Filter(names, func(name string) bool {
return !sliceutil.Contains(pluckedNames, name)
return !slices.Contains(pluckedNames, name)
})

if len(missingPerformers) > 0 {
Expand Down Expand Up @@ -212,7 +213,7 @@ func (i *Importer) populateTags(ctx context.Context) error {
}

missingTags := sliceutil.Filter(names, func(name string) bool {
return !sliceutil.Contains(pluckedNames, name)
return !slices.Contains(pluckedNames, name)
})

if len(missingTags) > 0 {
Expand Down
Loading

0 comments on commit c6bcdd8

Please sign in to comment.