Migrate profile icons from fields to list of icons

This commit is contained in:
Daniel
2023-09-12 16:11:46 +02:00
parent e81953d8f3
commit 2a5db42a66
3 changed files with 109 additions and 14 deletions

52
profile/icon.go Normal file
View File

@@ -0,0 +1,52 @@
package profile
import (
"strings"
"golang.org/x/exp/slices"
)
// Icon describes an icon.
type Icon struct {
Type IconType
Value string
}
// IconType describes the type of an Icon.
type IconType string
// Supported icon types.
const (
IconTypeFile IconType = "path"
IconTypeDatabase IconType = "database"
IconTypeBlob IconType = "blob"
)
func (t IconType) sortOrder() int {
switch t {
case IconTypeFile:
return 1
case IconTypeDatabase:
return 2
case IconTypeBlob:
return 3
default:
return 100
}
}
func sortIcons(icons []Icon) {
slices.SortFunc[[]Icon, Icon](icons, func(a, b Icon) int {
aOrder := a.Type.sortOrder()
bOrder := b.Type.sortOrder()
switch {
case aOrder != bOrder:
return aOrder - bOrder
case a.Value != b.Value:
return strings.Compare(a.Value, b.Value)
default:
return 0
}
})
}