Skip to content

Commit ea7340b

Browse files
authored
Trim spaces before parsing values (#18)
Signed-off-by: Adrián Moreno <[email protected]>
1 parent 970489b commit ea7340b

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

csv_files/trailingSpaces.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
header1, header2, header3
2+
line1, 1, 1.2
3+
line2, 2, 2.3
4+
line3, 3, 3.4

load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func mapToDestination(header []string, content [][]string, destination interface
192192
// @param valRv: the reflected value where we want to store our value.
193193
// @return an error if one occurs.
194194
func storeValue(rawValue string, valRv reflect.Value) error {
195+
rawValue = strings.TrimSpace(rawValue)
195196
switch valRv.Kind() {
196197
case reflect.String:
197198
valRv.SetString(rawValue)
@@ -201,7 +202,6 @@ func storeValue(rawValue string, valRv reflect.Value) error {
201202
value, err := strconv.ParseInt(rawValue, 10, 64)
202203
if err != nil && rawValue != "" {
203204
return fmt.Errorf("error parsing int '%v':\n ==> %v", rawValue, err)
204-
205205
}
206206
valRv.SetInt(value)
207207
case reflect.Float64:

load_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ func TestWithSemicolon(t *testing.T) {
7979
}
8080
}
8181

82+
func TestWithTrailingSpaces(t *testing.T) {
83+
tabT := []test{}
84+
err := LoadFromPath("csv_files/trailingSpaces.csv", &tabT)
85+
if err != nil || checkValues(tabT) {
86+
t.Fail()
87+
}
88+
}
89+
8290
func TestToMutchOptions(t *testing.T) {
8391
tabT := []test{}
8492
err := LoadFromPath(

0 commit comments

Comments
 (0)