-
Notifications
You must be signed in to change notification settings - Fork 40
/
schema.go
224 lines (207 loc) · 5.09 KB
/
schema.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
213
214
215
216
217
218
219
220
221
222
223
224
package excel
import (
"encoding/json"
"reflect"
"strings"
)
const (
tagIdentify = "xlsx"
tagSplit = ";"
encodingTag = "encoding"
columnTag = "column"
splitTag = "split"
defaultTag = "default"
nilTag = "nil"
ignoreTag = "-"
reqTag = "req"
)
type FieldConfig struct {
// The config equals to tag: column
ColumnName string
// The config equals to tag: default
DefaultValue string
// The config equals to tag: split
Split string
// The config equals to tag: decode
Encoding string
// The config equals to tag: nil
// if cell.value == NilValue, will skip fc scan
NilValue string
// The config equals to tag: req
// panic if reuqired fc column but not set
IsRequired bool
// The config equals to tag: -
Ignore bool
}
func (this *FieldConfig) froze(fieldIdx int) *fieldConfig {
return &fieldConfig{
FieldIndex: fieldIdx,
ColumnName: this.ColumnName,
DefaultValue: this.DefaultValue,
Split: this.Split,
Encoding: this.Encoding,
NilValue: this.NilValue,
IsRequired: this.IsRequired,
}
}
type ExcelFiledConfiger interface {
GetXLSXFieldConfigs() map[string]FieldConfig
}
type fieldConfig struct {
FieldIndex int
// use ptr in order to know if configed.
ColumnName string
DefaultValue string
Split string
// decode column string as encoding type
Encoding string
// if cell.value == NilValue, will skip fc scan
NilValue string
// panic if reuqired fc column but not set
IsRequired bool
}
func (fc *fieldConfig) scan(valStr string, fieldValue reflect.Value) error {
if fc.NilValue == valStr {
// log.Printf("Got nil,skip")
return nil
}
var err error
switch fieldValue.Kind() {
case reflect.Slice, reflect.Array:
if len(fc.Split) != 0 && len(valStr) > 0 {
// use split
elems := strings.Split(valStr, fc.Split)
fieldValue.Set(reflect.MakeSlice(fieldValue.Type(), 0, len(elems)))
err = scanSlice(elems, fieldValue.Addr())
} else {
// 如果标识是一个JSON
switch fc.Encoding {
case "json":
err = json.Unmarshal([]byte(valStr), fieldValue.Addr().Interface())
}
}
case reflect.Ptr:
newValue := fieldValue
if newValue.IsNil() {
for newValue.Kind() == reflect.Ptr {
newValue.Set(reflect.New(newValue.Type().Elem()))
newValue = newValue.Elem()
}
}
// 如果标识是一个JSON
switch fc.Encoding {
case "json":
err = json.Unmarshal([]byte(valStr), newValue.Addr().Interface())
default:
err = scan(valStr, newValue.Addr().Interface())
}
default:
switch fc.Encoding {
case "json":
err = json.Unmarshal([]byte(valStr), fieldValue.Addr().Interface())
default:
err = scan(valStr, fieldValue.Addr().Interface())
}
}
return err
}
func (fc *fieldConfig) ScanDefault(fieldValue reflect.Value) error {
err := fc.scan(fc.DefaultValue, fieldValue)
if err != nil && len(fc.DefaultValue) > 0 {
return err
}
return nil
}
type schema struct {
Type reflect.Type
// map[FieldIndex]*Field
Fields []*fieldConfig
}
func newSchema(t reflect.Type) *schema {
s := &schema{
Fields: make([]*fieldConfig, 0, t.NumField()),
}
// if implement the ExcelFiledConfiger
var selfDefinedCfgs map[string]FieldConfig
v := reflect.New(t)
if v.CanInterface() {
if i, ok := v.Interface().(ExcelFiledConfiger); ok {
selfDefinedCfgs = i.GetXLSXFieldConfigs()
}
} else if vElem := v.Elem(); vElem.CanInterface() {
if i, ok := vElem.Interface().(ExcelFiledConfiger); ok {
selfDefinedCfgs = i.GetXLSXFieldConfigs()
}
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if selfCfg, ok := selfDefinedCfgs[field.Name]; ok {
// Use self defiend config first
if !selfCfg.Ignore {
frzCfg := selfCfg.froze(i)
if frzCfg.ColumnName == "" {
frzCfg.ColumnName = field.Name
}
s.Fields = append(s.Fields, frzCfg)
}
} else if value, ok := field.Tag.Lookup(tagIdentify); ok {
// Use tag second
if value != ignoreTag {
fieldCnf := praseTagValue(value)
fieldCnf.FieldIndex = i
if fieldCnf.ColumnName == "" {
fieldCnf.ColumnName = field.Name
}
s.Fields = append(s.Fields, fieldCnf)
}
} else {
// use default config
fieldCnf := &fieldConfig{
FieldIndex: i,
ColumnName: field.Name,
}
s.Fields = append(s.Fields, fieldCnf)
}
}
s.Type = t
return s
}
func praseTagValue(v string) *fieldConfig {
c := &fieldConfig{}
params := strings.Split(v, tagSplit)
for _, param := range params {
if param == "" {
continue
}
cnfKey, cnfVal := getTagParam(param)
fillField(c, cnfKey, cnfVal)
}
// with more params
return c
}
func getTagParam(v string) (key, value string) {
// expect v = `field_name` or `column(fieldName)` or `default(0)` and so on ...
start := strings.Index(v, "(")
end := strings.Index(v, ")")
if start > 0 && end == len(v)-1 {
return v[:start], v[start+1 : end]
}
// log.Printf("Use column as default?[%s]\n", v)
return columnTag, v
}
func fillField(c *fieldConfig, k, v string) {
switch k {
case columnTag:
c.ColumnName = v
case defaultTag:
c.DefaultValue = v
case splitTag:
c.Split = v
case encodingTag:
c.Encoding = v
case nilTag:
c.NilValue = v
case reqTag:
c.IsRequired = true
}
}