-
Notifications
You must be signed in to change notification settings - Fork 4
/
values.go
83 lines (73 loc) · 1.98 KB
/
values.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
package goenum
import (
"fmt"
"strings"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/lib/types"
"projectforge.dev/projectforge/app/project/export/enum"
"projectforge.dev/projectforge/app/project/export/files/helper"
"projectforge.dev/projectforge/app/project/export/golang"
"projectforge.dev/projectforge/app/util"
)
func enumValues(e *enum.Enum) *golang.Block {
names := lo.Map(e.Values, func(x *enum.Value, _ int) string {
return e.Proper() + x.Proper()
})
b := golang.NewBlock(e.Proper(), "vars")
if e.Simple() {
b.W("var All%s = %s{%s}", e.ProperPlural(), e.ProperPlural(), strings.Join(names, ", "))
return b
}
b.W("var (")
maxCount := util.StringArrayMaxLength(e.ValuesCamel())
pl := len(e.Proper())
maxColLength := maxCount + pl
lo.ForEach(e.Values, func(v *enum.Value, _ int) {
b.W(enumValue(e, v, maxColLength))
})
b.WB()
b.W("\tAll%s = %s{%s}", e.ProperPlural(), e.ProperPlural(), strings.Join(names, ", "))
b.W(")")
return b
}
func enumValue(e *enum.Enum, v *enum.Value, maxColLength int) string {
msg := fmt.Sprintf("\t%s = %s{Key: %q", util.StringPad(e.Proper()+v.Proper(), maxColLength), e.Proper(), v.Key)
if v.Name != "" {
msg += fmt.Sprintf(", Name: %q", v.Name)
}
if v.Description != "" {
msg += fmt.Sprintf(", Description: %q", v.Description)
}
if v.Icon != "" {
msg += fmt.Sprintf(", Icon: %q", v.Icon)
}
ef := e.ExtraFields()
for _, extraKey := range ef.Order {
var t string
if v.Extra != nil {
t = fmt.Sprint(v.Extra.GetSimple(extraKey))
}
switch ef.GetSimple(extraKey) {
case types.KeyString, "":
if t == "" {
continue
}
t = "\"" + t + "\""
case types.KeyTimestamp:
if t == helper.TextNil || t == "" {
continue
}
t = "util.TimeFromStringSimple(\"" + t + "\")"
case types.KeyBool:
if t == helper.TextNil {
t = util.BoolFalse
}
if t == util.BoolFalse || t == "" {
continue
}
}
msg += fmt.Sprintf(", %s: %s", util.StringToCamel(extraKey), t)
}
msg += "}"
return msg
}