-
Notifications
You must be signed in to change notification settings - Fork 4
/
enum.go
171 lines (150 loc) · 3.67 KB
/
enum.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package enum
import (
"fmt"
"strings"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/lib/types"
"projectforge.dev/projectforge/app/util"
)
const defaultIcon = "hammer"
type Enum struct {
Name string `json:"name"`
Package string `json:"package"`
Group []string `json:"group,omitempty"`
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
Values Values `json:"values,omitempty"`
Tags []string `json:"tags,omitempty"`
TitleOverride string `json:"title,omitempty"`
ProperOverride string `json:"proper,omitempty"`
RouteOverride string `json:"route,omitempty"`
Config util.ValueMap `json:"config,omitempty"`
}
func (e *Enum) Title() string {
if e.TitleOverride == "" {
return util.StringToTitle(e.Name)
}
return e.TitleOverride
}
func (e *Enum) TitleLower() string {
return strings.ToLower(e.Title())
}
func (e *Enum) Proper() string {
if e.ProperOverride == "" {
return util.StringToCamel(e.Name)
}
return util.StringToCamel(e.ProperOverride)
}
func (e *Enum) ProperPlural() string {
ret := util.StringToPlural(e.Proper())
if ret == e.Proper() {
return ret + "Set"
}
return ret
}
func (e *Enum) FirstLetter() any {
return strings.ToLower(e.Name[0:1])
}
func (e *Enum) IconSafe() string {
if _, ok := util.SVGLibrary[e.Icon]; ok {
return e.Icon
}
return defaultIcon
}
func (e *Enum) Camel() string {
return util.StringToLowerCamel(e.Name)
}
func (e *Enum) ExtraFields() *util.OrderedMap[string] {
ret := util.NewOrderedMap[string](false, 0)
for _, v := range e.Values {
if v.Extra == nil {
continue
}
for _, k := range v.Extra.Order {
x := v.Extra.GetSimple(k)
if _, exists := ret.Get(k); exists {
continue
}
typ := ""
switch x.(type) {
case string:
typ = types.KeyString
case float64:
typ = types.KeyFloat
case int, int32, int64:
typ = types.KeyInt
case bool:
typ = types.KeyBool
}
if x := e.Config.GetStringOpt("type:" + k); x != "" {
switch x {
case types.KeyString:
typ = types.KeyString
case types.KeyInt:
typ = types.KeyInt
case types.KeyFloat:
typ = types.KeyFloat
case types.KeyBool:
typ = types.KeyBool
case types.KeyTimestamp:
typ = types.KeyTimestamp
default:
typ = "unknown config type [" + x + "]"
}
}
ret.Append(k, typ)
}
}
return ret
}
func (e *Enum) ExtraFieldValues(k string) ([]any, bool) {
ret := make([]any, 0, len(e.Values))
for _, v := range e.Values {
if v.Extra == nil {
continue
}
if x, ok := v.Extra.Get(k); ok && x != nil {
ret = append(ret, x)
}
}
return ret, len(lo.Uniq(ret)) == len(ret)
}
func (e *Enum) PackageWithGroup(prefix string) string {
if len(e.Group) == 0 {
if prefix != "" && !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
return prefix + e.Package
}
var x []string
if prefix != "" {
x = append(x, prefix)
}
x = append(x, e.Group...)
x = append(x, e.Package)
return strings.Join(x, "/")
}
func (e *Enum) HasTag(t string) bool {
return lo.Contains(e.Tags, t)
}
func (e *Enum) Breadcrumbs() string {
ret := lo.Map(e.Group, func(g string, _ int) string {
return fmt.Sprintf("%q", g)
})
ret = append(ret, fmt.Sprintf("%q", e.Package))
return strings.Join(ret, ", ")
}
func (e *Enum) ValuesCamel() []string {
return lo.Map(e.Values, func(x *Value, _ int) string {
return util.StringToCamel(x.Key)
})
}
func (e *Enum) Simple() bool {
return e.Values.AllSimple()
}
type Enums []*Enum
func (e Enums) Get(key string) *Enum {
return lo.FindOrElse(e, nil, func(x *Enum) bool {
return x.Name == key
})
}