summaryrefslogtreecommitdiff
path: root/vendor/github.com/gocarina/gocsv/unmarshaller.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/gocarina/gocsv/unmarshaller.go')
-rw-r--r--vendor/github.com/gocarina/gocsv/unmarshaller.go20
1 files changed, 18 insertions, 2 deletions
diff --git a/vendor/github.com/gocarina/gocsv/unmarshaller.go b/vendor/github.com/gocarina/gocsv/unmarshaller.go
index 87e6a8f..50d528e 100644
--- a/vendor/github.com/gocarina/gocsv/unmarshaller.go
+++ b/vendor/github.com/gocarina/gocsv/unmarshaller.go
@@ -2,7 +2,6 @@ package gocsv
import (
"encoding/csv"
- "errors"
"fmt"
"reflect"
)
@@ -15,6 +14,7 @@ type Unmarshaller struct {
MismatchedHeaders []string
MismatchedStructFields []string
outType reflect.Type
+ out interface{}
}
// NewUnmarshaller creates an unmarshaller from a csv.Reader and a struct.
@@ -66,7 +66,7 @@ func validate(um *Unmarshaller, s interface{}, headers []string) error {
}
structInfo := getStructInfo(concreteType) // Get struct info to get CSV annotations.
if len(structInfo.Fields) == 0 {
- return errors.New("no csv struct tags found")
+ return ErrNoStructTags
}
csvHeadersLabels := make([]*fieldInfo, len(headers)) // Used to store the corresponding header <-> position in CSV
headerCount := map[string]int{}
@@ -91,6 +91,7 @@ func validate(um *Unmarshaller, s interface{}, headers []string) error {
um.fieldInfoMap = csvHeadersLabels
um.MismatchedHeaders = mismatchHeaderFields(structInfo.Fields, headers)
um.MismatchedStructFields = mismatchStructFields(structInfo.Fields, headers)
+ um.out = s
return nil
}
@@ -116,3 +117,18 @@ func (um *Unmarshaller) unmarshalRow(row []string, unmatched map[string]string)
}
return outValue.Interface(), nil
}
+
+// RenormalizeHeaders will remap the header names based on the headerNormalizer.
+// This can be used to map a CSV to a struct where the CSV header names do not match in the file but a mapping is known
+func (um *Unmarshaller) RenormalizeHeaders(headerNormalizer func([]string) []string) error {
+ headers := um.Headers
+ if headerNormalizer != nil {
+ headers = headerNormalizer(headers)
+ }
+ err := validate(um, um.out, headers)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}