Skip to content

Commit

Permalink
Trim spaces before parsing values (#18)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrián Moreno <[email protected]>
  • Loading branch information
adrianmo authored Feb 19, 2020
1 parent 970489b commit ea7340b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions csv_files/trailingSpaces.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
header1, header2, header3
line1, 1, 1.2
line2, 2, 2.3
line3, 3, 3.4
2 changes: 1 addition & 1 deletion load.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func mapToDestination(header []string, content [][]string, destination interface
// @param valRv: the reflected value where we want to store our value.
// @return an error if one occurs.
func storeValue(rawValue string, valRv reflect.Value) error {
rawValue = strings.TrimSpace(rawValue)
switch valRv.Kind() {
case reflect.String:
valRv.SetString(rawValue)
Expand All @@ -201,7 +202,6 @@ func storeValue(rawValue string, valRv reflect.Value) error {
value, err := strconv.ParseInt(rawValue, 10, 64)
if err != nil && rawValue != "" {
return fmt.Errorf("error parsing int '%v':\n ==> %v", rawValue, err)

}
valRv.SetInt(value)
case reflect.Float64:
Expand Down
8 changes: 8 additions & 0 deletions load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ func TestWithSemicolon(t *testing.T) {
}
}

func TestWithTrailingSpaces(t *testing.T) {
tabT := []test{}
err := LoadFromPath("csv_files/trailingSpaces.csv", &tabT)
if err != nil || checkValues(tabT) {
t.Fail()
}
}

func TestToMutchOptions(t *testing.T) {
tabT := []test{}
err := LoadFromPath(
Expand Down

0 comments on commit ea7340b

Please sign in to comment.