Skip to content

Commit

Permalink
Refactor complex map types into their own type
Browse files Browse the repository at this point in the history
map[string][]string  -> mapStringSlice
mao[string]stringSet -> mapStringSet
  • Loading branch information
Morganamilo committed May 7, 2018
1 parent 58f23bf commit ef7e8ac
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
36 changes: 18 additions & 18 deletions conflicts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Checks a single conflict against every other to be installed package's
// name and its provides.
func checkInnerConflict(name string, conflict string, conflicts map[string]stringSet, dc *depCatagories) {
func checkInnerConflict(name string, conflict string, conflicts mapStringSet, dc *depCatagories) {
deps, err := gopkg.ParseDeps([]string{conflict})
if err != nil {
return
Expand All @@ -28,7 +28,7 @@ func checkInnerConflict(name string, conflict string, conflicts map[string]strin
return
}
if dep.Name == pkg.Name && version.Satisfies(dep) {
addMapStringSet(conflicts, name, pkg.Name)
conflicts.Add(name, pkg.Name)
continue
}

Expand Down Expand Up @@ -57,7 +57,7 @@ func checkInnerConflict(name string, conflict string, conflicts map[string]strin
}

if version != nil && version.Satisfies(dep) {
addMapStringSet(conflicts, name, pkg.Name)
conflicts.Add(name, pkg.Name)
break
}

Expand All @@ -75,7 +75,7 @@ func checkInnerConflict(name string, conflict string, conflicts map[string]strin
}

if dep.Name == pkg.Name() && version.Satisfies(dep) {
addMapStringSet(conflicts, name, pkg.Name())
conflicts.Add(name, pkg.Name())
continue
}

Expand All @@ -92,7 +92,7 @@ func checkInnerConflict(name string, conflict string, conflicts map[string]strin
}

if provide.Mod == alpm.DepModAny {
addMapStringSet(conflicts, name, pkg.Name())
conflicts.Add(name, pkg.Name())
return fmt.Errorf("")
}

Expand All @@ -102,7 +102,7 @@ func checkInnerConflict(name string, conflict string, conflicts map[string]strin
}

if version.Satisfies(dep) {
addMapStringSet(conflicts, name, pkg.Name())
conflicts.Add(name, pkg.Name())
return fmt.Errorf("")
}

Expand All @@ -113,8 +113,8 @@ func checkInnerConflict(name string, conflict string, conflicts map[string]strin

// Checks every to be installed package's conflicts against every other to be
// installed package and its provides.
func checkForInnerConflicts(dc *depCatagories) map[string]stringSet {
conflicts := make(map[string]stringSet)
func checkForInnerConflicts(dc *depCatagories) mapStringSet {
conflicts := make(mapStringSet)

for _, pkg := range dc.Aur {
for _, cpkg := range pkg.Conflicts {
Expand All @@ -134,7 +134,7 @@ func checkForInnerConflicts(dc *depCatagories) map[string]stringSet {

// Checks a provide or packagename from a to be installed package
// against every already installed package's conflicts
func checkReverseConflict(name string, provide string, conflicts map[string]stringSet) error {
func checkReverseConflict(name string, provide string, conflicts mapStringSet) error {
var version *gopkg.CompleteVersion
var err error

Expand Down Expand Up @@ -176,7 +176,7 @@ func checkReverseConflict(name string, provide string, conflicts map[string]stri

if version == nil || version.Satisfies(dep) {
// Todo
addMapStringSet(conflicts, name, pkg.Name()+" ("+provide+")")
conflicts.Add(name, pkg.Name()+" ("+provide+")")
return fmt.Errorf("")
}

Expand All @@ -191,7 +191,7 @@ func checkReverseConflict(name string, provide string, conflicts map[string]stri

// Checks the conflict of a to be installed package against the package name and
// provides of every installed package.
func checkConflict(name string, conflict string, conflicts map[string]stringSet) error {
func checkConflict(name string, conflict string, conflicts mapStringSet) error {
localDb, err := alpmHandle.LocalDb()
if err != nil {
return err
Expand All @@ -215,7 +215,7 @@ func checkConflict(name string, conflict string, conflicts map[string]stringSet)
}

if dep.Name == pkg.Name() && version.Satisfies(dep) {
addMapStringSet(conflicts, name, pkg.Name())
conflicts.Add(name, pkg.Name())
return nil
}

Expand All @@ -232,7 +232,7 @@ func checkConflict(name string, conflict string, conflicts map[string]stringSet)
}

if provide.Mod == alpm.DepModAny {
addMapStringSet(conflicts, name, pkg.Name()+" ("+provide.Name+")")
conflicts.Add(name, pkg.Name()+" ("+provide.Name+")")
return fmt.Errorf("")
}

Expand All @@ -242,7 +242,7 @@ func checkConflict(name string, conflict string, conflicts map[string]stringSet)
}

if version.Satisfies(dep) {
addMapStringSet(conflicts, name, pkg.Name()+" ("+provide.Name+")")
conflicts.Add(name, pkg.Name()+" ("+provide.Name+")")
return fmt.Errorf("")
}

Expand All @@ -258,8 +258,8 @@ func checkConflict(name string, conflict string, conflicts map[string]stringSet)
// Checks every to be installed package's conflicts against the names and
// provides of every already installed package and checks every to be installed
// package's name and provides against every already installed package.
func checkForConflicts(dc *depCatagories) (map[string]stringSet, error) {
conflicts := make(map[string]stringSet)
func checkForConflicts(dc *depCatagories) (mapStringSet, error) {
conflicts := make(mapStringSet)

for _, pkg := range dc.Aur {
for _, cpkg := range pkg.Conflicts {
Expand Down Expand Up @@ -296,8 +296,8 @@ func checkForConflicts(dc *depCatagories) (map[string]stringSet, error) {
// does some printing.
func checkForAllConflicts(dc *depCatagories) error {
var err error
var conflicts map[string]stringSet
var innerConflicts map[string]stringSet
var conflicts mapStringSet
var innerConflicts mapStringSet
var wg sync.WaitGroup
wg.Add(2)

Expand Down
12 changes: 6 additions & 6 deletions dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ func depTreeRecursive(dt *depTree, localDb *alpm.Db, syncDb alpm.DbList, isMake
}

func checkVersions(dt *depTree) error {
has := make(map[string][]string)
has := make(mapStringSlice)
allDeps := make([]*gopkg.Dependency, 0)

localDb, err := alpmHandle.LocalDb()
Expand All @@ -569,13 +569,13 @@ func checkVersions(dt *depTree) error {
}
}

addMapStringSlice(has, pkg.Name, pkg.Version)
has.Add(pkg.Name, pkg.Version)

if !isDevelName(pkg.Name) {
for _, name := range pkg.Provides {
_name, _ver := splitNameFromDep(name)
if _ver != "" {
addMapStringSlice(has, _name, _ver)
has.Add(_name, _ver)
} else {
delete(has, _name)
}
Expand All @@ -594,11 +594,11 @@ func checkVersions(dt *depTree) error {
return nil
})

addMapStringSlice(has, pkg.Name(), pkg.Version())
has.Add(pkg.Name(), pkg.Version())

pkg.Provides().ForEach(func(dep alpm.Depend) error {
if dep.Mod != alpm.DepModAny {
addMapStringSlice(has, dep.Name, dep.Version)
has.Add(dep.Name, dep.Version)
} else {
delete(has, dep.Name)
}
Expand All @@ -611,7 +611,7 @@ func checkVersions(dt *depTree) error {
localDb.PkgCache().ForEach(func(pkg alpm.Package) error {
pkg.Provides().ForEach(func(dep alpm.Depend) error {
if dep.Mod != alpm.DepModAny {
addMapStringSlice(has, dep.Name, dep.Version)
has.Add(dep.Name, dep.Version)
} else {
delete(has, dep.Name)
}
Expand Down
4 changes: 2 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func hangingPackages(removeOptional bool) (hanging []string, err error) {
// 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(map[string]stringSet)
provides := make(mapStringSet)
packages := localDb.PkgCache()

// Mark explicit dependencies and enumerate the provides list
Expand All @@ -344,7 +344,7 @@ func hangingPackages(removeOptional bool) (hanging []string, err error) {
}

pkg.Provides().ForEach(func(dep alpm.Depend) error {
addMapStringSet(provides, dep.Name, pkg.Name())
provides.Add(dep.Name, pkg.Name())
return nil
})
return nil
Expand Down
19 changes: 11 additions & 8 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"unicode"
)

type mapStringSlice map[string][]string
type mapStringSet map[string]stringSet

type intRange struct {
min int
max int
Expand Down Expand Up @@ -49,20 +52,20 @@ func max(a, b int) int {
return a
}

func addMapStringSet(h map[string]stringSet, n string, v string) {
_, ok := h[n]
func (mss mapStringSet) Add(n string, v string) {
_, ok := mss[n]
if !ok {
h[n] = make(stringSet)
mss[n] = make(stringSet)
}
h[n].set(v)
mss[n].set(v)
}

func addMapStringSlice(h map[string][]string, n string, v string) {
_, ok := h[n]
func (mss mapStringSlice) Add(n string, v string) {
_, ok := mss[n]
if !ok {
h[n] = make([]string, 0, 1)
mss[n] = make([]string, 0, 1)
}
h[n] = append(h[n], v)
mss[n] = append(mss[n], v)
}

func completeFileName(dir, name string) (string, error) {
Expand Down

0 comments on commit ef7e8ac

Please sign in to comment.