-
Notifications
You must be signed in to change notification settings - Fork 4
/
group.go
82 lines (68 loc) · 1.48 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package model
import (
"slices"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/util"
)
const defaultIcon = "star"
type Group struct {
Key string `json:"key"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Route string `json:"route,omitempty"`
Icon string `json:"icon,omitempty"`
Tags []string `json:"tags,omitempty"`
Children Groups `json:"children,omitempty"`
Provided bool `json:"provided,omitempty"`
}
func (g *Group) HasTag(t string) bool {
return lo.Contains(g.Tags, t)
}
func (g *Group) IconSafe() string {
if g.Icon == "" {
return defaultIcon
}
return g.Icon
}
func (g *Group) TitleSafe() string {
if g.Title == "" {
return util.StringToTitle(g.Key)
}
return g.Title
}
func (g *Group) Proper() string {
return util.StringToCamel(g.Key)
}
func (g *Group) String() string {
if g.Title == "" {
return g.Key
}
return g.Title
}
type Groups []*Group
func (g Groups) Get(keys ...string) *Group {
if g == nil || len(keys) == 0 {
return nil
}
for _, x := range g {
if x.Key == keys[0] {
if len(keys) == 1 {
return x
}
return x.Children.Get(keys[1:]...)
}
}
return nil
}
func (g Groups) Flatten() Groups {
ret := slices.Clone(g)
for _, x := range g {
ret = append(ret, x.Children.Flatten()...)
}
return ret
}
func (g Groups) Strings(prefix string) []string {
return lo.Map(g, func(x *Group, _ int) string {
return prefix + x.Proper()
})
}