@@ -12,16 +12,15 @@ import (
1212 "github.com/pkg/errors"
1313 "io"
1414 "reflect"
15- "strings"
1615)
1716
1817// DeserializeCSV deserializes a CSV or TSV string into a Go instance.
1918// - https://golang.org/pkg/encoding/csv/
20- func DeserializeCSV (input string , format string , input_header []string , input_comment string , input_lazy_quotes bool , inputSkipLines int , input_limit int , output_type reflect.Type ) (interface {}, error ) {
19+ func DeserializeCSV (input io. Reader , format string , input_header []string , input_comment string , inputLazyQuotes bool , inputSkipLines int , inputLimit int , output_type reflect.Type ) (interface {}, error ) {
2120
2221 if output_type .Kind () == reflect .Map {
23- if input_limit != 1 {
24- return nil , errors .Wrap (& ErrInvalidLimit {Value : input_limit }, "DeserializeCSV expects input limit of 1 when output type is of kind map." )
22+ if inputLimit != 1 {
23+ return nil , errors .Wrap (& ErrInvalidLimit {Value : inputLimit }, "DeserializeCSV expects input limit of 1 when output type is of kind map." )
2524 }
2625 if len (input_header ) == 0 {
2726 return nil , errors .New ("deserializeCSV when returning a map type expects a input header" )
@@ -30,11 +29,12 @@ func DeserializeCSV(input string, format string, input_header []string, input_co
3029 return nil , & ErrInvalidKind {Value : output_type .Kind (), Valid : []reflect.Kind {reflect .Array , reflect .Slice , reflect .Map }}
3130 }
3231
33- reader := csv .NewReader (strings . NewReader ( input ) )
32+ reader := csv .NewReader (input )
3433 if format == "tsv" {
3534 reader .Comma = '\t'
3635 }
37- reader .LazyQuotes = input_lazy_quotes
36+ reader .LazyQuotes = inputLazyQuotes
37+ reader .FieldsPerRecord = - 1 // records may have a variable number of fields
3838
3939 if len (input_comment ) > 1 {
4040 return nil , errors .New ("go's encoding/csv package only supports single character comment characters" )
@@ -45,7 +45,7 @@ func DeserializeCSV(input string, format string, input_header []string, input_co
4545 if output_type .Kind () == reflect .Map {
4646 inRow , err := reader .Read ()
4747 if err != nil {
48- return nil , errors .Wrap (err , "Error reading row from input with format csv" )
48+ return nil , errors .Wrap (err , "error reading row from input with format csv" )
4949 }
5050 if len (inRow ) == 0 {
5151 return nil , & ErrEmptyRow {}
@@ -79,14 +79,23 @@ func DeserializeCSV(input string, format string, input_header []string, input_co
7979 if err == io .EOF {
8080 break
8181 } else {
82- return nil , errors .Wrap (err , "Error reading row from input with format csv" )
82+ return nil , errors .Wrap (err , "error reading row from input with format csv" )
8383 }
8484 }
8585 m := reflect .MakeMap (output_type .Elem ())
8686 for i , h := range input_header {
87- m .SetMapIndex (reflect .ValueOf (h ), reflect .ValueOf (inRow [i ]))
87+ if i < len (inRow ) {
88+ m .SetMapIndex (reflect .ValueOf (h ), reflect .ValueOf (inRow [i ]))
89+ }
90+ //else {
91+ // m.SetMapIndex(reflect.ValueOf(h), "")
92+ //}
8893 }
8994 output = reflect .Append (output , m )
95+
96+ if inputLimit > 0 && output .Len () >= inputLimit {
97+ break
98+ }
9099 }
91100
92101 return output .Interface (), nil
0 commit comments