Repository navigation
Expand file tree
/
Copy pathconf.go
More file actions
103 lines (89 loc) · 2.07 KB
/
Copy pathconf.go
File metadata and controls
103 lines (89 loc) · 2.07 KB
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/go-sql-driver/mysql"
)
// Col xls column
type Col struct {
Column string `json:"column"` // db column name
Format string `json:"format"` // data format : int,float,string,datetime
cellIdx int
isKey bool
}
func (c *Col) String() string {
return fmt.Sprintf("column=%s format=%s cell_idx=%d", c.Column, c.Format, c.cellIdx)
}
// DefaultData get col default data
func (c *Col) DefaultData() interface{} {
switch c.Format {
case "int":
return 0
case "float":
return 0.0
case "string":
return ""
case "datetime":
return mysql.NullTime{}
}
return nil
}
// SheetConf xls sheet config
type SheetConf struct {
CheckURL string `json:"check_url"`
Reload string `json:"reload"`
Table string `json:"table"`
Keys []string `json:"keys"`
HeadLine int `json:"head_line"`
Cols map[string]*Col `json:"cols"` // key is xls column name
}
// SheetConfs sheetconfig list
type SheetConfs map[string]*SheetConf // key is sheet name
/*
func (c *SheetConf) isKey(name string) bool {
for _, key := range c.Keys {
if name == key {
return true
}
}
return false
}
*/
// ReadSheetConf read xls sheet conf
func ReadSheetConf(path string, sheets []string) (SheetConfs, error) {
dat, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("invalid sheet config path=%s err=%s", path, err)
}
var src SheetConfs
if err := json.Unmarshal(dat, &src); err != nil {
return nil, fmt.Errorf("config parse error path=%s err=%s", path, err)
}
// check and mark key
for n, s := range src {
for _, k := range s.Keys {
exist := false
for _, c := range s.Cols {
if c.Column == k {
exist = true
c.isKey = true
break
}
}
if !exist {
return nil, fmt.Errorf("not found key from col list sheet=%s key=%s", n, k)
}
}
}
if len(sheets) == 0 || sheets[0] == "all" {
return src, nil
}
result := make(SheetConfs)
for _, s := range sheets {
if dst, exist := src[s]; exist {
result[s] = dst
}
}
return result, nil
}