forked from davyxu/tabtoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharefunc.go
99 lines (75 loc) · 2.09 KB
/
sharefunc.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
package gen
import (
"github.com/adamluo159/tabtoy/util"
"github.com/adamluo159/tabtoy/v3/model"
"github.com/ahmetb/go-linq"
"text/template"
)
var UsefulFunc = template.FuncMap{}
type TableIndices struct {
Table *model.DataTable
FieldInfo *model.TypeDefine
}
func KeyValueTypeNames(globals *model.Globals) (ret []string) {
linq.From(globals.IndexList).Where(func(raw interface{}) bool {
pragma := raw.(*model.IndexDefine)
return pragma.Kind == model.TableKind_KeyValue
}).Select(func(raw interface{}) interface{} {
pragma := raw.(*model.IndexDefine)
return pragma.TableType
}).Distinct().ToSlice(&ret)
return
}
func WrapSingleValue(globals *model.Globals, valueType *model.TypeDefine, value string) string {
switch {
case valueType.FieldType == "string": // 字符串
return util.StringWrap(util.StringEscape(value))
case valueType.FieldType == "float":
if value == "" {
return model.FetchDefaultValue(valueType.FieldType)
}
return value
case globals.Types.IsEnumKind(valueType.FieldType): // 枚举
return globals.Types.ResolveEnumValue(valueType.FieldType, value)
case valueType.FieldType == "bool":
v, _ := model.ParseBool(value)
if v {
return "true"
}
return "false"
}
if value == "" {
return model.FetchDefaultValue(valueType.FieldType)
}
return value
}
func GetIndicesByTable(tab *model.DataTable) (ret []TableIndices) {
// 遍历输入数据的每一列
for _, header := range tab.Headers {
// 输入的列头
if header.TypeInfo == nil {
continue
}
if header.TypeInfo.MakeIndex {
ret = append(ret, TableIndices{
Table: tab,
FieldInfo: header.TypeInfo,
})
}
}
return
}
func GetIndices(globals *model.Globals) (ret []TableIndices) {
for _, tab := range globals.Datas.AllTables() {
ret = append(ret, GetIndicesByTable(tab)...)
}
return
}
func init() {
UsefulFunc["HasKeyValueTypes"] = func(globals *model.Globals) bool {
return len(KeyValueTypeNames(globals)) > 0
}
UsefulFunc["GetKeyValueTypeNames"] = KeyValueTypeNames
UsefulFunc["GetIndicesByTable"] = GetIndicesByTable
UsefulFunc["GetIndices"] = GetIndices
}