-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
233 lines (227 loc) · 6.53 KB
/
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
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
package scrago
import (
"github.com/PuerkitoBio/goquery"
"reflect"
"fmt"
"regexp"
"strings"
"strconv"
)
//::text()
//::value()
//::html()
//::attr(xxx)
var rxFunc = regexp.MustCompile("^\\s*([a-zA-Z]+)\\s*\\(([^\\)]*)\\){0,1}\\s*$")
const (
parserTagName = "scrago"
parserSplitSep = "::"
)
type cssTager struct {
Selector string
FuncName string
FuncParams []string
}
func ParserDocument(v interface{}, document *goquery.Document) (err error) {
return ParserSelection(v, document.Selection)
}
func ParserSelection(v interface{}, selection *goquery.Selection) (err error) {
refType := reflect.TypeOf(v)
refValue := reflect.ValueOf(v)
//log.Printf("%#v kind is %v | %v", v, refValue.Kind(), reflect.Ptr)
if refValue.Kind() != reflect.Ptr{
return fmt.Errorf("%v is non-pointer?", refType)
}
if refValue.IsNil(){
return fmt.Errorf("%v is nil?", refType)
}
refTypeElem := refType.Elem()
refValueElem := refValue.Elem()
for i := 0; i < refValueElem.NumField(); i++{
fieldType := refTypeElem.Field(i)
fieldValue := refValueElem.Field(i)
tagValue := fieldType.Tag.Get(parserTagName)
tager := newCssTager(tagValue)
node := selection
if tager.Selector != ""{
node = selection.Find(tager.Selector)
}
nodeValue := ""
switch strings.ToLower(tager.FuncName) {
case "text":
nodeValue = node.Text()
case "html":
nodeValue, _ = node.Html()
case "outerhtml":
nodeValue, _ = goquery.OuterHtml(node)
case "value":
nodeValue = node.AttrOr("value", "")
case "attr":
if len(tager.FuncParams) > 0 && tager.FuncParams[0] != ""{
nodeValue = node.AttrOr(tager.FuncParams[0], "")
}
case "":
nodeValue = node.Text()
default:
callMethod := refValueElem.MethodByName(tager.FuncName)
if !callMethod.IsValid(){
callMethod = refValue.MethodByName(tager.FuncName)
}
if !callMethod.IsValid(){
return fmt.Errorf("method %v not found!", tager.FuncName)
}
callParams := make([]reflect.Value, 0)
callParams = append(callParams, reflect.ValueOf(node))
callReturns := callMethod.Call(callParams)
if len(callReturns) <= 0{
return fmt.Errorf("method %v not return any value", tager.FuncName)
}
if callReturns[0].Type() != fieldType.Type{
return fmt.Errorf("method %v return value of type %v is not assignable to type %v", tager.FuncName, callReturns[0].Type(), fieldType.Type)
}
if len(callReturns) > 1{
if err, ok := callReturns[len(callReturns) - 1].Interface().(error); ok{
if err != nil {
return fmt.Errorf("method %v return error: %v", tager.FuncName, err)
}
}
}
fieldValue.Set(callReturns[0])
continue
}
//set value
kind := fieldType.Type.Kind()
switch {
//Bool
case kind == reflect.Bool:
value, err := strconv.ParseBool(nodeValue)
if err != nil {
return fmt.Errorf("field %v convert value %v to %v error: %v", fieldType, nodeValue, kind, err)
}
fieldValue.SetBool(value)
//Int
//Int8
//Int16
//Int32
//Int64
case kind >= reflect.Int && kind <= reflect.Int64:
value, err := strconv.ParseInt(nodeValue, 10, int(fieldValue.Type().Size()*8))
if err != nil {
return fmt.Errorf("field %v convert value %v to %v error: %v", fieldType, nodeValue, kind, err)
}
fieldValue.SetInt(value)
//Uint
//Uint8
//Uint16
//Uint32
//Uint64
//Uintptr
case kind >= reflect.Uint && kind <= reflect.Uintptr:
value, err := strconv.ParseUint(nodeValue, 10, int(fieldValue.Type().Size()*8))
if err != nil {
return fmt.Errorf("field %v convert value %v to %v error: %v", fieldType, nodeValue, kind, err)
}
fieldValue.SetUint(value)
//Float32
//Float64
case kind == reflect.Float32 || kind == reflect.Float64:
value, err := strconv.ParseFloat(nodeValue, 64)
if err != nil {
return fmt.Errorf("field %v convert value %v to %v error: %v", fieldType, nodeValue, kind, err)
}
fieldValue.SetFloat(value)
//Interface
case kind == reflect.Interface:
fieldValue.Set(reflect.ValueOf(nodeValue))
//Map
//Ptr
case kind == reflect.Ptr:
subModel := reflect.New(fieldType.Type.Elem())
fieldValue.Set(subModel)
err = ParserSelection(subModel.Interface(), node)
if err != nil {
return fmt.Errorf("%#v parser error: %v", subModel, err)
}
//Slice
case kind == reflect.Slice:
slicetyp := fieldValue.Type()
itemtyp := slicetyp.Elem()
itemkind := itemtyp.Kind()
slice := reflect.MakeSlice(slicetyp, node.Size(), node.Size())
node.EachWithBreak(func(i int, subNode *goquery.Selection) bool {
//outhtml, _ := goquery.OuterHtml(subNode)
//log.Printf("%v => %v", i, outhtml)
tmp := reflect.New(itemtyp).Elem()
switch {
case itemkind == reflect.String:
tmp.SetString(subNode.Text())
case itemkind == reflect.Struct:
err = ParserSelection(tmp.Addr().Interface(), subNode)
if err != nil {
err = fmt.Errorf("%#v parser error: %v", tmp, err)
return false
}
case itemkind == reflect.Ptr && tmp.Type().Elem().Kind() == reflect.String:
tmpStr := subNode.Text()
tmp.Set(reflect.ValueOf(&tmpStr))
case itemkind == reflect.Ptr && tmp.Type().Elem().Kind() == reflect.Struct:
tmp = reflect.New(itemtyp.Elem())
err = ParserSelection(tmp.Interface(), subNode)
if err != nil {
err = fmt.Errorf("%#v parser error: %v", tmp, err)
return false
}
default:
err = fmt.Errorf("slice not support item type %v, kind %v", itemtyp, itemkind)
return false
}
slice.Index(i).Set(tmp)
return true
})
if err != nil {
return err
}
fieldValue.Set(slice)
case kind == reflect.String:
fieldValue.SetString(nodeValue)
case kind == reflect.Struct:
subModel := reflect.New(fieldType.Type)
err = ParserSelection(subModel.Interface(), node)
if err != nil {
return fmt.Errorf("%#v parser error: %v", subModel, err)
}
fieldValue.Set(subModel.Elem())
//UnsafePointer
//Complex64
//Complex128
//Array
//Chan
//Func
default:
return fmt.Errorf("field %v not support type %v", fieldType, kind)
}
}
return nil
}
func newCssTager(tagValue string) *cssTager {
cssParser := &cssTager{}
if tagValue == ""{
return cssParser
}
selectors := strings.Split(tagValue, parserSplitSep)
funcValue := ""
for i := 0; i < len(selectors); i++{
switch i {
case 0:
cssParser.Selector = strings.TrimSpace(selectors[i])
case 1:
funcValue = selectors[i]
}
}
matchs := rxFunc.FindStringSubmatch(funcValue)
if len(matchs) < 3{
return cssParser
}
cssParser.FuncName = strings.TrimSpace(matchs[1])
cssParser.FuncParams = strings.Split(matchs[2], ",")
return cssParser
}