Skip to content

Commit 37e479e

Browse files
committed
added type assertion after getting objects from the pool
Signed-off-by: Amir Malka <[email protected]>
1 parent 36b5907 commit 37e479e

File tree

1 file changed

+28
-33
lines changed

1 file changed

+28
-33
lines changed

reporthandling/helpers/v1/listing.go

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ var allListsPool = &sync.Pool{
1515

1616
// GetAllListsFromPool get the AllLists object from the pool
1717
func GetAllListsFromPool() *AllLists {
18-
l := allListsPool.Get().(*AllLists)
18+
l, ok := allListsPool.Get().(*AllLists)
19+
if !ok {
20+
return nil
21+
}
1922
// reset the object before returning it as it might be dirty
2023
l.Clear()
2124
return l
@@ -77,48 +80,40 @@ func (all *AllLists) Append(status apis.ScanningStatus, str ...string) {
7780
oldStatus, exist := all.itemToStatus[s]
7881
if !exist {
7982
all.itemToStatus[s] = status
80-
switch status {
81-
case apis.StatusPassed:
82-
all.passed++
83-
case apis.StatusFailed:
84-
all.failed++
85-
case apis.StatusSkipped:
86-
all.skipped++
87-
default:
88-
all.other++
89-
}
83+
all.updateCounters(status, true)
9084
// element exist with different status
9185
} else if oldStatus != status {
9286
// check if the new status is more significant
9387
if result := apis.Compare(oldStatus, status); result == status {
9488
all.itemToStatus[s] = status
95-
switch status {
96-
case apis.StatusPassed:
97-
all.passed++
98-
case apis.StatusFailed:
99-
all.failed++
100-
case apis.StatusSkipped:
101-
all.skipped++
102-
default:
103-
all.other++
104-
}
105-
106-
// update the old status
107-
switch oldStatus {
108-
case apis.StatusPassed:
109-
all.passed--
110-
case apis.StatusFailed:
111-
all.failed--
112-
case apis.StatusSkipped:
113-
all.skipped--
114-
default:
115-
all.other--
116-
}
89+
all.updateCounters(status, true)
90+
all.updateCounters(oldStatus, false)
11791
}
11892
}
11993
}
12094
}
12195

96+
// Helper function to update status counters
97+
func (all *AllLists) updateCounters(status apis.ScanningStatus, increment bool) {
98+
var delta int
99+
if increment {
100+
delta = 1
101+
} else {
102+
delta = -1
103+
}
104+
105+
switch status {
106+
case apis.StatusPassed:
107+
all.passed += delta
108+
case apis.StatusFailed:
109+
all.failed += delta
110+
case apis.StatusSkipped:
111+
all.skipped += delta
112+
default:
113+
all.other += delta
114+
}
115+
}
116+
122117
// Update AllLists objects with
123118
func (all *AllLists) Update(all2 *AllLists) {
124119
for item, status := range all2.itemToStatus {

0 commit comments

Comments
 (0)