Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Commit

Permalink
Ensure sorted subcomponents (#196)
Browse files Browse the repository at this point in the history
- `AddSubcomponent()` now sorts subcomponents by name
  • Loading branch information
evanlouie authored Jun 18, 2019
1 parent cbadf8d commit 3d727da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
21 changes: 21 additions & 0 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ func TestAdd(t *testing.T) {
// there should be 2 subcomponents now
assert.Nil(t, err)
assert.True(t, len(componentComponent.Subcomponents) == 2)

// Testing: ensure subcomponents sorted by name
componentComponent.Subcomponents = []core.Component{}
assert.True(t, len(componentComponent.Subcomponents) == 0)
subcomponentA := core.Component{
Name: "a",
}
subcomponentB := core.Component{
Name: "b",
}
subcomponentC := core.Component{
Name: "c",
}

// Add subcomponents in random order
assert.Nil(t, componentComponent.AddSubcomponent(subcomponentC, subcomponentA, subcomponentB))

// Subcomponent should be sorted by name
assert.EqualValues(t, componentComponent.Subcomponents[0].Name, "a")
assert.EqualValues(t, componentComponent.Subcomponents[1].Name, "b")
assert.EqualValues(t, componentComponent.Subcomponents[2].Name, "c")
////////////////////////////////////////////////////////////////////////////////
//End adding a subcomponent
////////////////////////////////////////////////////////////////////////////////
Expand Down
12 changes: 9 additions & 3 deletions core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path"
"path/filepath"
"reflect"
"sort"
"strings"
"sync"

Expand Down Expand Up @@ -401,23 +402,28 @@ func (c *Component) Write() (err error) {
return ioutil.WriteFile(path, marshaledComponent, 0644)
}

// AddSubcomponent adds the provided subcomponent to a component.
// AddSubcomponent adds the provided subcomponents to a component.
// If the subcomponents Name matches an existing entry, the existing entry is overwritten.
// If the subcomponents Name does not match, a new subcomponent entry is created.
func (c *Component) AddSubcomponent(subcomponent Component) (err error) {
func (c *Component) AddSubcomponent(subcomponents ...Component) (err error) {
// Index all existing components based on name and add the new component
// Warning: this will remove any duplicates with the same name if present
nameComponentMap := map[string]Component{}
for _, subcomponent := range c.Subcomponents {
nameComponentMap[subcomponent.Name] = subcomponent
}
nameComponentMap[subcomponent.Name] = subcomponent
for _, subcomponent := range subcomponents {
nameComponentMap[subcomponent.Name] = subcomponent
}

// Re-add all subcomponents so no named collisions occur
c.Subcomponents = []Component{}
for _, subcomponent := range nameComponentMap {
c.Subcomponents = append(c.Subcomponents, subcomponent)
}

// Sort by subcomponent name to ensure order is maintained
sort.Slice(c.Subcomponents, func(i, j int) bool { return c.Subcomponents[i].Name < c.Subcomponents[j].Name })

return nil
}

0 comments on commit 3d727da

Please sign in to comment.