From ea7340b19719a1b60aa1911f2f4d6c4c1c5f12c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Moreno?= Date: Wed, 19 Feb 2020 14:19:32 +0100 Subject: [PATCH] Trim spaces before parsing values (#18) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Adrián Moreno --- csv_files/trailingSpaces.csv | 4 ++++ load.go | 2 +- load_test.go | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 csv_files/trailingSpaces.csv diff --git a/csv_files/trailingSpaces.csv b/csv_files/trailingSpaces.csv new file mode 100644 index 0000000..77cff8f --- /dev/null +++ b/csv_files/trailingSpaces.csv @@ -0,0 +1,4 @@ +header1, header2, header3 +line1, 1, 1.2 +line2, 2, 2.3 +line3, 3, 3.4 diff --git a/load.go b/load.go index a189732..4ac9dbf 100644 --- a/load.go +++ b/load.go @@ -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) @@ -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: diff --git a/load_test.go b/load_test.go index f0a2dff..003a7a3 100644 --- a/load_test.go +++ b/load_test.go @@ -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(