-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse.go
388 lines (362 loc) · 10.2 KB
/
parse.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package pagser
import (
"fmt"
"io"
"reflect"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/spf13/cast"
)
// Parse parse html to struct
func (p *Pagser) Parse(v interface{}, document string) (err error) {
reader, err := goquery.NewDocumentFromReader(strings.NewReader(document))
if err != nil {
return err
}
return p.ParseDocument(v, reader)
}
// ParseReader parse html to struct
func (p *Pagser) ParseReader(v interface{}, reader io.Reader) (err error) {
doc, err := goquery.NewDocumentFromReader(reader)
if err != nil {
return err
}
return p.ParseDocument(v, doc)
}
// ParseDocument parse document to struct
func (p *Pagser) ParseDocument(v interface{}, document *goquery.Document) (err error) {
return p.ParseSelection(v, document.Selection)
}
// ParseSelection parse selection to struct
func (p *Pagser) ParseSelection(v interface{}, selection *goquery.Selection) (err error) {
return p.doParse(v, nil, selection)
}
// ParseSelection parse selection to struct
func (p *Pagser) doParse(v interface{}, stackRefValues []reflect.Value, selection *goquery.Selection) (err error) {
objRefType := reflect.TypeOf(v)
objRefValue := reflect.ValueOf(v)
//log.Printf("%#v kind is %v | %v", v, objRefValue.Kind(), reflect.Ptr)
if objRefValue.Kind() != reflect.Ptr {
return fmt.Errorf("%v is non-pointer", objRefType)
}
if objRefValue.IsNil() {
return fmt.Errorf("%v is nil", objRefType)
}
objRefTypeElem := objRefType.Elem()
objRefValueElem := objRefValue.Elem()
for i := 0; i < objRefValueElem.NumField(); i++ {
fieldType := objRefTypeElem.Field(i)
fieldValue := objRefValueElem.Field(i)
kind := fieldType.Type.Kind()
//tagValue := fieldType.Tag.Get(parserTagName)
tagValue, tagOk := fieldType.Tag.Lookup(p.Config.TagName)
if !tagOk {
if p.Config.Debug {
fmt.Printf("[INFO] not found tag name=[%v] in field: %v, eg: `%v:\".navlink a->attr(href)\"`\n",
p.Config.TagName, fieldType.Name, p.Config.TagName)
}
continue
}
if tagValue == ignoreSymbol {
continue
}
cacheTag, ok := p.mapTags.Load(tagValue)
var tag *tagTokenizer
if !ok || cacheTag == nil {
tag, err = p.newTag(tagValue)
if err != nil {
return err
}
p.mapTags.Store(tagValue, tag)
} else {
tag = cacheTag.(*tagTokenizer)
}
node := selection
if tag.Selector != "" {
node = selection.Find(tag.Selector)
}
var callOutValue interface{}
var callErr error
if tag.FuncName != "" {
callOutValue, callErr = p.findAndExecFunc(objRefValue, stackRefValues, tag, node)
if callErr != nil {
return fmt.Errorf("tag=`%v` parse func error: %v", tagValue, callErr)
}
if subNode, ok := callOutValue.(*goquery.Selection); ok {
//set sub node to current node
node = subNode
} else {
svErr := p.setRefectValue(fieldType.Type.Kind(), fieldValue, callOutValue)
if svErr != nil {
return fmt.Errorf("tag=`%v` set value error: %v", tagValue, svErr)
}
//goto parse next field
continue
}
}
if stackRefValues == nil {
stackRefValues = make([]reflect.Value, 0)
}
stackRefValues = append(stackRefValues, objRefValue)
//set value
switch {
case kind == reflect.Ptr:
subModel := reflect.New(fieldType.Type.Elem())
fieldValue.Set(subModel)
err = p.doParse(subModel.Interface(), stackRefValues, node)
if err != nil {
return fmt.Errorf("tag=`%v` %#v parser error: %v", tagValue, subModel, err)
}
//Slice
case kind == reflect.Slice:
sliceType := fieldValue.Type()
itemType := sliceType.Elem()
itemKind := itemType.Kind()
slice := reflect.MakeSlice(sliceType, node.Size(), node.Size())
node.EachWithBreak(func(i int, subNode *goquery.Selection) bool {
//outhtml, _ := goquery.OuterHtml(subNode)
//log.Printf("%v => %v", i, outhtml)
itemValue := reflect.New(itemType).Elem()
switch {
case itemKind == reflect.Struct:
err = p.doParse(itemValue.Addr().Interface(), stackRefValues, subNode)
if err != nil {
err = fmt.Errorf("tag=`%v` %#v parser error: %v", tagValue, itemValue, err)
return false
}
case itemKind == reflect.Ptr && itemValue.Type().Elem().Kind() == reflect.Struct:
itemValue = reflect.New(itemType.Elem())
err = p.doParse(itemValue.Interface(), stackRefValues, subNode)
if err != nil {
err = fmt.Errorf("tag=`%v` %#v parser error: %v", tagValue, itemValue, err)
return false
}
default:
itemValue.SetString(strings.TrimSpace(subNode.Text()))
}
slice.Index(i).Set(itemValue)
return true
})
if err != nil {
return err
}
fieldValue.Set(slice)
case kind == reflect.Struct:
subModel := reflect.New(fieldType.Type)
err = p.doParse(subModel.Interface(), stackRefValues, node)
if err != nil {
return fmt.Errorf("tag=`%v` %#v parser error: %v", tagValue, subModel, err)
}
fieldValue.Set(subModel.Elem())
//UnsafePointer
//Complex64
//Complex128
//Array
//Chan
//Func
default:
fieldValue.SetString(strings.TrimSpace(node.Text()))
}
}
return nil
}
/**
fieldType := refTypeElem.Field(i)
fieldValue := refValueElem.Field(i)
*/
func (p *Pagser) findAndExecFunc(objRefValue reflect.Value, stackRefValues []reflect.Value, selTag *tagTokenizer, node *goquery.Selection) (interface{}, error) {
if selTag.FuncName != "" {
//call object method
callMethod := findMethod(objRefValue, selTag.FuncName)
if callMethod.IsValid() {
//execute method
return execMethod(callMethod, selTag, node)
}
//call root method
size := len(stackRefValues)
if size > 0 {
for i := size - 1; i >= 0; i-- {
callMethod = findMethod(stackRefValues[i], selTag.FuncName)
if callMethod.IsValid() {
//execute method
return execMethod(callMethod, selTag, node)
}
}
}
//global function
if fn, ok := p.mapFuncs.Load(selTag.FuncName); ok {
cfn := fn.(CallFunc)
outValue, err := cfn(node, selTag.FuncParams...)
if err != nil {
return nil, fmt.Errorf("call registered func %v error: %v", selTag.FuncName, err)
}
return outValue, nil
}
//not found method
return nil, fmt.Errorf("not found method %v", selTag.FuncName)
}
return strings.TrimSpace(node.Text()), nil
}
func findMethod(objRefValue reflect.Value, funcName string) reflect.Value {
callMethod := objRefValue.MethodByName(funcName)
if callMethod.IsValid() {
return callMethod
}
//call element method
return objRefValue.Elem().MethodByName(funcName)
}
func execMethod(callMethod reflect.Value, selTag *tagTokenizer, node *goquery.Selection) (interface{}, error) {
callParams := make([]reflect.Value, 0)
callParams = append(callParams, reflect.ValueOf(node))
callReturns := callMethod.Call(callParams)
if len(callReturns) <= 0 {
return nil, fmt.Errorf("method %v not return any value", selTag.FuncName)
}
if len(callReturns) > 1 {
if err, ok := callReturns[len(callReturns)-1].Interface().(error); ok {
if err != nil {
return nil, fmt.Errorf("method %v return error: %v", selTag.FuncName, err)
}
}
}
return callReturns[0].Interface(), nil
}
func (p Pagser) setRefectValue(kind reflect.Kind, fieldValue reflect.Value, v interface{}) (err error) {
//set value
switch {
//Bool
case kind == reflect.Bool:
if p.Config.CastError {
kv, err := cast.ToBoolE(v)
if err != nil {
return err
}
fieldValue.SetBool(kv)
} else {
fieldValue.SetBool(cast.ToBool(v))
}
case kind >= reflect.Int && kind <= reflect.Int64:
if p.Config.CastError {
kv, err := cast.ToInt64E(v)
if err != nil {
return err
}
fieldValue.SetInt(kv)
} else {
fieldValue.SetInt(cast.ToInt64(v))
}
case kind >= reflect.Uint && kind <= reflect.Uintptr:
if p.Config.CastError {
kv, err := cast.ToUint64E(v)
if err != nil {
return err
}
fieldValue.SetUint(kv)
} else {
fieldValue.SetUint(cast.ToUint64(v))
}
case kind == reflect.Float32 || kind == reflect.Float64:
if p.Config.CastError {
value, err := cast.ToFloat64E(v)
if err != nil {
return err
}
fieldValue.SetFloat(value)
} else {
fieldValue.SetFloat(cast.ToFloat64(v))
}
case kind == reflect.String:
if p.Config.CastError {
kv, err := cast.ToStringE(v)
if err != nil {
return err
}
fieldValue.SetString(kv)
} else {
fieldValue.SetString(cast.ToString(v))
}
case kind == reflect.Slice || kind == reflect.Array:
sliceType := fieldValue.Type().Elem()
itemKind := sliceType.Kind()
if p.Config.CastError {
switch itemKind {
case reflect.Bool:
kv, err := cast.ToBoolSliceE(v)
if err != nil {
return err
}
fieldValue.Set(reflect.ValueOf(kv))
case reflect.Int:
kv, err := cast.ToIntSliceE(v)
if err != nil {
return err
}
fieldValue.Set(reflect.ValueOf(kv))
case reflect.Int32:
kv, err := toInt32SliceE(v)
if err != nil {
return err
}
fieldValue.Set(reflect.ValueOf(kv))
case reflect.Int64:
kv, err := toInt64SliceE(v)
if err != nil {
return err
}
fieldValue.Set(reflect.ValueOf(kv))
case reflect.Float32:
kv, err := toFloat32SliceE(v)
if err != nil {
return err
}
fieldValue.Set(reflect.ValueOf(kv))
case reflect.Float64:
kv, err := toFloat64SliceE(v)
if err != nil {
return err
}
fieldValue.Set(reflect.ValueOf(kv))
case reflect.String:
kv, err := cast.ToStringSliceE(v)
if err != nil {
return err
}
fieldValue.Set(reflect.ValueOf(kv))
default:
fieldValue.Set(reflect.ValueOf(v))
}
} else {
switch itemKind {
case reflect.Bool:
kv := cast.ToBoolSlice(v)
fieldValue.Set(reflect.ValueOf(kv))
case reflect.Int:
kv := cast.ToIntSlice(v)
fieldValue.Set(reflect.ValueOf(kv))
case reflect.Int32:
kv := toInt32Slice(v)
fieldValue.Set(reflect.ValueOf(kv))
case reflect.Int64:
kv := toInt64Slice(v)
fieldValue.Set(reflect.ValueOf(kv))
case reflect.Float32:
kv := toFloat32Slice(v)
fieldValue.Set(reflect.ValueOf(kv))
case reflect.Float64:
kv := toFloat64Slice(v)
fieldValue.Set(reflect.ValueOf(kv))
case reflect.String:
kv := cast.ToStringSlice(v)
fieldValue.Set(reflect.ValueOf(kv))
default:
fieldValue.Set(reflect.ValueOf(v))
}
}
//case kind == reflect.Interface:
// fieldValue.Set(reflect.ValueOf(v))
default:
fieldValue.Set(reflect.ValueOf(v))
//return fmt.Errorf("not support type %v", kind)
}
return nil
}