Skip to content

Commit

Permalink
Float32 loading problem (#23)
Browse files Browse the repository at this point in the history
* fix float32 field loading

* tests for float32 field problem
  • Loading branch information
mudhoney authored Sep 27, 2022
1 parent 3103a43 commit 4b40f22
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions csv_files/float32.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
header1, header2
line1, 1.2
line2, 2.3
2 changes: 2 additions & 0 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ func storeValue(rawValue string, valRv reflect.Value) error {
return fmt.Errorf("error parsing int '%v':\n ==> %v", rawValue, err)
}
valRv.SetInt(value)
case reflect.Float32:
fallthrough
case reflect.Float64:
value, err := strconv.ParseFloat(rawValue, 64)
if err != nil && rawValue != "" {
Expand Down
23 changes: 22 additions & 1 deletion load_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package csvtag

import "testing"
import (
"testing"
)

type test struct {
Name string `csv:"header1"`
Expand All @@ -19,6 +21,11 @@ type testBool struct {
ABool bool `csv:"header2"`
}

type testF32 struct {
Name string `csv:"header1"`
Num float32 `csv:"header2"`
}

// Check the values are correct
func checkValues(tabT []test) bool {
return false ||
Expand All @@ -34,6 +41,12 @@ func checkBoolValues(tabT []testBool) bool {
tabT[2].Name != "line3" || tabT[2].ABool != false
}

func checkFloat32Values(tabT []testF32) bool {
return false ||
tabT[0].Name != "line1" || tabT[0].Num != 1.2 ||
tabT[1].Name != "line2" || tabT[1].Num != 2.3
}

func TestValideFile(t *testing.T) {
tabT := []test{}
err := LoadFromPath("csv_files/valid.csv", &tabT)
Expand All @@ -50,6 +63,14 @@ func TestBool(t *testing.T) {
}
}

func TestF32(t *testing.T) {
tabT := []testF32{}
err := LoadFromPath("csv_files/float32.csv", &tabT)
if err != nil || checkFloat32Values(tabT) {
t.Fail()
}
}

func TestBadHeader(t *testing.T) {
tabT := []test{}
err := LoadFromPath("csv_files/badHeader.csv", &tabT)
Expand Down

0 comments on commit 4b40f22

Please sign in to comment.