forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
59 lines (48 loc) · 1.24 KB
/
option.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
package importer
import (
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/kun/any"
)
// UnmarshalJSON for json marshalJSON
func (option *Option) UnmarshalJSON(source []byte) error {
var data = map[string]interface{}{}
err := jsoniter.Unmarshal(source, &data)
if err != nil {
return err
}
new, err := OptionOf(data)
if err != nil {
return err
}
*option = *new
return nil
}
// OptionOf 解析配置
func OptionOf(data map[string]interface{}) (*Option, error) {
option := &Option{
UseTemplate: true,
ChunkSize: 500,
MappingPreview: PreviewAuto,
DataPreview: PreviewAuto,
}
if autoMatching, ok := data["useTemplate"].(bool); ok {
option.UseTemplate = autoMatching
}
chunkSize := any.Of(data["chunkSize"]).CInt()
if chunkSize > 0 && chunkSize < 2000 {
option.ChunkSize = chunkSize
}
if mappingPreview, ok := data["mappingPreview"].(string); ok {
option.MappingPreview = getPreviewOption(mappingPreview)
}
if dataPreview, ok := data["dataPreview"].(string); ok {
option.DataPreview = getPreviewOption(dataPreview)
}
return option, nil
}
func getPreviewOption(value string) string {
if value != PreviewAlways && value != PreviewAuto && value != PreviewNever {
return PreviewAuto
}
return value
}