fix(GODT-2626): Handle rare crash due to missing address update ch

Ensure that we can handle the rare case that can cause a crash if for
whichever reason we end up with an Address Delete and Message
Create/Update in the same event object.
This commit is contained in:
Leander Beernaert
2023-05-16 10:41:36 +02:00
parent 4e3ad4f7fa
commit d8ccc6c05d
2 changed files with 76 additions and 5 deletions

View File

@ -83,6 +83,18 @@ func getAddrIdx(apiAddrs map[string]proton.Address, idx int) (proton.Address, er
return sorted[idx], nil
}
func getPrimaryAddr(apiAddrs map[string]proton.Address) (proton.Address, error) {
sorted := sortSlice(maps.Values(apiAddrs), func(a, b proton.Address) bool {
return a.Order < b.Order
})
if len(sorted) == 0 {
return proton.Address{}, fmt.Errorf("no addresses available")
}
return sorted[0], nil
}
// sortSlice returns the given slice sorted by the given comparator.
func sortSlice[Item any](items []Item, less func(Item, Item) bool) []Item {
sorted := make([]Item, len(items))