Skip to content

Commit

Permalink
Genericise sliceutil functions (#4253)
Browse files Browse the repository at this point in the history
* Genericise sliceutil.SliceSame
* Genericise intslice functions
* Genericise stringutil functions
  • Loading branch information
DingDongSoLong4 authored Nov 1, 2023
1 parent cc6673f commit 9621213
Show file tree
Hide file tree
Showing 51 changed files with 256 additions and 406 deletions.
4 changes: 2 additions & 2 deletions internal/api/resolver_mutation_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stashapp/stash/pkg/image"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/plugin"
"github.com/stashapp/stash/pkg/sliceutil/intslice"
"github.com/stashapp/stash/pkg/sliceutil"
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
"github.com/stashapp/stash/pkg/utils"
)
Expand Down Expand Up @@ -256,7 +256,7 @@ func (r *mutationResolver) BulkImageUpdate(ctx context.Context, input BulkImageU
}

thisUpdatedGalleryIDs := updatedImage.GalleryIDs.ImpactedIDs(i.GalleryIDs.List())
updatedGalleryIDs = intslice.IntAppendUniques(updatedGalleryIDs, thisUpdatedGalleryIDs)
updatedGalleryIDs = sliceutil.AppendUniques(updatedGalleryIDs, thisUpdatedGalleryIDs)
}

image, err := qb.UpdatePartial(ctx, imageID, updatedImage)
Expand Down
4 changes: 2 additions & 2 deletions internal/api/resolver_mutation_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stashapp/stash/internal/manager/config"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/plugin"
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
"github.com/stashapp/stash/pkg/sliceutil"
)

func (r *mutationResolver) RunPluginTask(ctx context.Context, pluginID string, taskName string, args []*plugin.PluginArgInput) (string, error) {
Expand Down Expand Up @@ -41,7 +41,7 @@ func (r *mutationResolver) SetPluginsEnabled(ctx context.Context, enabledMap map
// add plugins that are newly disabled
for pluginID, enabled := range enabledMap {
if !enabled {
newDisabled = stringslice.StrAppendUnique(newDisabled, pluginID)
newDisabled = sliceutil.AppendUnique(newDisabled, pluginID)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/api/resolver_mutation_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/plugin"
"github.com/stashapp/stash/pkg/scene"
"github.com/stashapp/stash/pkg/sliceutil/intslice"
"github.com/stashapp/stash/pkg/sliceutil"
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
"github.com/stashapp/stash/pkg/utils"
)
Expand Down Expand Up @@ -627,7 +627,7 @@ func (r *mutationResolver) SceneMarkerCreate(ctx context.Context, input SceneMar

// Save the marker tags
// If this tag is the primary tag, then let's not add it.
tagIDs = intslice.IntExclude(tagIDs, []int{newMarker.PrimaryTagID})
tagIDs = sliceutil.Exclude(tagIDs, []int{newMarker.PrimaryTagID})
return qb.UpdateTags(ctx, newMarker.ID, tagIDs)
}); err != nil {
return nil, err
Expand Down Expand Up @@ -716,7 +716,7 @@ func (r *mutationResolver) SceneMarkerUpdate(ctx context.Context, input SceneMar
if tagIdsIncluded {
// Save the marker tags
// If this tag is the primary tag, then let's not add it.
tagIDs = intslice.IntExclude(tagIDs, []int{newMarker.PrimaryTagID})
tagIDs = sliceutil.Exclude(tagIDs, []int{newMarker.PrimaryTagID})
if err := qb.UpdateTags(ctx, markerID, tagIDs); err != nil {
return err
}
Expand Down
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 @@ -6,7 +6,7 @@ import (

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

func (r *queryResolver) FindImage(ctx context.Context, id *string, checksum *string) (*models.Image, error) {
Expand Down Expand Up @@ -55,11 +55,11 @@ func (r *queryResolver) FindImages(ctx context.Context, imageFilter *models.Imag
result, err := qb.Query(ctx, models.ImageQueryOptions{
QueryOptions: models.QueryOptions{
FindFilter: filter,
Count: stringslice.StrInclude(fields, "count"),
Count: sliceutil.Contains(fields, "count"),
},
ImageFilter: imageFilter,
Megapixels: stringslice.StrInclude(fields, "megapixels"),
TotalSize: stringslice.StrInclude(fields, "filesize"),
Megapixels: sliceutil.Contains(fields, "megapixels"),
TotalSize: sliceutil.Contains(fields, "filesize"),
})
if err != nil {
return err
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 @@ -8,7 +8,7 @@ import (

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

func (r *queryResolver) FindScene(ctx context.Context, id *string, checksum *string) (*models.Scene, error) {
Expand Down Expand Up @@ -105,11 +105,11 @@ func (r *queryResolver) FindScenes(ctx context.Context, sceneFilter *models.Scen
result, err = r.repository.Scene.Query(ctx, models.SceneQueryOptions{
QueryOptions: models.QueryOptions{
FindFilter: filter,
Count: stringslice.StrInclude(fields, "count"),
Count: sliceutil.Contains(fields, "count"),
},
SceneFilter: sceneFilter,
TotalDuration: stringslice.StrInclude(fields, "duration"),
TotalSize: stringslice.StrInclude(fields, "filesize"),
TotalDuration: sliceutil.Contains(fields, "duration"),
TotalSize: sliceutil.Contains(fields, "filesize"),
})
if err == nil {
scenes, err = result.Resolve(ctx)
Expand Down Expand Up @@ -160,11 +160,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: stringslice.StrInclude(fields, "count"),
Count: sliceutil.Contains(fields, "count"),
},
SceneFilter: sceneFilter,
TotalDuration: stringslice.StrInclude(fields, "duration"),
TotalSize: stringslice.StrInclude(fields, "filesize"),
TotalDuration: sliceutil.Contains(fields, "duration"),
TotalSize: sliceutil.Contains(fields, "filesize"),
})
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion internal/api/resolver_query_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/scraper"
"github.com/stashapp/stash/pkg/scraper/stashbox"
"github.com/stashapp/stash/pkg/sliceutil"
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
)

Expand Down Expand Up @@ -80,7 +81,7 @@ func filterSceneTags(scenes []*scraper.ScrapedScene) {
for _, reg := range excludeRegexps {
if reg.MatchString(strings.ToLower(t.Name)) {
ignore = true
ignoredTags = stringslice.StrAppendUnique(ignoredTags, t.Name)
ignoredTags = sliceutil.AppendUnique(ignoredTags, t.Name)
break
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/autotag/gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stashapp/stash/pkg/gallery"
"github.com/stashapp/stash/pkg/match"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/sliceutil/intslice"
"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 intslice.IntInclude(existing, otherID) {
if sliceutil.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 intslice.IntInclude(existing, otherID) {
if sliceutil.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 @@ -6,7 +6,7 @@ import (
"github.com/stashapp/stash/pkg/image"
"github.com/stashapp/stash/pkg/match"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/sliceutil/intslice"
"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 intslice.IntInclude(existing, otherID) {
if sliceutil.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 intslice.IntInclude(existing, otherID) {
if sliceutil.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 @@ -8,7 +8,7 @@ import (
"github.com/stashapp/stash/pkg/match"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/scene"
"github.com/stashapp/stash/pkg/sliceutil/intslice"
"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 intslice.IntInclude(existing, p.ID) {
if sliceutil.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 intslice.IntInclude(existing, p.ID) {
if sliceutil.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 intslice.IntInclude(existing, p.ID) {
if sliceutil.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 @@ -6,7 +6,7 @@ import (
"github.com/stashapp/stash/pkg/match"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/scene"
"github.com/stashapp/stash/pkg/sliceutil/intslice"
"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 intslice.IntInclude(existing, otherID) {
if sliceutil.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 intslice.IntInclude(existing, otherID) {
if sliceutil.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 @@ -8,7 +8,7 @@ import (
"github.com/stashapp/stash/pkg/match"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/scene"
"github.com/stashapp/stash/pkg/sliceutil/intslice"
"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 intslice.IntInclude(existing, p.ID) {
if sliceutil.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 intslice.IntInclude(existing, p.ID) {
if sliceutil.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 intslice.IntInclude(existing, p.ID) {
if sliceutil.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 @@ -40,7 +40,7 @@ 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/stringslice"
"github.com/stashapp/stash/pkg/sliceutil"
)

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

func getPageFromID(paths []string) *int {
i := stringslice.StrIndex(paths, "page")
i := sliceutil.Index(paths, "page")
if i == -1 || i+1 >= len(paths) {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/dlna/whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"sync"
"time"

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

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

i := stringslice.StrIndex(m.recentIPAddresses, addr)
i := sliceutil.Index(m.recentIPAddresses, addr)
if i != -1 {
if i == 0 {
// don't do anything if it's already at the start
Expand Down
2 changes: 1 addition & 1 deletion internal/identify/identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (t *SceneIdentifier) addTagToScene(ctx context.Context, s *models.Scene, ta
}
existing := s.TagIDs.List()

if sliceutil.Include(existing, tagID) {
if sliceutil.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 @@ -10,7 +10,7 @@ import (
"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/intslice"
"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 intslice.IntInclude(s.errIDs, sceneID) {
if sliceutil.Contains(s.errIDs, sceneID) {
return nil, errors.New("scrape scene error")
}
return s.results[sceneID], nil
Expand Down
5 changes: 2 additions & 3 deletions internal/identify/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/sliceutil"
"github.com/stashapp/stash/pkg/sliceutil/intslice"
"github.com/stashapp/stash/pkg/utils"
)

Expand Down Expand Up @@ -111,7 +110,7 @@ func (g sceneRelationships) performers(ctx context.Context, ignoreMale bool) ([]
}

if performerID != nil {
performerIDs = intslice.IntAppendUnique(performerIDs, *performerID)
performerIDs = sliceutil.AppendUnique(performerIDs, *performerID)
}
}

Expand Down Expand Up @@ -161,7 +160,7 @@ func (g sceneRelationships) tags(ctx context.Context) ([]int, error) {
return nil, fmt.Errorf("error converting tag ID %s: %w", *t.StoredID, err)
}

tagIDs = intslice.IntAppendUnique(tagIDs, int(tagID))
tagIDs = sliceutil.AppendUnique(tagIDs, int(tagID))
} else if createMissing {
newTag := models.NewTag()
newTag.Name = t.Name
Expand Down
Loading

0 comments on commit 9621213

Please sign in to comment.