Other(refactor): Use normal value + mutex for user.apiAddrs

This commit is contained in:
James Houlahan
2022-10-26 23:48:18 +02:00
parent 83339da26c
commit 0bc99dbd4f
8 changed files with 311 additions and 306 deletions

View File

@ -24,6 +24,8 @@ import (
"strings"
"gitlab.protontech.ch/go/liteapi"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
// mapTo converts the slice to the given type.
@ -82,7 +84,7 @@ func hexDecode(b []byte) ([]byte, error) {
}
// getAddrID returns the address ID for the given email address.
func getAddrID(apiAddrs []liteapi.Address, email string) (string, error) {
func getAddrID(apiAddrs map[string]liteapi.Address, email string) (string, error) {
for _, addr := range apiAddrs {
if strings.EqualFold(addr.Email, sanitizeEmail(email)) {
return addr.ID, nil
@ -91,3 +93,27 @@ func getAddrID(apiAddrs []liteapi.Address, email string) (string, error) {
return "", fmt.Errorf("address %s not found", email)
}
// getAddrIdx returns the address with the given index.
func getAddrIdx(apiAddrs map[string]liteapi.Address, idx int) (liteapi.Address, error) {
sorted := sortSlice(maps.Values(apiAddrs), func(a, b liteapi.Address) bool {
return a.Order < b.Order
})
if idx < 0 || idx >= len(sorted) {
return liteapi.Address{}, fmt.Errorf("address index %d out of range", idx)
}
return sorted[idx], 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))
copy(sorted, items)
slices.SortFunc(sorted, less)
return sorted
}