Skip to content

Commit

Permalink
rename Set type to Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
svenwltr committed Feb 5, 2018
1 parent 4629ded commit 88419ca
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 82 deletions.
4 changes: 2 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

type ResourceConfig struct {
Targets types.Set `yaml:"targets"`
Excludes types.Set `yaml:"excludes"`
Targets types.Collection `yaml:"targets"`
Excludes types.Collection `yaml:"excludes"`
}

type NukeConfig struct {
Expand Down
6 changes: 3 additions & 3 deletions cmd/nuke.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Nuke struct {
Account awsutil.Account
Config *NukeConfig

ResourceTypes types.Set
ResourceTypes types.Collection

ForceSleep time.Duration

Expand Down Expand Up @@ -113,12 +113,12 @@ func (n *Nuke) Scan() error {

resourceTypes := ResolveResourceTypes(
resources.GetListerNames(),
[]types.Set{
[]types.Collection{
n.Parameters.Targets,
n.Config.ResourceTypes.Targets,
accountConfig.ResourceTypes.Targets,
},
[]types.Set{
[]types.Collection{
n.Parameters.Excludes,
n.Config.ResourceTypes.Excludes,
accountConfig.ResourceTypes.Excludes,
Expand Down
2 changes: 1 addition & 1 deletion cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Prompt(expect string) error {
return nil
}

func ResolveResourceTypes(base types.Set, include, exclude []types.Set) types.Set {
func ResolveResourceTypes(base types.Collection, include, exclude []types.Collection) types.Collection {
for _, i := range include {
if len(i) > 0 {
base = base.Intersect(i)
Expand Down
40 changes: 20 additions & 20 deletions cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@ import (

func TestResolveResourceTypes(t *testing.T) {
cases := []struct {
base types.Set
include []types.Set
exclude []types.Set
result types.Set
base types.Collection
include []types.Collection
exclude []types.Collection
result types.Collection
}{
{
base: types.Set{"a", "b", "c", "d"},
include: []types.Set{types.Set{"a", "b", "c"}},
result: types.Set{"a", "b", "c"},
base: types.Collection{"a", "b", "c", "d"},
include: []types.Collection{types.Collection{"a", "b", "c"}},
result: types.Collection{"a", "b", "c"},
},
{
base: types.Set{"a", "b", "c", "d"},
exclude: []types.Set{types.Set{"b", "d"}},
result: types.Set{"a", "c"},
base: types.Collection{"a", "b", "c", "d"},
exclude: []types.Collection{types.Collection{"b", "d"}},
result: types.Collection{"a", "c"},
},
{
base: types.Set{"a", "b"},
include: []types.Set{types.Set{}},
result: types.Set{"a", "b"},
base: types.Collection{"a", "b"},
include: []types.Collection{types.Collection{}},
result: types.Collection{"a", "b"},
},
{
base: types.Set{"c", "b"},
exclude: []types.Set{types.Set{}},
result: types.Set{"c", "b"},
base: types.Collection{"c", "b"},
exclude: []types.Collection{types.Collection{}},
result: types.Collection{"c", "b"},
},
{
base: types.Set{"a", "b", "c", "d"},
include: []types.Set{types.Set{"a", "b", "c"}},
exclude: []types.Set{types.Set{"a"}},
result: types.Set{"b", "c"},
base: types.Collection{"a", "b", "c", "d"},
include: []types.Collection{types.Collection{"a", "b", "c"}},
exclude: []types.Collection{types.Collection{"a"}},
result: types.Collection{"b", "c"},
},
}

Expand Down
50 changes: 50 additions & 0 deletions pkg/types/collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package types

type Collection []string

func (c Collection) Intersect(o Collection) Collection {
mo := o.toMap()

result := Collection{}
for _, t := range c {
if mo[t] {
result = append(result, t)
}
}

return result
}

func (c Collection) Remove(o Collection) Collection {
mo := o.toMap()

result := Collection{}
for _, t := range c {
if !mo[t] {
result = append(result, t)
}
}

return result
}

func (c Collection) Union(o Collection) Collection {
ms := c.toMap()

result := []string(c)
for _, oi := range o {
if !ms[oi] {
result = append(result, oi)
}
}

return Collection(result)
}

func (c Collection) toMap() map[string]bool {
m := map[string]bool{}
for _, t := range c {
m[t] = true
}
return m
}
12 changes: 6 additions & 6 deletions pkg/types/set_test.go → pkg/types/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

func TestSetInterset(t *testing.T) {
s1 := types.Set{"a", "b", "c"}
s2 := types.Set{"b", "a", "d"}
s1 := types.Collection{"a", "b", "c"}
s2 := types.Collection{"b", "a", "d"}

r := s1.Intersect(s2)

Expand All @@ -22,8 +22,8 @@ func TestSetInterset(t *testing.T) {
}

func TestSetRemove(t *testing.T) {
s1 := types.Set{"a", "b", "c"}
s2 := types.Set{"b", "a", "d"}
s1 := types.Collection{"a", "b", "c"}
s2 := types.Collection{"b", "a", "d"}

r := s1.Remove(s2)

Expand All @@ -36,8 +36,8 @@ func TestSetRemove(t *testing.T) {
}

func TestSetUnion(t *testing.T) {
s1 := types.Set{"a", "b", "c"}
s2 := types.Set{"b", "a", "d"}
s1 := types.Collection{"a", "b", "c"}
s2 := types.Collection{"b", "a", "d"}

r := s1.Union(s2)

Expand Down
50 changes: 0 additions & 50 deletions pkg/types/set.go

This file was deleted.

0 comments on commit 88419ca

Please sign in to comment.