-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xsv_read.go
52 lines (45 loc) · 1.98 KB
/
xsv_read.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
package xsv
import (
"bytes"
"encoding/csv"
"os"
"strings"
)
// XsvRead manages configuration values related to the csv read process.
type XsvRead[T any] struct {
TagName string //key in the struct field's tag to scan
TagSeparator string //separator string for multiple csv tags in struct fields
FailIfUnmatchedStructTags bool // indicates whether it is considered an error when there is an unmatched struct tag.
FailIfDoubleHeaderNames bool // indicates whether it is considered an error when a header name is repeated in the csv header.
ShouldAlignDuplicateHeadersWithStructFieldOrder bool // indicates whether we should align duplicate CSV headers per their alignment in the struct definition.
OnRecord func(T) T // callback function to be called on each record
NameNormalizer Normalizer
ErrorHandler ErrorHandler
}
// NewXsvRead creates a new XsvRead struct with default configuration values
func NewXsvRead[T any]() *XsvRead[T] {
return &XsvRead[T]{
TagName: "csv",
TagSeparator: ",",
FailIfUnmatchedStructTags: false,
FailIfDoubleHeaderNames: false,
ShouldAlignDuplicateHeadersWithStructFieldOrder: false,
OnRecord: nil,
NameNormalizer: func(s string) string { return s },
ErrorHandler: nil,
}
}
func (x *XsvRead[T]) SetReader(r *csv.Reader) (xr *XsvReader[T]) {
xr = NewXsvReader(*x)
xr.reader = r
return xr
}
func (x *XsvRead[T]) SetFileReader(file *os.File) (xr *XsvReader[T]) {
return x.SetReader(csv.NewReader(file))
}
func (x *XsvRead[T]) SetStringReader(string string) (xr *XsvReader[T]) {
return x.SetReader(csv.NewReader(strings.NewReader(string)))
}
func (x *XsvRead[T]) SetByteReader(byte []byte) (xr *XsvReader[T]) {
return x.SetReader(csv.NewReader(bytes.NewReader(byte)))
}