-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathslice.go
157 lines (145 loc) · 3.53 KB
/
slice.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
package htmltable
import (
"context"
"fmt"
"io"
"net/http"
"reflect"
"strings"
)
// NewSlice returns slice of annotated struct types from io.Reader
func NewSlice[T any](ctx context.Context, r io.Reader) ([]T, error) {
f := &feeder[T]{
Page: Page{ctx: ctx},
}
f.init(r)
return f.slice()
}
// NewSliceFromPage finds a table matching the slice and returns the slice
func NewSliceFromPage[T any](p *Page) ([]T, error) {
return (&feeder[T]{
Page: *p,
}).slice()
}
// NewSliceFromString is same as NewSlice(context.Context, io.Reader),
// but takes just a string.
func NewSliceFromString[T any](in string) ([]T, error) {
return NewSlice[T](context.Background(), strings.NewReader(in))
}
// NewSliceFromString is same as NewSlice(context.Context, io.Reader),
// but takes just an http.Response
func NewSliceFromResponse[T any](resp *http.Response) ([]T, error) {
return NewSlice[T](resp.Request.Context(), resp.Body)
}
// NewSliceFromString is same as NewSlice(context.Context, io.Reader),
// but takes just an URL.
func NewSliceFromURL[T any](url string) ([]T, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
if resp.Body != nil {
defer resp.Body.Close()
}
return NewSliceFromResponse[T](resp)
}
type feeder[T any] struct {
Page
dummy T
}
func (f *feeder[T]) headers() ([]string, map[string]int, error) {
dt := reflect.ValueOf(f.dummy)
elem := dt.Type()
headers := []string{}
fields := map[string]int{}
for i := 0; i < elem.NumField(); i++ {
field := elem.Field(i)
header := field.Tag.Get("header")
if header == "" {
continue
}
err := f.isTypeSupported(field)
if err != nil {
return nil, nil, err
}
fields[header] = i
headers = append(headers, header)
}
return headers, fields, nil
}
func (f *feeder[T]) isTypeSupported(field reflect.StructField) error {
k := field.Type.Kind()
if k == reflect.String {
return nil
}
if k == reflect.Int {
return nil
}
if k == reflect.Bool {
return nil
}
return fmt.Errorf("setting field is not supported, %s is %v",
field.Name, field.Type.Name())
}
func (f *feeder[T]) table() (*Table, map[int]int, error) {
headers, fields, err := f.headers()
if err != nil {
return nil, nil, err
}
table, err := f.FindWithColumns(headers...)
if err != nil {
return nil, nil, err
}
mapping := map[int]int{}
for idx, header := range table.Header {
field, ok := fields[header]
if !ok {
continue
}
mapping[idx] = field
}
return table, mapping, nil
}
func (f *feeder[T]) slice() ([]T, error) {
table, mapping, err := f.table()
if err != nil {
return nil, err
}
dummy := reflect.ValueOf(f.dummy)
dt := dummy.Type()
sliceValue := reflect.MakeSlice(reflect.SliceOf(dt),
len(table.Rows), len(table.Rows))
for rowIdx, row := range table.Rows {
item := sliceValue.Index(rowIdx)
for idx, field := range mapping {
if len(row) < len(mapping) && idx == len(row) {
// either corrupt row or something like that
continue
}
switch item.Field(field).Kind() {
case reflect.String:
item.Field(field).SetString(row[idx])
case reflect.Bool:
var v bool
lower := strings.ToLower(row[idx])
if lower == "yes" ||
lower == "y" ||
lower == "true" ||
lower == "t" {
v = true
}
item.Field(field).SetBool(v)
case reflect.Int:
var v int64
_, err := fmt.Sscan(row[idx], &v)
if err != nil {
column := table.Header[idx]
return nil, fmt.Errorf("row %d: %s: %w", rowIdx, column, err)
}
item.Field(field).SetInt(v)
default: // noop
}
}
}
return sliceValue.Interface().([]T), nil
}