Skip to content

Commit

Permalink
chore(yay): replace custom set package with dep (#2249)
Browse files Browse the repository at this point in the history
* replace string set with dep

* remove unused field

* remove custom string set package
  • Loading branch information
Jguer authored Jul 23, 2023
1 parent abd398a commit 04c82b8
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 165 deletions.
16 changes: 8 additions & 8 deletions clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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())
}
}

Expand All @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/dep/dep_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pkg/intrange/intrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 == ','
Expand All @@ -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 {
Expand Down
42 changes: 20 additions & 22 deletions pkg/intrange/intrange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ 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) {
t.Parallel()
type result struct {
Include IntRanges
Exclude IntRanges
OtherInclude stringset.StringSet
OtherExclude stringset.StringSet
OtherInclude mapset.Set[string]
OtherExclude mapset.Set[string]
}

inputs := []string{
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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)
}
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/menus/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/query/aur_warnings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -18,7 +17,6 @@ type AURWarnings struct {
OutOfDate []string
Missing []string
LocalNewer []string
Ignore stringset.StringSet

log *text.Logger
}
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/query/query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
)
Expand Down
Loading

0 comments on commit 04c82b8

Please sign in to comment.