Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix order by to not remove keys #55

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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