-
Notifications
You must be signed in to change notification settings - Fork 4
/
value.go
117 lines (101 loc) · 2.81 KB
/
value.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
package enum
import (
"strings"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/util"
)
var ValueFieldDescs = util.FieldDescs{
{Key: "key", Title: "Key", Description: "The key of the enum"},
{Key: "name", Title: "Name", Description: "The name of the enum"},
{Key: "description", Title: "Description", Description: "The description of the enum"},
{Key: "icon", Title: "Icon", Description: "The icon of the enum", Type: "icon"},
{Key: "default", Title: "Default", Description: "Indicates if this is the default value", Type: "bool"},
// {Key: "extra", Title: "Extra", Description: "The X of the column"},
}
type valueMarshal struct {
Key string `json:"key"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
Default bool `json:"default,omitempty"`
Extra *util.OrderedMap[any] `json:"extra,omitempty"`
}
type Value struct {
Key string
Name string
Description string
Icon string
Extra *util.OrderedMap[any]
Default bool
Simple bool
}
func (x *Value) Proper() string {
return util.StringToCamel(x.Key)
}
func (x *Value) Title() string {
return util.StringToTitle(x.Key)
}
func (x *Value) MarshalJSON() ([]byte, error) {
if x.Simple {
return util.ToJSONBytes(x.Key, false), nil
}
marshaller := &valueMarshal{Key: x.Key, Name: x.Name, Description: x.Description, Icon: x.Icon, Extra: x.Extra, Default: x.Default}
return util.ToJSONBytes(marshaller, false), nil
}
func (x *Value) UnmarshalJSON(data []byte) error {
if strings.Contains(string(data), "{") {
var v valueMarshal
if err := util.FromJSON(data, &v); err != nil {
return err
}
x.Key = v.Key
x.Name = v.Name
x.Description = v.Description
x.Icon = v.Icon
x.Extra = v.Extra
x.Default = v.Default
x.Simple = false
return nil
}
str := ""
if err := util.FromJSON(data, &str); err != nil {
return err
}
x.Key = str
x.Name = ""
x.Description = ""
x.Icon = ""
x.Extra = nil
x.Default = false
x.Simple = true
return nil
}
type Values []*Value
func (v Values) Keys() []string {
return lo.Map(v, func(x *Value, _ int) string {
return x.Key
})
}
func (v Values) Names() []string {
return lo.Map(v, func(x *Value, _ int) string {
return x.Name
})
}
func (v Values) AllSimple() bool {
return !lo.ContainsBy(v, func(x *Value) bool {
return (!x.Simple) || x.Name != "" || x.Description != "" || x.Icon != "" || (x.Extra != nil && len(x.Extra.Map) > 0)
})
}
func (v Values) Default() *Value {
return lo.FindOrElse(v, nil, func(x *Value) bool {
return x.Default
})
}
func (v Values) Titles() []string {
return lo.Map(v, func(x *Value, _ int) string {
if x.Name != "" {
return x.Title()
}
return x.Key
})
}