Skip to content

Commit 4b40f22

Browse files
authored
Float32 loading problem (#23)
* fix float32 field loading * tests for float32 field problem
1 parent 3103a43 commit 4b40f22

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

csv_files/float32.csv

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

load.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ func storeValue(rawValue string, valRv reflect.Value) error {
204204
return fmt.Errorf("error parsing int '%v':\n ==> %v", rawValue, err)
205205
}
206206
valRv.SetInt(value)
207+
case reflect.Float32:
208+
fallthrough
207209
case reflect.Float64:
208210
value, err := strconv.ParseFloat(rawValue, 64)
209211
if err != nil && rawValue != "" {

load_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package csvtag
22

3-
import "testing"
3+
import (
4+
"testing"
5+
)
46

57
type test struct {
68
Name string `csv:"header1"`
@@ -19,6 +21,11 @@ type testBool struct {
1921
ABool bool `csv:"header2"`
2022
}
2123

24+
type testF32 struct {
25+
Name string `csv:"header1"`
26+
Num float32 `csv:"header2"`
27+
}
28+
2229
// Check the values are correct
2330
func checkValues(tabT []test) bool {
2431
return false ||
@@ -34,6 +41,12 @@ func checkBoolValues(tabT []testBool) bool {
3441
tabT[2].Name != "line3" || tabT[2].ABool != false
3542
}
3643

44+
func checkFloat32Values(tabT []testF32) bool {
45+
return false ||
46+
tabT[0].Name != "line1" || tabT[0].Num != 1.2 ||
47+
tabT[1].Name != "line2" || tabT[1].Num != 2.3
48+
}
49+
3750
func TestValideFile(t *testing.T) {
3851
tabT := []test{}
3952
err := LoadFromPath("csv_files/valid.csv", &tabT)
@@ -50,6 +63,14 @@ func TestBool(t *testing.T) {
5063
}
5164
}
5265

66+
func TestF32(t *testing.T) {
67+
tabT := []testF32{}
68+
err := LoadFromPath("csv_files/float32.csv", &tabT)
69+
if err != nil || checkFloat32Values(tabT) {
70+
t.Fail()
71+
}
72+
}
73+
5374
func TestBadHeader(t *testing.T) {
5475
tabT := []test{}
5576
err := LoadFromPath("csv_files/badHeader.csv", &tabT)

0 commit comments

Comments
 (0)