Skip to content

Commit b26afec

Browse files
committed
added csvtable.ReadFile... functions
1 parent c2abe54 commit b26afec

File tree

9 files changed

+51
-51
lines changed

9 files changed

+51
-51
lines changed

csvtable/format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type FormatDetectionConfig struct {
4747
EncodingTests []string `json:"encodingTests"`
4848
}
4949

50-
func NewFormatDetectionConfig() *FormatDetectionConfig {
50+
func NewDefaultFormatDetectionConfig() *FormatDetectionConfig {
5151
return &FormatDetectionConfig{
5252
Encodings: []string{
5353
"UTF-8",

csvtable/parse.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99

1010
// ParseDetectFormat returns a slice of strings per row
1111
// with the format detected via the optional FormatDetectionConfig.
12-
func ParseDetectFormat(csv []byte, configOrNil *FormatDetectionConfig) (rows [][]string, format *Format, err error) {
13-
config := configOrNil
12+
// NewDefaultFormatDetectionConfig() will be used if config is nil.
13+
func ParseDetectFormat(csv []byte, config *FormatDetectionConfig) (rows [][]string, format *Format, err error) {
1414
if config == nil {
15-
config = NewFormatDetectionConfig()
15+
config = NewDefaultFormatDetectionConfig()
1616
}
1717

1818
format, lines, err := detectFormatAndSplitLines(csv, config)

csvtable/read.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,50 @@ import (
44
"reflect"
55

66
"github.com/domonda/go-retable"
7+
"github.com/ungerik/go-fs"
78
)
89

9-
func AsStructSlice[T any](rows [][]string, naming *retable.StructFieldNaming, requiredCols []string, dstScanner retable.Scanner, srcFormatter retable.Formatter, validate func(reflect.Value) error) ([]T, error) {
10+
func ReadStringsToStructSlice[T any](rows [][]string, naming *retable.StructFieldNaming, dstScanner retable.Scanner, srcFormatter retable.Formatter, validate func(reflect.Value) error, requiredCols ...string) ([]T, error) {
1011
rows = RemoveEmptyRows(rows)
1112
return retable.ViewToStructSlice[T](
1213
retable.NewStringsView("", rows),
1314
naming,
14-
requiredCols,
1515
dstScanner,
1616
srcFormatter,
1717
validate,
18+
requiredCols...,
1819
)
1920
}
2021

21-
func ReadWithFormatAsStructSlice[T any](csv []byte, format *Format, naming *retable.StructFieldNaming, requiredCols []string, dstScanner retable.Scanner, srcFormatter retable.Formatter, validate func(reflect.Value) error) ([]T, error) {
22-
rows, err := ParseWithFormat(csv, format)
22+
func ReadBytesWithFormatToStructSlice[T any](csvData []byte, format *Format, naming *retable.StructFieldNaming, dstScanner retable.Scanner, srcFormatter retable.Formatter, validate func(reflect.Value) error, requiredCols ...string) ([]T, error) {
23+
rows, err := ParseWithFormat(csvData, format)
2324
if err != nil {
2425
return nil, err
2526
}
26-
rows = RemoveEmptyRows(rows)
27-
return retable.ViewToStructSlice[T](
28-
retable.NewStringsView("", rows),
29-
naming,
30-
requiredCols,
31-
dstScanner,
32-
srcFormatter,
33-
validate,
34-
)
27+
return ReadStringsToStructSlice[T](rows, naming, dstScanner, srcFormatter, validate, requiredCols...)
3528
}
3629

37-
func ReadDetectFormatAsStructSlice[T any](csv []byte, configOrNil *FormatDetectionConfig, naming *retable.StructFieldNaming, requiredCols []string, dstScanner retable.Scanner, srcFormatter retable.Formatter, validate func(reflect.Value) error) ([]T, error) {
38-
rows, _, err := ParseDetectFormat(csv, configOrNil)
30+
func ReadFileWithFormatToStructSlice[T any](csvFile fs.FileReader, format *Format, naming *retable.StructFieldNaming, dstScanner retable.Scanner, srcFormatter retable.Formatter, validate func(reflect.Value) error, requiredCols ...string) ([]T, error) {
31+
data, err := csvFile.ReadAll()
3932
if err != nil {
4033
return nil, err
4134
}
42-
rows = RemoveEmptyRows(rows)
43-
return retable.ViewToStructSlice[T](
44-
retable.NewStringsView("", rows),
45-
naming,
46-
requiredCols,
47-
dstScanner,
48-
srcFormatter,
49-
validate,
50-
)
35+
return ReadBytesWithFormatToStructSlice[T](data, format, naming, dstScanner, srcFormatter, validate, requiredCols...)
36+
}
37+
38+
func ReadBytesDetectFormatToStructSlice[T any](csvData []byte, detectConfig *FormatDetectionConfig, naming *retable.StructFieldNaming, dstScanner retable.Scanner, srcFormatter retable.Formatter, validate func(reflect.Value) error, requiredCols ...string) ([]T, *Format, error) {
39+
rows, format, err := ParseDetectFormat(csvData, detectConfig)
40+
if err != nil {
41+
return nil, format, err
42+
}
43+
slice, err := ReadStringsToStructSlice[T](rows, naming, dstScanner, srcFormatter, validate, requiredCols...)
44+
return slice, format, err
45+
}
46+
47+
func ReadFileDetectFormatToStructSlice[T any](csvFile fs.FileReader, detectConfig *FormatDetectionConfig, naming *retable.StructFieldNaming, dstScanner retable.Scanner, srcFormatter retable.Formatter, validate func(reflect.Value) error, requiredCols ...string) ([]T, *Format, error) {
48+
data, err := csvFile.ReadAll()
49+
if err != nil {
50+
return nil, nil, err
51+
}
52+
return ReadBytesDetectFormatToStructSlice[T](data, detectConfig, naming, dstScanner, srcFormatter, validate, requiredCols...)
5153
}

exceltable/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ replace github.com/domonda/go-retable => ..
66

77
require github.com/domonda/go-retable v0.0.0-00010101000000-000000000000 // replaced
88

9-
require github.com/xuri/excelize/v2 v2.8.1
9+
require github.com/xuri/excelize/v2 v2.9.0
1010

1111
require (
1212
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect

exceltable/go.sum

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,22 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
77
github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM=
88
github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7gK3DypaEsUk=
99
github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
10-
github.com/richardlehane/msoleps v1.0.3 h1:aznSZzrwYRl3rLKRT3gUk9am7T/mLNSnJINvN0AQoVM=
11-
github.com/richardlehane/msoleps v1.0.3/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
1210
github.com/richardlehane/msoleps v1.0.4 h1:WuESlvhX3gH2IHcd8UqyCuFY5yiq/GR/yqaSM/9/g00=
1311
github.com/richardlehane/msoleps v1.0.4/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
1412
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1513
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1614
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY=
1715
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
18-
github.com/xuri/excelize/v2 v2.8.1 h1:pZLMEwK8ep+CLIUWpWmvW8IWE/yxqG0I1xcN6cVMGuQ=
19-
github.com/xuri/excelize/v2 v2.8.1/go.mod h1:oli1E4C3Pa5RXg1TBXn4ENCXDV5JUMlBluUhG7c+CEE=
16+
github.com/xuri/excelize/v2 v2.9.0 h1:1tgOaEq92IOEumR1/JfYS/eR0KHOCsRv/rYXXh6YJQE=
17+
github.com/xuri/excelize/v2 v2.9.0/go.mod h1:uqey4QBZ9gdMeWApPLdhm9x+9o2lq4iVmjiLfBS5hdE=
2018
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
2119
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
22-
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
23-
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
24-
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
25-
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
2620
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
2721
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
28-
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
29-
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
30-
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
31-
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
32-
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
33-
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
22+
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
23+
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
3424
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
3525
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
36-
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
37-
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
38-
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
39-
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
4026
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
4127
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
4228
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ module github.com/domonda/go-retable
33
go 1.23
44

55
require (
6-
github.com/domonda/go-types v0.0.0-20241001090154-50384689aa30
6+
github.com/domonda/go-types v0.0.0-20241016145418-49737a904fc1
77
github.com/stretchr/testify v1.9.0
8+
github.com/ungerik/go-fs v0.0.0-20240919125757-1b6f933a416d
89
)
910

1011
require (
1112
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/fsnotify/fsnotify v1.7.0 // indirect
1214
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
golang.org/x/sys v0.26.0 // indirect
1316
golang.org/x/text v0.19.0 // indirect
1417
gopkg.in/yaml.v3 v3.0.1 // indirect
1518
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/domonda/go-types v0.0.0-20241001090154-50384689aa30 h1:XHFdfkOBZZibBewf1/DP/0GGwZERGQC36aYIrhPqHsA=
44
github.com/domonda/go-types v0.0.0-20241001090154-50384689aa30/go.mod h1:QfZG5NrNWDrwcqOp3ZlNh2XaLjZI1ncNpGPAa9MIUUE=
5+
github.com/domonda/go-types v0.0.0-20241016145418-49737a904fc1 h1:cXAYa3IsvNqlXAb7+VG3++CbJ0CX5eiRboIK0uerfL0=
6+
github.com/domonda/go-types v0.0.0-20241016145418-49737a904fc1/go.mod h1:QfZG5NrNWDrwcqOp3ZlNh2XaLjZI1ncNpGPAa9MIUUE=
7+
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
8+
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
59
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
610
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
711
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
812
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
13+
github.com/ungerik/go-fs v0.0.0-20240919125757-1b6f933a416d h1:71JniF82NUc6v7nBx23OMSzdYiV5phxvTIU8XsRMdnU=
14+
github.com/ungerik/go-fs v0.0.0-20240919125757-1b6f933a416d/go.mod h1:nMIa35zyLzk4K3tTLL+AAsOZ9Q+0lgX/lxYubEwCZSY=
15+
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
16+
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
17+
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
18+
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
919
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
1020
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
1121
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

go.work.sum

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a/go.mod h1:2GxOXOlEPAMFPfp014mK1SWq8G8BN8o7/dfYqJrVGn8=
22
github.com/domonda/go-errs v0.0.0-20240702051036-0e696c849b5f/go.mod h1:qLWt1z3aIg12+Dbxu9bMydFOHEi92vWE7vAHcHLd8n8=
33
github.com/domonda/go-pretty v0.0.0-20240110134850-17385799142f/go.mod h1:3QkM8UJdyJMeKZiIo7hYzSkQBpRS3k0gOHw4ysyEIB4=
4-
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
5-
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
64
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f/go.mod h1:Pcatq5tYkCW2Q6yrR2VRHlbHpZ/R4/7qyL1TCF7vl14=
75
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
86
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
@@ -35,6 +33,7 @@ golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
3533
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
3634
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
3735
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
36+
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
3837
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
3938
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
4039
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=

viewtostructslice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
// to call Validate() methods on the struct field values.
2727
//
2828
// The arguments dstScanner, srcFormatter, and validate can be nil.
29-
func ViewToStructSlice[T any](view View, naming *StructFieldNaming, requiredCols []string, dstScanner Scanner, srcFormatter Formatter, validate func(reflect.Value) error) ([]T, error) {
29+
func ViewToStructSlice[T any](view View, naming *StructFieldNaming, dstScanner Scanner, srcFormatter Formatter, validate func(reflect.Value) error, requiredCols ...string) ([]T, error) {
3030
rowType := reflect.TypeFor[T]()
3131
if rowType.Kind() != reflect.Struct && (rowType.Kind() != reflect.Pointer || rowType.Elem().Kind() != reflect.Struct) {
3232
return nil, fmt.Errorf("slice element type %s is not a struct or pointer to struct", rowType)

0 commit comments

Comments
 (0)