-
Notifications
You must be signed in to change notification settings - Fork 4
/
typeview.go
105 lines (99 loc) · 3.05 KB
/
typeview.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
package model
import (
"fmt"
"projectforge.dev/projectforge/app/lib/types"
"projectforge.dev/projectforge/app/project/export/enum"
"projectforge.dev/projectforge/app/util"
)
func ToGoViewString(t *types.Wrapped, prop string, nullable bool, format string, verbose bool, url bool, enums enum.Enums, src string) string {
switch t.Key() {
case types.KeyAny:
if src == util.KeySimple {
return tmplStartV + prop + tmplEnd
}
return jsonGoViewString(prop)
case types.KeyBool:
return tmplStartV + prop + tmplEnd
case types.KeyInt:
switch format {
case FmtSI.Key:
return tmplStartS + fmt.Sprintf("util.ByteSizeSI(int64(%s))", prop) + tmplEnd
case FmtSeconds.Key:
return tmplStartS + fmt.Sprintf("view.DurationSeconds(float64(%s))", prop) + tmplEnd
case "":
return tmplStart + "d " + prop + tmplEnd
default:
return "INVALID_INT_FORMAT[" + format + "]"
}
case types.KeyFloat:
switch format {
case FmtSeconds.Key:
return tmplStartEQ + fmt.Sprintf("view.DurationSeconds(%s)", prop) + tmplEnd
case "":
return tmplStart + "f " + prop + tmplEnd
default:
return "INVALID_FLOAT_FORMAT[" + format + "]"
}
case types.KeyList:
return listGoViewString(t, prop, src, enums)
case types.KeyMap, types.KeyValueMap, types.KeyReference:
if src == util.KeySimple {
return tmplStartV + prop + " %%}"
}
return jsonGoViewString(prop)
case types.KeyDate:
if nullable {
return tmplStartEQ + "view.TimestampDay(" + prop + tmplEndP
}
return tmplStartEQ + "view.TimestampDay(&" + prop + tmplEndP
case types.KeyEnum:
e, _ := AsEnumInstance(t, enums)
if e == nil || e.Simple() {
return tmplStartV + ToGoString(t, nullable, prop, false) + tmplEnd
}
return tmplStartS + ToGoString(t, nullable, prop, false) + ".String()" + tmplEnd
case types.KeyTimestamp:
if nullable {
return tmplStartEQ + "view.Timestamp(" + prop + tmplEndP
}
return tmplStartEQ + "view.Timestamp(&" + prop + tmplEndP
case types.KeyUUID:
if nullable {
return tmplStartEQ + "view.UUID(" + prop + tmplEndP
}
return tmplStartEQ + "view.UUID(&" + prop + tmplEndP
case types.KeyString:
return goViewStringForString(url, src, t, nullable, prop, format, verbose)
default:
return tmplStartV + ToGoString(t, nullable, prop, false) + tmplEnd
}
}
func jsonGoViewString(prop string) string {
return tmplStartEQ + "components.JSON(" + prop + tmplEndP
}
func saGoViewString(x string) string {
return tmplStartEQ + "view.StringArray(" + x + tmplEndP
}
func listGoViewString(t *types.Wrapped, prop string, src string, enums enum.Enums) string {
if src == util.KeySimple {
return tmplStartV + prop + tmplEnd
}
lt := t.ListType()
if lt == nil {
lt = types.NewString()
}
switch lt.Key() {
case types.KeyString:
return saGoViewString(prop)
case types.KeyInt:
return tmplStartEQ + fmt.Sprintf("view.IntArray(util.ArrayFromAny(%s))", prop) + tmplEnd
case types.KeyEnum:
e, _ := AsEnumInstance(lt, enums)
if e == nil {
return "ERROR: invalid enum [" + lt.String() + "]"
}
return saGoViewString(prop + stringsSuffix)
default:
return jsonGoViewString(prop)
}
}