-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder.go
86 lines (82 loc) · 1.77 KB
/
decoder.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
package seltabl
import (
"fmt"
"io"
)
// Decoder is a struct for decoding a reader into a slice of structs.
//
// It is used by the NewDecoder function.
//
// It is not intended to be used directly.
//
// Example:
//
// type TableStruct struct {
// A string `json:"a" seltabl:"a" hSel:"tr:nth-child(1) td:nth-child(1)" dSel:"tr td:nth-child(1)" cSel:"$text"`
// B string `json:"b" seltabl:"b" hSel:"tr:nth-child(1) td:nth-child(2)" dSel:"tr td:nth-child(2)" cSel:"$text"`
// }
//
// func main() {
// r := strings.NewReader(`
// <table>
// <tr>
// <td>a</td>
// <td>b</td>
// </tr>
// <tr>
// <td> 1 </td>
// <td>2</td>
// </tr>
// <tr>
// <td>3 </td>
// <td> 4</td>
// </tr>
// <tr>
// <td> 5 </td>
// <td> 6</td>
// </tr>
// <tr>
// <td>7 </td>
// <td> 8</td>
// </tr>
// </table>
// `)
// p, err := seltabl.NewDecoder[TableStruct](r)
// if err != nil {
// panic(err)
// }
// for _, pp := range p {
// fmt.Printf("pp %+v\n", pp)
// }
// }
type Decoder[T any] struct {
reader io.ReadCloser
}
// NewDecoder parses a reader into a slice of structs.
//
// It is used by the NewFromReader function.
//
// This allows for decoding a reader into a slice of structs.
//
// Similar to the json.Decoder for brevity.
func NewDecoder[T any](r io.ReadCloser) *Decoder[T] {
return &Decoder[T]{
reader: r,
}
}
// Decode parses a reader into a slice of structs.
//
// It is used by the Decoder.Decode function.
//
// This allows for decoding a reader into a slice of structs.
//
// Similar to the json.Decoder for brevity.
func (d *Decoder[T]) Decode() ([]T, error) {
defer d.reader.Close()
var result []T
result, err := NewFromReader[T](d.reader)
if err != nil {
return nil, fmt.Errorf("failed to decode: %w", err)
}
return result, nil
}