1
0
Fork 0
mirror of https://github.com/imjasonh/csvstruct synced 2026-07-18 14:56:23 +00:00

support uints

This commit is contained in:
Jason Hall 2014-05-15 12:20:41 -04:00
parent f70eb22586
commit 9d07ca61c9
2 changed files with 11 additions and 4 deletions

View file

@ -72,12 +72,18 @@ func (d *decoder) DecodeNext(v interface{}) error {
switch vf.Kind() { switch vf.Kind() {
case reflect.String: case reflect.String:
vf.SetString(strv) vf.SetString(strv)
case reflect.Int, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i, err := strconv.ParseInt(strv, 10, 64) i, err := strconv.ParseInt(strv, 10, 64)
if err != nil { if err != nil {
return fmt.Errorf("error decoding: %v", err) return fmt.Errorf("error decoding: %v", err)
} }
vf.SetInt(i) vf.SetInt(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
u, err := strconv.ParseUint(strv, 10, 64)
if err != nil {
return fmt.Errorf("error decoding: %v", err)
}
vf.SetUint(u)
case reflect.Float64: case reflect.Float64:
f, err := strconv.ParseFloat(strv, 64) f, err := strconv.ParseFloat(strv, 64)
if err != nil { if err != nil {

View file

@ -96,15 +96,16 @@ func TestDecode_NonStrings(t *testing.T) {
type row struct { type row struct {
Int int Int int
Int64 int64 Int64 int64
Uint64 uint64
Float64 float64 Float64 float64
Bool bool Bool bool
} }
var r row var r row
if err := NewDecoder(strings.NewReader(`Int,Int64,Float64,Bool if err := NewDecoder(strings.NewReader(`Int,Int64,Uint64,Float64,Bool
123,123456789,123.456,true`)).DecodeNext(&r); err != nil { 123,-123456789,123456789,123.456,true`)).DecodeNext(&r); err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
exp := row{123, 123456789, 123.456, true} exp := row{123, -123456789, 123456789, 123.456, true}
if !reflect.DeepEqual(r, exp) { if !reflect.DeepEqual(r, exp) {
t.Errorf("unexpected results, got %v, want %v", r, exp) t.Errorf("unexpected results, got %v, want %v", r, exp)
} }