-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
161 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package csvutil | ||
|
||
import ( | ||
"bytes" | ||
"encoding/csv" | ||
"io" | ||
"reflect" | ||
) | ||
|
||
// Unmarshal parses the CSV-encoded data and stores the result in the slice | ||
// pointed to by v. If v is nil or not a pointer to a slice, Unmarshal returns | ||
// an InvalidUnmarshalError. | ||
// | ||
// Unmarshal uses the std encoding/csv.Reader for parsing and csvutil.Decoder | ||
// for populating the struct elements in the provided slice. For exact decoding | ||
// rules look at the Decoder's documentation. | ||
// | ||
// The first line in data is treated as a header. Decoder will use it to map | ||
// csv columns to struct's fields. | ||
// | ||
// In case of success the provided slice will be reinitialized and its content | ||
// fully replaced with decoded data. | ||
func Unmarshal(data []byte, v interface{}) error { | ||
vv := reflect.ValueOf(v) | ||
|
||
if vv.Kind() != reflect.Ptr || vv.IsNil() { | ||
return &InvalidUnmarshalError{Type: reflect.TypeOf(v)} | ||
} | ||
|
||
if vv.Type().Elem().Kind() != reflect.Slice { | ||
return &InvalidUnmarshalError{Type: vv.Type()} | ||
} | ||
|
||
typ := vv.Type().Elem() | ||
slice := reflect.MakeSlice(typ, 0, 2) | ||
|
||
dec, err := NewDecoder(csv.NewReader(bytes.NewReader(data))) | ||
if err == io.EOF { | ||
return nil | ||
} else if err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
elem := reflect.New(typ.Elem()) | ||
if err := dec.Decode(elem.Interface()); err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return err | ||
} | ||
slice = reflect.Append(slice, elem.Elem()) | ||
} | ||
|
||
vv.Elem().Set(slice) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package csvutil | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestUnmarshal(t *testing.T) { | ||
fixture := []struct { | ||
desc string | ||
src []byte | ||
in interface{} | ||
out interface{} | ||
}{ | ||
{ | ||
desc: "type with two records", | ||
src: []byte("String,int\nstring1,1\nstring2,2"), | ||
in: new([]TypeI), | ||
out: &[]TypeI{ | ||
{"string1", 1}, | ||
{"string2", 2}, | ||
}, | ||
}, | ||
{ | ||
desc: "pointer types with two records", | ||
src: []byte("String,int\nstring1,1\nstring2,2"), | ||
in: &[]*TypeI{}, | ||
out: &[]*TypeI{ | ||
{"string1", 1}, | ||
{"string2", 2}, | ||
}, | ||
}, | ||
{ | ||
desc: "header only", | ||
src: []byte("String,int\n"), | ||
in: &[]TypeI{}, | ||
out: &[]TypeI{}, | ||
}, | ||
{ | ||
desc: "no data", | ||
src: []byte(""), | ||
in: &[]TypeI{}, | ||
out: &[]TypeI{}, | ||
}, | ||
} | ||
|
||
for _, f := range fixture { | ||
t.Run(f.desc, func(t *testing.T) { | ||
if err := Unmarshal(f.src, f.in); err != nil { | ||
t.Fatalf("want err=nil; got %v", err) | ||
} | ||
|
||
if !reflect.DeepEqual(f.in, f.out) { | ||
t.Errorf("want out=%v; got %v", f.out, f.in) | ||
} | ||
}) | ||
} | ||
|
||
t.Run("test invalid arguments", func(t *testing.T) { | ||
var fixtures = []struct { | ||
desc string | ||
v interface{} | ||
expected string | ||
}{ | ||
{"nil interface", interface{}(nil), "csvutil: Unmarshal(nil)"}, | ||
{"nil", nil, "csvutil: Unmarshal(nil)"}, | ||
{"non pointer struct", struct{}{}, "csvutil: Unmarshal(non-pointer struct {})"}, | ||
{"non-slice pointer", (*int)(nil), "csvutil: Unmarshal(non-slice pointer)"}, | ||
} | ||
|
||
for _, f := range fixtures { | ||
t.Run(f.desc, func(t *testing.T) { | ||
err := Unmarshal([]byte(""), f.v) | ||
if err == nil { | ||
t.Fatalf("want err != nil") | ||
} | ||
if got := err.Error(); got != f.expected { | ||
t.Errorf("want err=%s; got %s", f.expected, got) | ||
} | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters