summaryrefslogtreecommitdiff
path: root/vendor/github.com/gocarina/gocsv/csv.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/gocarina/gocsv/csv.go')
-rw-r--r--vendor/github.com/gocarina/gocsv/csv.go76
1 files changed, 68 insertions, 8 deletions
diff --git a/vendor/github.com/gocarina/gocsv/csv.go b/vendor/github.com/gocarina/gocsv/csv.go
index 22846c0..3ba3efb 100644
--- a/vendor/github.com/gocarina/gocsv/csv.go
+++ b/vendor/github.com/gocarina/gocsv/csv.go
@@ -127,6 +127,15 @@ func MarshalString(in interface{}) (out string, err error) {
return bufferString.String(), nil
}
+// MarshalStringWithoutHeaders returns the CSV string from the interface.
+func MarshalStringWithoutHeaders(in interface{}) (out string, err error) {
+ bufferString := bytes.NewBufferString(out)
+ if err := MarshalWithoutHeaders(in, bufferString); err != nil {
+ return "", err
+ }
+ return bufferString.String(), nil
+}
+
// MarshalBytes returns the CSV bytes from the interface.
func MarshalBytes(in interface{}) (out []byte, err error) {
bufferString := bytes.NewBuffer(out)
@@ -149,17 +158,22 @@ func MarshalWithoutHeaders(in interface{}, out io.Writer) (err error) {
}
// MarshalChan returns the CSV read from the channel.
-func MarshalChan(c <-chan interface{}, out *SafeCSVWriter) error {
- return writeFromChan(out, c)
+func MarshalChan(c <-chan interface{}, out CSVWriter) error {
+ return writeFromChan(out, c, false)
+}
+
+// MarshalChanWithoutHeaders returns the CSV read from the channel.
+func MarshalChanWithoutHeaders(c <-chan interface{}, out CSVWriter) error {
+ return writeFromChan(out, c, true)
}
// MarshalCSV returns the CSV in writer from the interface.
-func MarshalCSV(in interface{}, out *SafeCSVWriter) (err error) {
+func MarshalCSV(in interface{}, out CSVWriter) (err error) {
return writeTo(out, in, false)
}
// MarshalCSVWithoutHeaders returns the CSV in writer from the interface.
-func MarshalCSVWithoutHeaders(in interface{}, out *SafeCSVWriter) (err error) {
+func MarshalCSVWithoutHeaders(in interface{}, out CSVWriter) (err error) {
return writeTo(out, in, true)
}
@@ -216,6 +230,43 @@ func UnmarshalCSV(in CSVReader, out interface{}) error {
return readTo(csvDecoder{in}, out)
}
+// UnmarshalCSVToMap parses a CSV of 2 columns into a map.
+func UnmarshalCSVToMap(in CSVReader, out interface{}) error {
+ decoder := NewSimpleDecoderFromCSVReader(in)
+ header, err := decoder.GetCSVRow()
+ if err != nil {
+ return err
+ }
+ if len(header) != 2 {
+ return fmt.Errorf("maps can only be created for csv of two columns")
+ }
+ outValue, outType := getConcreteReflectValueAndType(out)
+ if outType.Kind() != reflect.Map {
+ return fmt.Errorf("cannot use " + outType.String() + ", only map supported")
+ }
+ keyType := outType.Key()
+ valueType := outType.Elem()
+ outValue.Set(reflect.MakeMap(outType))
+ for {
+ key := reflect.New(keyType)
+ value := reflect.New(valueType)
+ line, err := decoder.GetCSVRow()
+ if err == io.EOF {
+ break
+ } else if err != nil {
+ return err
+ }
+ if err := setField(key, line[0], false); err != nil {
+ return err
+ }
+ if err := setField(value, line[1], false); err != nil {
+ return err
+ }
+ outValue.SetMapIndex(key.Elem(), value.Elem())
+ }
+ return nil
+}
+
// UnmarshalToChan parses the CSV from the reader and send each value in the chan c.
// The channel must have a concrete type.
func UnmarshalToChan(in io.Reader, c interface{}) error {
@@ -278,7 +329,13 @@ func UnmarshalToCallback(in io.Reader, f interface{}) error {
if !notClosed || v.Interface() == nil {
break
}
- valueFunc.Call([]reflect.Value{v})
+ callResults := valueFunc.Call([]reflect.Value{v})
+ // if last returned value from Call() is an error, return it
+ if len(callResults) > 0 {
+ if err, ok := callResults[len(callResults)-1].Interface().(error); ok {
+ return err
+ }
+ }
}
return nil
}
@@ -340,7 +397,7 @@ func UnmarshalToCallbackWithError(in io.Reader, f interface{}) error {
return fmt.Errorf("the given function must have exactly one return value")
}
if !isErrorType(t.Out(0)) {
- return fmt.Errorf("the given function must only return error.")
+ return fmt.Errorf("the given function must only return error")
}
cerr := make(chan error)
@@ -361,6 +418,9 @@ func UnmarshalToCallbackWithError(in io.Reader, f interface{}) error {
}
v, notClosed := c.Recv()
if !notClosed || v.Interface() == nil {
+ if err := <-cerr; err != nil {
+ fErr = err
+ }
break
}
@@ -405,7 +465,7 @@ func UnmarshalStringToCallbackWithError(in string, c interface{}) (err error) {
// CSVToMap creates a simple map from a CSV of 2 columns.
func CSVToMap(in io.Reader) (map[string]string, error) {
decoder := newSimpleDecoderFromReader(in)
- header, err := decoder.getCSVRow()
+ header, err := decoder.GetCSVRow()
if err != nil {
return nil, err
}
@@ -414,7 +474,7 @@ func CSVToMap(in io.Reader) (map[string]string, error) {
}
m := make(map[string]string)
for {
- line, err := decoder.getCSVRow()
+ line, err := decoder.GetCSVRow()
if err == io.EOF {
break
} else if err != nil {