-
Notifications
You must be signed in to change notification settings - Fork 1
/
sq.go
281 lines (244 loc) · 5.77 KB
/
sq.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
package sq
import (
"errors"
"fmt"
"io"
"reflect"
"strconv"
"unicode"
"unicode/utf8"
"github.com/PuerkitoBio/goquery"
)
var (
// reflection errors
ErrInvalidKind = errors.New("invalid kind")
ErrNotSettable = errors.New("v is not settable")
ErrNonStructPtrValue = errors.New("*struct type required")
ErrTagNotFound = errors.New("sq tag not found")
// not found errors
ErrNodeNotFound = errors.New("node not found")
ErrAttributeNotFound = errors.New("attribute not found")
)
func Scrape(structPtr interface{}, r io.Reader) []error {
v := reflect.ValueOf(structPtr)
if v.Kind() != reflect.Ptr || v.Type().Elem().Kind() != reflect.Struct {
return []error{ErrNonStructPtrValue}
}
doc, err := goquery.NewDocumentFromReader(r)
if err != nil {
return []error{err}
}
return hydrateValue(&v, doc.Selection, nil)
}
// initialize and dereference pointers
func resolvePointer(v *reflect.Value) {
for v.Kind() == reflect.Ptr {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
*v = v.Elem()
}
}
func hydrateValue(v *reflect.Value, sel *goquery.Selection, p *path) []error {
resolvePointer(v)
if !v.CanSet() {
return nil
}
if p != nil && len(p.selector) > 0 && p.selector != "." && !sel.Is(p.selector) {
sel = sel.Find(p.selector)
if sel.Size() == 0 {
return []error{fmt.Errorf("%q did not match", p.selector)}
}
}
if p != nil && p.loader != nil {
if err := setValueFromSel(v, sel, p); err != nil {
return []error{err}
}
return nil
}
t := v.Type()
for _, tl := range typeLoaders {
if tl.isType(t) {
p.loader = &loader{
f: func(sel *goquery.Selection, text, _ string) (interface{}, error) {
return tl.load(sel, text)
},
}
if err := setValueFromSel(v, sel, p); err != nil {
return []error{err}
}
return nil
}
}
switch v.Kind() {
case reflect.Struct:
var errs []error
for i := 0; i < t.NumField(); i++ {
ft := t.Field(i)
p, err := parseTag(ft.Tag)
if err != nil {
if err != ErrTagNotFound {
errs = append(errs, err)
}
} else {
if r, _ := utf8.DecodeRuneInString(ft.Name); !unicode.IsUpper(r) {
errs = append(errs, fmt.Errorf("private field with sq tag: %q", ft.Name))
} else {
f := v.Field(i)
if err := hydrateValue(&f, sel, p); err != nil {
errs = append(errs, err...)
}
}
}
}
return errs
case reflect.Array:
// handle [N]byte copy from string
if t.Elem().Kind() == reflect.Uint8 {
s, err := extractString(sel, p.acc)
if err != nil {
return []error{err}
}
reflect.Copy(*v, reflect.ValueOf([]byte(s)))
return nil
}
var errs []error
sel.Each(func(i int, sel *goquery.Selection) {
if i < v.Len() {
vv := v.Index(i)
if err := hydrateValue(&vv, sel, p); err != nil {
errs = append(errs, err...)
}
}
})
return errs
case reflect.Slice:
// handle []byte setting directly
if t.Elem().Kind() == reflect.Uint8 {
s, err := extractString(sel, p.acc)
if err != nil {
return []error{err}
}
v.SetBytes([]byte(s))
return nil
}
var errs []error
slicev := reflect.MakeSlice(t, sel.Size(), sel.Size())
sel.Each(func(i int, sel *goquery.Selection) {
vv := slicev.Index(i)
if err := hydrateValue(&vv, sel, p); err != nil {
errs = append(errs, err...)
}
})
v.Set(slicev)
return errs
case reflect.Bool,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr,
reflect.Float32,
reflect.Float64,
reflect.Interface,
reflect.String:
if err := setValueFromSel(v, sel, p); err != nil {
return []error{err}
}
return nil
default:
// case reflect.Map:
// case reflect.Complex64:
// case reflect.Complex128:
// case reflect.Chan:
// case reflect.Func:
return []error{fmt.Errorf("%s: %v", ErrInvalidKind, v.Kind())}
}
}
func setValueFromSel(v *reflect.Value, sel *goquery.Selection, p *path) error {
s, err := extractString(sel, p.acc)
if err != nil {
return err
}
for _, pp := range p.parsers {
s, err = pp.parse(s)
if err != nil {
return fmt.Errorf("%s: (parser fail) %q", p.selector, err)
}
}
if p.loader != nil {
vv, err := p.loader.load(sel, s)
if err != nil {
return fmt.Errorf("%s: (loader fail) %q", p.selector, err)
}
rv := reflect.ValueOf(vv)
// deref pointers for correct setting.
// by this point we're operating on
// non-pointer values only.
for rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
v.Set(rv)
return nil
}
switch v.Kind() {
case reflect.Bool:
b, err := strconv.ParseBool(s)
if err != nil {
return fmt.Errorf("%s: %s", p.selector, err)
}
v.SetBool(b)
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
n, err := strconv.ParseInt(s, 10, v.Type().Bits())
if err != nil {
return fmt.Errorf("%s: %s", p.selector, err)
}
v.SetInt(n)
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr:
n, err := strconv.ParseUint(s, 10, v.Type().Bits())
if err != nil {
return fmt.Errorf("%s: %s", p.selector, err)
}
v.SetUint(n)
case reflect.Float32,
reflect.Float64:
n, err := strconv.ParseFloat(s, v.Type().Bits())
if err != nil {
return fmt.Errorf("%s: %s", p.selector, err)
}
v.SetFloat(n)
case reflect.String:
v.SetString(s)
case reflect.Interface:
v.Set(reflect.ValueOf(s))
default:
// unable to set these values from a string
// but should never reach this block with
// one of these value kinds.
// case reflect.Slice:
// case reflect.Struct:
// case reflect.Array:
// case reflect.Map:
// case reflect.Complex64:
// case reflect.Complex128:
// case reflect.Chan:
// case reflect.Func:
panic("unreachable")
}
return nil
}