Skip to content

Commit

Permalink
Merge pull request #108 from amirmalka/refactor-alllists
Browse files Browse the repository at this point in the history
Added type assertion after getting objects from the pool
  • Loading branch information
amirmalka authored May 29, 2023
2 parents 36b5907 + 37e479e commit e68640b
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions reporthandling/helpers/v1/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ var allListsPool = &sync.Pool{

// GetAllListsFromPool get the AllLists object from the pool
func GetAllListsFromPool() *AllLists {
l := allListsPool.Get().(*AllLists)
l, ok := allListsPool.Get().(*AllLists)
if !ok {
return nil
}
// reset the object before returning it as it might be dirty
l.Clear()
return l
Expand Down Expand Up @@ -77,48 +80,40 @@ func (all *AllLists) Append(status apis.ScanningStatus, str ...string) {
oldStatus, exist := all.itemToStatus[s]
if !exist {
all.itemToStatus[s] = status
switch status {
case apis.StatusPassed:
all.passed++
case apis.StatusFailed:
all.failed++
case apis.StatusSkipped:
all.skipped++
default:
all.other++
}
all.updateCounters(status, true)
// element exist with different status
} else if oldStatus != status {
// check if the new status is more significant
if result := apis.Compare(oldStatus, status); result == status {
all.itemToStatus[s] = status
switch status {
case apis.StatusPassed:
all.passed++
case apis.StatusFailed:
all.failed++
case apis.StatusSkipped:
all.skipped++
default:
all.other++
}

// update the old status
switch oldStatus {
case apis.StatusPassed:
all.passed--
case apis.StatusFailed:
all.failed--
case apis.StatusSkipped:
all.skipped--
default:
all.other--
}
all.updateCounters(status, true)
all.updateCounters(oldStatus, false)
}
}
}
}

// Helper function to update status counters
func (all *AllLists) updateCounters(status apis.ScanningStatus, increment bool) {
var delta int
if increment {
delta = 1
} else {
delta = -1
}

switch status {
case apis.StatusPassed:
all.passed += delta
case apis.StatusFailed:
all.failed += delta
case apis.StatusSkipped:
all.skipped += delta
default:
all.other += delta
}
}

// Update AllLists objects with
func (all *AllLists) Update(all2 *AllLists) {
for item, status := range all2.itemToStatus {
Expand Down

0 comments on commit e68640b

Please sign in to comment.