-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.go
151 lines (137 loc) · 6.51 KB
/
csv.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
package parse
import (
"fmt"
)
// CSVParse returns a slice of type T from the first comma separated
// string value found at the given key in the given sources in order.
// Each comma separated values is parsed using the provided
// `parse` function.
// The slice is returned as `nil` if:
// - the key given is NOT set in any of the sources.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and the corresponding value is empty.
func CSVParse[T any](sources []Source, key string,
parse ParseFunc[T], options ...Option) (values []T, err error) {
stringValues, sourceName := csv(sources, key, options...)
if stringValues == nil {
return nil, nil
}
values = make([]T, len(stringValues))
for i, stringValue := range stringValues {
values[i], err = parse(stringValue)
if err != nil {
return nil, fmt.Errorf("%s %s: %w", sourceName, key, err)
}
}
return values, nil
}
// CSVInt returns a slice of int from the first comma separated
// string value found at the given key in the given sources in order.
// It returns an error if any value is not a valid int string.
// The slice is returned as `nil` if:
// - the key given is NOT set in any of the sources.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and the corresponding value is empty.
func CSVInt(sources []Source, key string,
options ...Option) (values []int, err error) {
return CSVParse(sources, key, parseInt, options...)
}
// CSVInt8 returns a slice of int8 from the first comma separated
// string value found at the given key in the given sources in order.
// It returns an error if any value is not a valid int8 string.
// The slice is returned as `nil` if:
// - the key given is NOT set in any of the sources.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and the corresponding value is empty.
func CSVInt8(sources []Source, key string,
options ...Option) (values []int8, err error) {
return CSVParse(sources, key, parseInt8, options...)
}
// CSVInt16 returns a slice of int16 from the first comma separated
// string value found at the given key in the given sources in order.
// It returns an error if any value is not a valid int16 string.
// The slice is returned as `nil` if:
// - the key given is NOT set in any of the sources.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and the corresponding value is empty.
func CSVInt16(sources []Source, key string,
options ...Option) (values []int16, err error) {
return CSVParse(sources, key, parseInt16, options...)
}
// CSVInt32 returns a slice of int32 from the first comma separated
// string value found at the given key in the given sources in order.
// It returns an error if any value is not a valid int32 string.
// The slice is returned as `nil` if:
// - the key given is NOT set in any of the sources.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and the corresponding value is empty.
func CSVInt32(sources []Source, key string,
options ...Option) (values []int32, err error) {
return CSVParse(sources, key, parseInt32, options...)
}
// CSVInt64 returns a slice of int64 from the first comma separated
// string value found at the given key in the given sources in order.
// It returns an error if any value is not a valid int64 string.
// The slice is returned as `nil` if:
// - the key given is NOT set in any of the sources.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and the corresponding value is empty.
func CSVInt64(sources []Source, key string,
options ...Option) (values []int64, err error) {
return CSVParse(sources, key, parseInt64, options...)
}
// CSVUint returns a slice of uint from the first comma separated
// string value found at the given key in the given sources in order.
// It returns an error if any value is not a valid uint string.
// The slice is returned as `nil` if:
// - the key given is NOT set in any of the sources.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and the corresponding value is empty.
func CSVUint(sources []Source, key string,
options ...Option) (values []uint, err error) {
return CSVParse(sources, key, parseUint, options...)
}
// CSVUint8 returns a slice of uint8 from the first comma separated
// string value found at the given key in the given sources in order.
// It returns an error if any value is not a valid uint8 string.
// The slice is returned as `nil` if:
// - the key given is NOT set in any of the sources.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and the corresponding value is empty.
func CSVUint8(sources []Source, key string,
options ...Option) (values []uint8, err error) {
return CSVParse(sources, key, parseUint8, options...)
}
// CSVUint16 returns a slice of uint8 from the first comma separated
// string value found at the given key in the given sources in order.
// It returns an error if any value is not a valid uint8 string.
// The slice is returned as `nil` if:
// - the key given is NOT set in any of the sources.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and the corresponding value is empty.
func CSVUint16(sources []Source, key string,
options ...Option) (values []uint16, err error) {
return CSVParse(sources, key, parseUint16, options...)
}
// CSVUint32 returns a slice of uint32 from the first comma separated
// string value found at the given key in the given sources in order.
// It returns an error if any value is not a valid uint32 string.
// The slice is returned as `nil` if:
// - the key given is NOT set in any of the sources.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and the corresponding value is empty.
func CSVUint32(sources []Source, key string,
options ...Option) (values []uint32, err error) {
return CSVParse(sources, key, parseUint32, options...)
}
// CSVUint64 returns a slice of uint64 from the first comma separated
// string value found at the given key in the given sources in order.
// It returns an error if any value is not a valid uint64 string.
// The slice is returned as `nil` if:
// - the key given is NOT set in any of the sources.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and the corresponding value is empty.
func CSVUint64(sources []Source, key string,
options ...Option) (values []uint64, err error) {
return CSVParse(sources, key, parseUint64, options...)
}