-
Notifications
You must be signed in to change notification settings - Fork 4
/
columngo.go
155 lines (146 loc) · 5 KB
/
columngo.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
package model
import (
"fmt"
"strings"
"projectforge.dev/projectforge/app/lib/types"
"projectforge.dev/projectforge/app/project/export/enum"
"projectforge.dev/projectforge/app/util"
)
const msgTextarea = `{%%%%= components.TableTextarea(%q, %q, %q, 8, util.ToJSON(%s), 5, %q) %%%%}`
func (c *Column) ToSQLType() (string, error) {
ret, err := ToSQLType(c.Type)
if err != nil {
return "", err
}
if !c.Nullable {
ret += " not null"
}
if c.SQLDefault != "" {
ret += " default " + c.SQLDefault
}
return ret, nil
}
func (c *Column) ToGoEditString(prefix string, format string, id string, enums enum.Enums) (string, error) {
h, err := c.Help(enums)
if err != nil {
return "", err
}
switch c.Type.Key() {
case types.KeyAny:
return fmt.Sprintf(msgTextarea, c.Camel(), id, c.Title(), c.ToGoString(prefix), h), nil
case types.KeyBool:
return fmt.Sprintf(`{%%%%= components.TableBoolean(%q, %q, %s, 5, %q) %%%%}`, c.Camel(), c.Title(), prefix+c.Proper(), h), nil
// case types.KeyEnum:
// return fmt.Sprintf(`{%%%%= components.TableInput(%q, %q, string(%s), 5, %q) %%%%}`, c.Camel(), c.Title(), c.ToGoString(prefix), h), nil
case types.KeyEnum:
e, err := AsEnumInstance(c.Type, enums)
if err != nil {
return "", err
}
eRef := strings.Join(util.StringArrayQuoted(e.Values), ", ")
msg := `{%%%%= components.TableSelect(%q, %q, %q, string(%s), []string{%s}, []string{%s}, 5, %q) %%%%}`
return fmt.Sprintf(msg, c.Camel(), id, c.Title(), c.ToGoString(prefix), eRef, eRef, h), nil
case types.KeyInt:
return fmt.Sprintf(`{%%%%= components.TableInputNumber(%q, %q, %q, %s, 5, %q) %%%%}`, c.Camel(), id, c.Title(), prefix+c.Proper(), h), nil
case types.KeyFloat:
return fmt.Sprintf(`{%%%%= components.TableInputFloat(%q, %q, %q, %s, 5, %q) %%%%}`, c.Camel(), id, c.Title(), prefix+c.Proper(), h), nil
case types.KeyList, types.KeyMap, types.KeyValueMap:
return fmt.Sprintf(msgTextarea, c.Camel(), id, c.Title(), c.ToGoString(prefix), h), nil
case types.KeyReference:
return fmt.Sprintf(msgTextarea, c.Camel(), id, c.Title(), prefix+c.Proper(), h), nil
case types.KeyDate:
gs := c.ToGoString(prefix)
if !c.Nullable {
gs = "&" + gs
}
return fmt.Sprintf(`{%%%%= components.TableInputTimestampDay(%q, %q, %q, %s, 5, %q) %%%%}`, c.Camel(), id, c.Title(), gs, h), nil
case types.KeyTimestamp:
gs := c.ToGoString(prefix)
if !c.Nullable {
gs = "&" + gs
}
return fmt.Sprintf(`{%%%%= components.TableInputTimestamp(%q, %q, %q, %s, 5, %q) %%%%}`, c.Camel(), id, c.Title(), gs, h), nil
case types.KeyUUID:
gs := prefix + c.Proper()
if !c.Nullable {
gs = "&" + gs
}
return fmt.Sprintf(`{%%%%= components.TableInputUUID(%q, %q, %q, %s, 5, %q) %%%%}`, c.Camel(), id, c.Title(), gs, h), nil
case types.KeyString:
switch format {
case FmtCode, FmtCodeHidden, FmtHTML:
return fmt.Sprintf(`{%%%%= components.TableTextarea(%q, %q, %q, 8, %s, 5, %q) %%%%}`, c.Camel(), id, c.Title(), c.ToGoString(prefix), h), nil
case FmtSelect:
if len(c.Values) == 0 {
return fmt.Sprintf(`{%%%%= components.TableInput(%q, %q, %q, %s, 5, %q) %%%%}`, c.Camel(), id, c.Title(), c.ToGoString(prefix), h), nil
}
sel := `{%%%%= components.TableSelect(%q, "", %q, %s, %s, nil, 5, %q) %%%%}`
opts := "[]string{" + strings.Join(util.StringArrayQuoted(c.Values), ", ") + "}"
return fmt.Sprintf(sel, c.Camel(), c.Title(), c.ToGoString(prefix), opts, h), nil
default:
return fmt.Sprintf(`{%%%%= components.TableInput(%q, %q, %q, %s, 5, %q) %%%%}`, c.Camel(), id, c.Title(), c.ToGoString(prefix), h), nil
}
default:
return fmt.Sprintf(`{%%%%= components.TableInput(%q, %q, %q, %s, 5, %q) %%%%}`, c.Camel(), id, c.Title(), c.ToGoString(prefix), h), nil
}
}
func (c *Column) ToGoMapParse() string {
return toGoMapParse(c.Type)
}
func toGoMapParse(t types.Type) string {
switch t.Key() {
case types.KeyAny:
return "Interface"
case types.KeyBool:
return "Bool"
case types.KeyInt:
return "Int"
case types.KeyFloat:
return "Float"
case types.KeyList:
l := types.TypeAs[*types.List](t)
if l == nil {
return fmt.Sprintf("ERROR:invalid list type [%T]", t)
}
return "Array" + toGoMapParse(l.V)
case types.KeyMap, types.KeyValueMap:
return "Map"
case types.KeyReference:
return asRefK(t)
case types.KeyString, types.KeyEnum:
return "String"
case types.KeyDate, types.KeyTimestamp:
return "Time"
case types.KeyUUID:
return "UUID"
default:
return "ERROR:unhandled map parse for type [" + t.Key() + "]"
}
}
func (c *Column) ZeroVal() string {
if c.Nullable {
return types.KeyNil
}
switch c.Type.Key() {
case types.KeyAny:
return types.KeyNil
case types.KeyBool:
return "false"
case types.KeyList:
return types.KeyNil
case types.KeyInt:
return "0"
case types.KeyFloat:
return "0.0"
case types.KeyMap, types.KeyValueMap, types.KeyReference:
return types.KeyNil
case types.KeyString:
return `""`
case types.KeyDate, types.KeyTimestamp:
return "time.Time{}"
case types.KeyUUID:
return "uuid.UUID{}"
default:
return "ERROR:unhandled zero value for type [" + c.Type.Key() + "]"
}
}