-
Notifications
You must be signed in to change notification settings - Fork 664
/
column.go
212 lines (174 loc) · 4.03 KB
/
column.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package importer
import (
"fmt"
"strings"
jsoniter "github.com/json-iterator/go"
)
// MarshalJSON for json marshalJSON
func (column Column) MarshalJSON() ([]byte, error) {
data := column.ToMap()
return jsoniter.Marshal(data)
}
// UnmarshalJSON for json marshalJSON
func (column *Column) UnmarshalJSON(source []byte) error {
var data = map[string]interface{}{}
err := jsoniter.Unmarshal(source, &data)
if err != nil {
return err
}
new, err := ColumnOf(data)
if err != nil {
return err
}
*column = *new
return nil
}
// ColumnOf 映射表转换为字段定义
func ColumnOf(data map[string]interface{}) (*Column, error) {
var column = &Column{}
err := column.setLabel(data)
if err != nil {
return nil, err
}
err = column.setName(data)
if err != nil {
return nil, err
}
err = column.setMatch(data)
if err != nil {
return nil, err
}
err = column.setRules(data)
if err != nil {
return nil, err
}
if primary, ok := data["primary"].(bool); ok {
column.Primary = primary
}
if nullable, ok := data["nullable"].(bool); ok {
column.Nullable = nullable
}
return column, nil
}
// ToMap 转换为映射表
func (column Column) ToMap() map[string]interface{} {
data := map[string]interface{}{
"name": column.Field,
"label": column.Label,
"match": column.Match,
"rules": column.Rules,
}
if column.Nullable {
data["nullable"] = true
}
if column.Primary {
data["primary"] = true
}
return data
}
// setRules 设置清洗规则
func (column *Column) setRules(data map[string]interface{}) error {
rules, err := GetArrayString(data, "rules")
if err != nil {
return err
}
// 检查 process 是否存在
column.Rules = rules
return nil
}
// setLabel 读取并设置字段标签
func (column *Column) setLabel(data map[string]interface{}) error {
label, err := GetString(data, "label", true)
if err != nil {
return err
}
column.Label = label
return nil
}
// setMatch 读取并设置字段名称
func (column *Column) setMatch(data map[string]interface{}) error {
match, err := GetArrayString(data, "match")
if err != nil {
return err
}
column.Match = match
return nil
}
// setName 读取并设置字段名称
func (column *Column) setName(data map[string]interface{}) error {
name, err := GetString(data, "name", true)
if err != nil {
return err
}
column.Field = name // 留存原始数值
if strings.Contains(name, "[*]") { // Array
namer := strings.Split(name, "[*]")
name = namer[0]
column.IsArray = true
name = strings.Join(namer, "")
}
if strings.Contains(name, ".") { // Object
namer := strings.Split(name, ".")
name = namer[0]
if len(namer) > 1 {
column.IsObject = true
column.Key = strings.Join(namer[1:], ".")
}
}
column.Name = name
return nil
}
// GetString 读取字符串格式
func GetString(data map[string]interface{}, key string, required bool) (string, error) {
value, ok := data[key].(string)
if !ok {
if bytes, isok := data[key].([]byte); isok {
ok = isok
value = string(bytes)
}
}
if !ok || (value == "" && required) {
return "", ErrorF("the %s format is incorrect", key)
}
return value, nil
}
// GetArrayString 读取字符串数组
func GetArrayString(data map[string]interface{}, key string) ([]string, error) {
value := []string{}
if data[key] == nil {
return value, nil
}
if v, ok := data[key].(string); ok {
return []string{v}, nil
}
value, ok := data[key].([]string)
if !ok {
if anys, isok := data[key].([]interface{}); isok {
ok = isok
for _, any := range anys {
value = append(value, fmt.Sprintf("%v", any))
}
}
}
if !ok {
if anys, isok := data[key].([][]byte); isok {
ok = isok
for _, any := range anys {
value = append(value, string(any))
}
}
}
if !ok {
return nil, ErrorF("the %s format is incorrect", key)
}
return value, nil
}
// ErrorF 返回错误数据对象
func ErrorF(format string, data ...interface{}) error {
values := []interface{}{}
for _, value := range data {
v, _ := jsoniter.Marshal(value)
values = append(values, v)
}
return fmt.Errorf(format, values...)
}