Skip to content

Commit

Permalink
fix order by to not remove keys (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
iignatevich authored Nov 7, 2024
1 parent d3b9111 commit 27182e2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/sync/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type Inventory struct {

//internal
resourcesMap *OrderedMap[bool]
topOrder []string
requiredMap map[string]*OrderedMap[bool]
dependencyMap map[string]*OrderedMap[bool]

Expand Down Expand Up @@ -135,7 +136,7 @@ func (i *Inventory) GetResourcesMap() *OrderedMap[bool] {

// GetResourcesOrder returns the order of resources in the inventory.
func (i *Inventory) GetResourcesOrder() []string {
return i.resourcesMap.Keys()
return i.topOrder
}

// GetRequiredMap returns the required map, which represents the dependencies between resources in the Inventory.
Expand Down Expand Up @@ -260,6 +261,7 @@ func (i *Inventory) buildResourcesGraph() error {
order[y], order[j] = order[j], order[y]
}

i.topOrder = order
i.resourcesMap.OrderBy(order)

return nil
Expand Down
17 changes: 17 additions & 0 deletions pkg/sync/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,22 @@ func (m *OrderedMap[T]) Keys() []string {
// OrderBy updates the order of keys in the [OrderedMap] based on the orderList.
func (m *OrderedMap[T]) OrderBy(orderList []string) {
var newKeys []string
var remainingKeys []string

keysLoop:
for _, key := range m.keys {
isInOrderList := false
for _, orderKey := range orderList {
if key == orderKey {
isInOrderList = true
continue keysLoop
}
}

if !isInOrderList {
remainingKeys = append(remainingKeys, key)
}
}

for _, item := range orderList {
_, ok := m.Get(item)
Expand All @@ -279,6 +295,7 @@ func (m *OrderedMap[T]) OrderBy(orderList []string) {
}
}

newKeys = append(newKeys, remainingKeys...)
m.keys = newKeys
}

Expand Down

0 comments on commit 27182e2

Please sign in to comment.