Procházet zdrojové kódy

chore(yay): replace custom set package with dep (#2249)

* replace string set with dep

* remove unused field

* remove custom string set package
Jo před 1 rokem
rodič
revize
04c82b8112

+ 8 - 8
clean.go

@@ -7,13 +7,13 @@ import (
 	"path/filepath"
 
 	"github.com/Jguer/aur"
+	mapset "github.com/deckarep/golang-set/v2"
 	"github.com/leonelquinteros/gotext"
 
 	"github.com/Jguer/yay/v12/pkg/db"
 	"github.com/Jguer/yay/v12/pkg/settings"
 	"github.com/Jguer/yay/v12/pkg/settings/exe"
 	"github.com/Jguer/yay/v12/pkg/settings/parser"
-	"github.com/Jguer/yay/v12/pkg/stringset"
 	"github.com/Jguer/yay/v12/pkg/text"
 )
 
@@ -105,8 +105,8 @@ func cleanAUR(ctx context.Context, cfg *settings.Configuration,
 ) error {
 	cfg.Runtime.Logger.Println(gotext.Get("removing AUR packages from cache..."))
 
-	installedBases := make(stringset.StringSet)
-	inAURBases := make(stringset.StringSet)
+	installedBases := mapset.NewThreadUnsafeSet[string]()
+	inAURBases := mapset.NewThreadUnsafeSet[string]()
 
 	remotePackages := dbExecutor.InstalledRemotePackages()
 
@@ -138,15 +138,15 @@ func cleanAUR(ctx context.Context, cfg *settings.Configuration,
 		}
 
 		for i := range info {
-			inAURBases.Set(info[i].PackageBase)
+			inAURBases.Add(info[i].PackageBase)
 		}
 	}
 
 	for _, pkg := range remotePackages {
 		if pkg.Base() != "" {
-			installedBases.Set(pkg.Base())
+			installedBases.Add(pkg.Base())
 		} else {
-			installedBases.Set(pkg.Name())
+			installedBases.Add(pkg.Name())
 		}
 	}
 
@@ -156,11 +156,11 @@ func cleanAUR(ctx context.Context, cfg *settings.Configuration,
 		}
 
 		if !removeAll {
-			if keepInstalled && installedBases.Get(file.Name()) {
+			if keepInstalled && installedBases.Contains(file.Name()) {
 				continue
 			}
 
-			if keepCurrent && inAURBases.Get(file.Name()) {
+			if keepCurrent && inAURBases.Contains(file.Name()) {
 				continue
 			}
 		}

+ 1 - 1
pkg/dep/dep_graph.go

@@ -203,7 +203,7 @@ func (g *Grapher) pickSrcInfoPkgs(pkgs []*aurc.Pkg) ([]*aurc.Pkg, error) {
 	}
 
 	include, exclude, _, otherExclude := intrange.ParseNumberMenu(numberBuf)
-	isInclude := len(exclude) == 0 && len(otherExclude) == 0
+	isInclude := len(exclude) == 0 && otherExclude.Cardinality() == 0
 
 	for i := 1; i <= len(pkgs); i++ {
 		target := i - 1

+ 6 - 6
pkg/intrange/intrange.go

@@ -5,7 +5,7 @@ import (
 	"strings"
 	"unicode"
 
-	"github.com/Jguer/yay/v12/pkg/stringset"
+	mapset "github.com/deckarep/golang-set/v2"
 )
 
 // IntRange stores a max and min amount for range.
@@ -71,12 +71,12 @@ func Max(a, b int) int {
 // of course the implementation is up to the caller, this function mearley parses
 // the input and organizes it.
 func ParseNumberMenu(input string) (include, exclude IntRanges,
-	otherInclude, otherExclude stringset.StringSet,
+	otherInclude, otherExclude mapset.Set[string],
 ) {
 	include = make(IntRanges, 0)
 	exclude = make(IntRanges, 0)
-	otherInclude = make(stringset.StringSet)
-	otherExclude = make(stringset.StringSet)
+	otherInclude = mapset.NewThreadUnsafeSet[string]()
+	otherExclude = mapset.NewThreadUnsafeSet[string]()
 
 	words := strings.FieldsFunc(input, func(c rune) bool {
 		return unicode.IsSpace(c) || c == ','
@@ -102,14 +102,14 @@ func ParseNumberMenu(input string) (include, exclude IntRanges,
 
 		num1, err = strconv.Atoi(ranges[0])
 		if err != nil {
-			other.Set(strings.ToLower(word))
+			other.Add(strings.ToLower(word))
 			continue
 		}
 
 		if len(ranges) == 2 {
 			num2, err = strconv.Atoi(ranges[1])
 			if err != nil {
-				other.Set(strings.ToLower(word))
+				other.Add(strings.ToLower(word))
 				continue
 			}
 		} else {

+ 20 - 22
pkg/intrange/intrange_test.go

@@ -6,7 +6,8 @@ package intrange
 import (
 	"testing"
 
-	"github.com/Jguer/yay/v12/pkg/stringset"
+	mapset "github.com/deckarep/golang-set/v2"
+	"github.com/stretchr/testify/assert"
 )
 
 func TestParseNumberMenu(t *testing.T) {
@@ -14,8 +15,8 @@ func TestParseNumberMenu(t *testing.T) {
 	type result struct {
 		Include      IntRanges
 		Exclude      IntRanges
-		OtherInclude stringset.StringSet
-		OtherExclude stringset.StringSet
+		OtherInclude mapset.Set[string]
+		OtherExclude mapset.Set[string]
 	}
 
 	inputs := []string{
@@ -40,15 +41,15 @@ func TestParseNumberMenu(t *testing.T) {
 			makeIntRange(3, 3),
 			makeIntRange(4, 4),
 			makeIntRange(5, 5),
-		}, IntRanges{}, make(stringset.StringSet), make(stringset.StringSet)},
+		}, IntRanges{}, mapset.NewThreadUnsafeSet[string](), mapset.NewThreadUnsafeSet[string]()},
 		{IntRanges{
 			makeIntRange(1, 10),
 			makeIntRange(5, 15),
-		}, IntRanges{}, make(stringset.StringSet), make(stringset.StringSet)},
+		}, IntRanges{}, mapset.NewThreadUnsafeSet[string](), mapset.NewThreadUnsafeSet[string]()},
 		{IntRanges{
 			makeIntRange(5, 10),
 			makeIntRange(85, 90),
-		}, IntRanges{}, make(stringset.StringSet), make(stringset.StringSet)},
+		}, IntRanges{}, mapset.NewThreadUnsafeSet[string](), mapset.NewThreadUnsafeSet[string]()},
 		{
 			IntRanges{
 				makeIntRange(1, 1),
@@ -61,18 +62,18 @@ func TestParseNumberMenu(t *testing.T) {
 				makeIntRange(38, 40),
 				makeIntRange(123, 123),
 			},
-			make(stringset.StringSet), make(stringset.StringSet),
+			mapset.NewThreadUnsafeSet[string](), mapset.NewThreadUnsafeSet[string](),
 		},
-		{IntRanges{}, IntRanges{}, stringset.Make("abort", "all", "none"), make(stringset.StringSet)},
-		{IntRanges{}, IntRanges{}, stringset.Make("a-b"), stringset.Make("abort", "a-b")},
-		{IntRanges{}, IntRanges{}, stringset.Make("-9223372036854775809-9223372036854775809"), make(stringset.StringSet)},
+		{IntRanges{}, IntRanges{}, mapset.NewThreadUnsafeSet[string]("abort", "all", "none"), mapset.NewThreadUnsafeSet[string]()},
+		{IntRanges{}, IntRanges{}, mapset.NewThreadUnsafeSet[string]("a-b"), mapset.NewThreadUnsafeSet[string]("abort", "a-b")},
+		{IntRanges{}, IntRanges{}, mapset.NewThreadUnsafeSet[string]("-9223372036854775809-9223372036854775809"), mapset.NewThreadUnsafeSet[string]()},
 		{IntRanges{
 			makeIntRange(1, 1),
 			makeIntRange(2, 2),
 			makeIntRange(3, 3),
 			makeIntRange(4, 4),
 			makeIntRange(5, 5),
-		}, IntRanges{}, make(stringset.StringSet), make(stringset.StringSet)},
+		}, IntRanges{}, mapset.NewThreadUnsafeSet[string](), mapset.NewThreadUnsafeSet[string]()},
 		{IntRanges{
 			makeIntRange(1, 1),
 			makeIntRange(2, 2),
@@ -82,23 +83,20 @@ func TestParseNumberMenu(t *testing.T) {
 			makeIntRange(6, 6),
 			makeIntRange(7, 7),
 			makeIntRange(8, 8),
-		}, IntRanges{}, make(stringset.StringSet), make(stringset.StringSet)},
-		{IntRanges{}, IntRanges{}, make(stringset.StringSet), make(stringset.StringSet)},
-		{IntRanges{}, IntRanges{}, make(stringset.StringSet), make(stringset.StringSet)},
-		{IntRanges{}, IntRanges{}, stringset.Make("a", "b", "c", "d", "e"), make(stringset.StringSet)},
+		}, IntRanges{}, mapset.NewThreadUnsafeSet[string](), mapset.NewThreadUnsafeSet[string]()},
+		{IntRanges{}, IntRanges{}, mapset.NewThreadUnsafeSet[string](), mapset.NewThreadUnsafeSet[string]()},
+		{IntRanges{}, IntRanges{}, mapset.NewThreadUnsafeSet[string](), mapset.NewThreadUnsafeSet[string]()},
+		{IntRanges{}, IntRanges{}, mapset.NewThreadUnsafeSet[string]("a", "b", "c", "d", "e"), mapset.NewThreadUnsafeSet[string]()},
 	}
 
 	for n, in := range inputs {
 		res := expected[n]
 		include, exclude, otherInclude, otherExclude := ParseNumberMenu(in)
 
-		if !intRangesEqual(include, res.Include) ||
-			!intRangesEqual(exclude, res.Exclude) ||
-			!stringset.Equal(otherInclude, res.OtherInclude) ||
-			!stringset.Equal(otherExclude, res.OtherExclude) {
-			t.Fatalf("Test %d Failed: Expected: include=%+v exclude=%+v otherInclude=%+v otherExclude=%+v got include=%+v excluive=%+v otherInclude=%+v otherExclude=%+v",
-				n+1, res.Include, res.Exclude, res.OtherInclude, res.OtherExclude, include, exclude, otherInclude, otherExclude)
-		}
+		assert.True(t, intRangesEqual(include, res.Include), "Test %d Failed: Expected: include=%+v got include=%+v", n+1, res.Include, include)
+		assert.True(t, intRangesEqual(exclude, res.Exclude), "Test %d Failed: Expected: exclude=%+v got exclude=%+v", n+1, res.Exclude, exclude)
+		assert.True(t, otherInclude.Equal(res.OtherInclude), "Test %d Failed: Expected: otherInclude=%+v got otherInclude=%+v", n+1, res.OtherInclude, otherInclude)
+		assert.True(t, otherExclude.Equal(res.OtherExclude), "Test %d Failed: Expected: otherExclude=%+v got otherExclude=%+v", n+1, res.OtherExclude, otherExclude)
 	}
 }
 

+ 8 - 8
pkg/menus/menu.go

@@ -53,13 +53,13 @@ func selectionMenu(w io.Writer, pkgbuildDirs map[string]string, bases []string,
 	}
 
 	eInclude, eExclude, eOtherInclude, eOtherExclude := intrange.ParseNumberMenu(selectInput)
-	eIsInclude := len(eExclude) == 0 && len(eOtherExclude) == 0
+	eIsInclude := len(eExclude) == 0 && eOtherExclude.Cardinality() == 0
 
-	if eOtherInclude.Get("abort") || eOtherInclude.Get("ab") {
+	if eOtherInclude.Contains("abort") || eOtherInclude.Contains("ab") {
 		return nil, settings.ErrUserAbort{}
 	}
 
-	if eOtherInclude.Get("n") || eOtherInclude.Get("none") {
+	if eOtherInclude.Contains("n") || eOtherInclude.Contains("none") {
 		return selected, nil
 	}
 
@@ -74,26 +74,26 @@ func selectionMenu(w io.Writer, pkgbuildDirs map[string]string, bases []string,
 			continue
 		}
 
-		if anyInstalled && (eOtherInclude.Get("i") || eOtherInclude.Get("installed")) {
+		if anyInstalled && (eOtherInclude.Contains("i") || eOtherInclude.Contains("installed")) {
 			selected = append(selected, pkgBase)
 			continue
 		}
 
-		if !anyInstalled && (eOtherInclude.Get("no") || eOtherInclude.Get("notinstalled")) {
+		if !anyInstalled && (eOtherInclude.Contains("no") || eOtherInclude.Contains("notinstalled")) {
 			selected = append(selected, pkgBase)
 			continue
 		}
 
-		if eOtherInclude.Get("a") || eOtherInclude.Get("all") {
+		if eOtherInclude.Contains("a") || eOtherInclude.Contains("all") {
 			selected = append(selected, pkgBase)
 			continue
 		}
 
-		if eIsInclude && (eInclude.Get(len(bases)-i) || eOtherInclude.Get(pkgBase)) {
+		if eIsInclude && (eInclude.Get(len(bases)-i) || eOtherInclude.Contains(pkgBase)) {
 			selected = append(selected, pkgBase)
 		}
 
-		if !eIsInclude && (!eExclude.Get(len(bases)-i) && !eOtherExclude.Get(pkgBase)) {
+		if !eIsInclude && (!eExclude.Get(len(bases)-i) && !eOtherExclude.Contains(pkgBase)) {
 			selected = append(selected, pkgBase)
 		}
 	}

+ 1 - 3
pkg/query/aur_warnings.go

@@ -9,7 +9,6 @@ import (
 	"github.com/Jguer/go-alpm/v2"
 
 	"github.com/Jguer/yay/v12/pkg/db"
-	"github.com/Jguer/yay/v12/pkg/stringset"
 	"github.com/Jguer/yay/v12/pkg/text"
 )
 
@@ -18,7 +17,6 @@ type AURWarnings struct {
 	OutOfDate  []string
 	Missing    []string
 	LocalNewer []string
-	Ignore     stringset.StringSet
 
 	log *text.Logger
 }
@@ -27,7 +25,7 @@ func NewWarnings(logger *text.Logger) *AURWarnings {
 	if logger == nil {
 		logger = text.GlobalLogger
 	}
-	return &AURWarnings{Ignore: make(stringset.StringSet), log: logger}
+	return &AURWarnings{log: logger}
 }
 
 func (warnings *AURWarnings) AddToWarnings(remote map[string]alpm.IPackage, aurPkg *aur.Pkg) {

+ 4 - 4
pkg/query/query_builder.go

@@ -11,12 +11,12 @@ import (
 	"github.com/Jguer/go-alpm/v2"
 	"github.com/adrg/strutil"
 	"github.com/adrg/strutil/metrics"
+	mapset "github.com/deckarep/golang-set/v2"
 	"github.com/leonelquinteros/gotext"
 
 	"github.com/Jguer/yay/v12/pkg/db"
 	"github.com/Jguer/yay/v12/pkg/intrange"
 	"github.com/Jguer/yay/v12/pkg/settings/parser"
-	"github.com/Jguer/yay/v12/pkg/stringset"
 	"github.com/Jguer/yay/v12/pkg/text"
 )
 
@@ -35,7 +35,7 @@ type Builder interface {
 	Len() int
 	Execute(ctx context.Context, dbExecutor db.Executor, pkgS []string)
 	Results(dbExecutor db.Executor, verboseSearch SearchVerbosity) error
-	GetTargets(include, exclude intrange.IntRanges, otherExclude stringset.StringSet) ([]string, error)
+	GetTargets(include, exclude intrange.IntRanges, otherExclude mapset.Set[string]) ([]string, error)
 }
 
 type SourceQueryBuilder struct {
@@ -253,10 +253,10 @@ func (s *SourceQueryBuilder) Len() int {
 }
 
 func (s *SourceQueryBuilder) GetTargets(include, exclude intrange.IntRanges,
-	otherExclude stringset.StringSet,
+	otherExclude mapset.Set[string],
 ) ([]string, error) {
 	var (
-		isInclude = len(exclude) == 0 && len(otherExclude) == 0
+		isInclude = len(exclude) == 0 && otherExclude.Cardinality() == 0
 		targets   []string
 		lenRes    = len(s.results)
 	)

+ 0 - 107
pkg/stringset/stringset.go

@@ -1,107 +0,0 @@
-package stringset
-
-// StringSet is a basic set implementation for strings.
-// This is used a lot so it deserves its own type.
-// Other types of sets are used throughout the code but do not have
-// their own typedef.
-// String sets and <type>sets should be used throughout the code when applicable,
-// they are a lot more flexible than slices and provide easy lookup.
-type StringSet map[string]struct{}
-
-// MapStringSet is a Map of StringSets.
-type MapStringSet map[string]StringSet
-
-// Add adds a new value to the Map.
-// If n is already in the map, then v is appended to the StringSet under that key.
-// Otherwise a new StringSet is creayed containing v.
-func (mss MapStringSet) Add(n, v string) {
-	if _, ok := mss[n]; !ok {
-		mss[n] = make(StringSet)
-	}
-
-	mss[n].Set(v)
-}
-
-// Set sets key in StringSet.
-func (set StringSet) Set(v string) {
-	set[v] = struct{}{}
-}
-
-// Extend sets multiple keys in StringSet.
-func (set StringSet) Extend(s ...string) {
-	for _, v := range s {
-		set[v] = struct{}{}
-	}
-}
-
-// Get returns true if the key exists in the set.
-func (set StringSet) Get(v string) bool {
-	_, exists := set[v]
-	return exists
-}
-
-// Remove deletes a key from the set.
-func (set StringSet) Remove(v string) {
-	delete(set, v)
-}
-
-// ToSlice turns all keys into a string slice.
-func (set StringSet) ToSlice() []string {
-	slice := make([]string, 0, len(set))
-
-	for v := range set {
-		slice = append(slice, v)
-	}
-
-	return slice
-}
-
-// Copy copies a StringSet into a new structure of the same type.
-func (set StringSet) Copy() StringSet {
-	newSet := make(StringSet)
-
-	for str := range set {
-		newSet.Set(str)
-	}
-
-	return newSet
-}
-
-// FromSlice creates a new StringSet from an input slice.
-func FromSlice(in []string) StringSet {
-	set := make(StringSet)
-
-	for _, v := range in {
-		set.Set(v)
-	}
-
-	return set
-}
-
-// Make creates a new StringSet from a set of arguments.
-func Make(in ...string) StringSet {
-	return FromSlice(in)
-}
-
-// Equal compares if two StringSets have the same values.
-func Equal(a, b StringSet) bool {
-	if a == nil && b == nil {
-		return true
-	}
-
-	if a == nil || b == nil {
-		return false
-	}
-
-	if len(a) != len(b) {
-		return false
-	}
-
-	for n := range a {
-		if !b.Get(n) {
-			return false
-		}
-	}
-
-	return true
-}

+ 3 - 3
pkg/upgrade/service.go

@@ -303,13 +303,13 @@ func (u *UpgradeService) UserExcludeUpgrades(graph *topo.Graph[string, *dep.Inst
 	// upgrade menu asks you which packages to NOT upgrade so in this case
 	// exclude and include are kind of swapped
 	exclude, include, otherExclude, otherInclude := intrange.ParseNumberMenu(numbers)
-	isInclude := len(include) == 0 && len(otherInclude) == 0
+	isInclude := len(include) == 0 && otherInclude.Cardinality() == 0
 
 	excluded := make([]string, 0)
 	for i := range allUp.Up {
 		up := &allUp.Up[i]
 
-		if isInclude && otherExclude.Get(up.Repository) {
+		if isInclude && otherExclude.Contains(up.Repository) {
 			u.log.Debugln("pruning", up.Name)
 			excluded = append(excluded, graph.Prune(up.Name)...)
 			continue
@@ -321,7 +321,7 @@ func (u *UpgradeService) UserExcludeUpgrades(graph *topo.Graph[string, *dep.Inst
 			continue
 		}
 
-		if !isInclude && !(include.Get(len(allUp.Up)-i) || otherInclude.Get(up.Repository)) {
+		if !isInclude && !(include.Get(len(allUp.Up)-i) || otherInclude.Contains(up.Repository)) {
 			u.log.Debugln("pruning", up.Name)
 			excluded = append(excluded, graph.Prune(up.Name)...)
 			continue

+ 17 - 3
query.go

@@ -8,12 +8,12 @@ import (
 
 	aur "github.com/Jguer/aur"
 	alpm "github.com/Jguer/go-alpm/v2"
+	mapset "github.com/deckarep/golang-set/v2"
 
 	"github.com/Jguer/yay/v12/pkg/db"
 	"github.com/Jguer/yay/v12/pkg/query"
 	"github.com/Jguer/yay/v12/pkg/settings"
 	"github.com/Jguer/yay/v12/pkg/settings/parser"
-	"github.com/Jguer/yay/v12/pkg/stringset"
 	"github.com/Jguer/yay/v12/pkg/text"
 )
 
@@ -116,6 +116,20 @@ func packageSlices(toCheck []string, config *settings.Configuration, dbExecutor
 	return aurNames, repoNames
 }
 
+// MapSetMap is a Map of Sets.
+type mapSetMap[T comparable] map[T]mapset.Set[T]
+
+// Add adds a new value to the Map.
+// If n is already in the map, then v is appended to the StringSet under that key.
+// Otherwise a new Set is created containing v.
+func (mss mapSetMap[T]) Add(n, v T) {
+	if _, ok := mss[n]; !ok {
+		mss[n] = mapset.NewSet[T]()
+	}
+
+	mss[n].Add(v)
+}
+
 // HangingPackages returns a list of packages installed as deps
 // and unneeded by the system
 // removeOptional decides whether optional dependencies are counted or not.
@@ -126,7 +140,7 @@ func hangingPackages(removeOptional bool, dbExecutor db.Executor) (hanging []str
 	// State = 2 - Keep package and have iterated over dependencies
 	safePackages := make(map[string]uint8)
 	// provides stores a mapping from the provides name back to the original package name
-	provides := make(stringset.MapStringSet)
+	provides := make(mapSetMap[string])
 
 	packages := dbExecutor.LocalPackages()
 	// Mark explicit dependencies and enumerate the provides list
@@ -166,7 +180,7 @@ func hangingPackages(removeOptional bool, dbExecutor db.Executor) (hanging []str
 				if !ok {
 					// Check if dep is a provides rather than actual package name
 					if pset, ok2 := provides[dep.Name]; ok2 {
-						for p := range pset {
+						for p := range pset.Iter() {
 							if safePackages[p] == 0 {
 								iterateAgain = true
 								safePackages[p] = 1