-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlsx_row_parser.go
178 lines (168 loc) · 4.2 KB
/
xlsx_row_parser.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
package xlsxcfg
import (
"context"
"fmt"
"strconv"
)
type rowParser struct {
param *Config
isStr func(fieldPath string) bool
errStack []error
hasMeta bool
res map[string]any
set []func(v string)
fnGet map[string]func() any
fnSet map[string]func(v any)
}
func newRowParser(typeName string, param *Config) *rowParser {
return &rowParser{
param: param,
isStr: func(fieldPath string) bool {
return param.IsStrField(typeName, fieldPath)
},
}
}
// Meta feed metadata row to the parser.
func (p *rowParser) Meta(ctx context.Context, row []string) {
p.fnGet = make(map[string]func() any, len(row))
p.fnSet = make(map[string]func(v any), len(row))
p.set = make([]func(v string), 0, len(row))
p.res = make(map[string]any)
p.fnGet[""] = func() any { return p.res } // top level getter
for _, meta := range row {
if meta == "" {
p.set = append(p.set, func(_ string) {})
continue
}
tr := newTokenReader(meta)
for tr.Next() {
ident := tr.Ident()
prevIdent := tr.FullPrev()
fullIdent := tr.FullIdent()
typePath := tr.TypePath()
// log.Printf("head: %s -- ident: %s -- prevIdent: %s -- fullIdent: %s -- typePath: %s\n", hd, ident, prevIdent, fullIdent, typePath)
if ai := tr.ListIndex(); ai >= 0 {
p.metaList(ident, prevIdent, fullIdent, typePath, ai-1, !tr.HasNext())
} else if tr.HasNext() {
p.metaStruct(ident, prevIdent, fullIdent, typePath)
} else {
p.metaField(ident, prevIdent, fullIdent, typePath)
}
}
}
p.hasMeta = true
}
// Parse parse one data row, MUST called after feed metadata.
func (p *rowParser) Parse(ctx context.Context, row []string) (map[string]any, error) {
if !p.hasMeta {
return nil, fmt.Errorf("row parser no metadata")
}
p.res = make(map[string]any)
ni := len(row) - 1
for ; ni >= 0; ni-- {
if row[ni] != "" {
break
}
}
if ni < 0 {
return nil, nil
}
row = row[:ni+1]
for i, cell := range row {
if i < len(p.set) {
p.set[i](cell)
}
}
if len(p.errStack) > 0 {
return nil, p.errStack[0]
}
return p.res, nil
}
func (p *rowParser) convertVal(typePath, fullIdent, v string) any {
var res any
if p.isStr(typePath) {
res = v
} else {
if v == "" {
v = "0"
}
n, e := strconv.ParseInt(v, 10, 64)
if e != nil {
p.errStack = append(p.errStack, fmt.Errorf("parse col[%s] as number failed: %v", fullIdent, e))
} else {
res = n
}
}
return res
}
func (p *rowParser) metaList(ident, prevIdent, fullIdent, typePath string, idx int, isEnd bool) {
p.fnGet[fullIdent] = func() any {
list := p.fnGet[prevIdent]().(map[string]any)[ident]
if list == nil || len(list.([]any)) <= idx {
if list != nil {
oldList := list.([]any)
newList := make([]any, idx+1)
copy(newList, oldList)
list = newList
} else {
list = make([]any, idx+1)
}
p.fnGet[prevIdent]().(map[string]any)[ident] = list
}
return list.([]any)[idx]
}
p.fnSet[fullIdent] = func(v any) {
list := p.fnGet[prevIdent]().(map[string]any)[ident]
if list == nil || len(list.([]any)) <= idx {
if list != nil {
oldList := list.([]any)
newList := make([]any, idx+1)
copy(newList, oldList)
list = newList
} else {
list = make([]any, idx+1)
}
p.fnGet[prevIdent]().(map[string]any)[ident] = list
}
list.([]any)[idx] = v
}
if !isEnd {
return
}
// end at list item
p.set = append(p.set, func(v string) {
// log.Printf("arr set [%s] to [%s]\n", fullIdent, v)
if v == "" {
return
}
p.fnSet[fullIdent](p.convertVal(typePath, fullIdent, v))
})
}
func (p *rowParser) metaStruct(ident, prevIdent, fullIdent, typePath string) {
p.fnGet[fullIdent] = func() any {
return p.fnGet[prevIdent]().(map[string]any)[ident]
}
p.fnSet[fullIdent] = func(v any) {
prev := p.fnGet[prevIdent]()
if prev == nil {
prev = map[string]any{}
p.fnSet[prevIdent](prev)
}
prev.(map[string]any)[ident] = v
}
}
func (p *rowParser) metaField(ident, prevIdent, fullIdent, typePath string) {
// end at a field
p.set = append(p.set, func(v string) {
// log.Printf("set [%s] to [%s]\n", fullIdent, v)
if v == "" {
return
}
prev := p.fnGet[prevIdent]()
if prev == nil {
prev = map[string]any{}
p.fnSet[prevIdent](prev)
}
prev.(map[string]any)[ident] = p.convertVal(typePath, fullIdent, v)
})
}