diff options
author | Victor Häggqvist <[email protected]> | 2022-09-14 23:54:17 +0200 |
---|---|---|
committer | Victor Häggqvist <[email protected]> | 2022-09-14 23:54:23 +0200 |
commit | 1bbce1aa65e4af84a144b137ea2037004eb2881d (patch) | |
tree | fa02ab592746556a884a8e75d480fb3804d26805 /vendor/github.com/gocarina/gocsv/unmarshaller.go | |
parent | 4e0fb944f18411381366e620ef5193f886957b6f (diff) |
go bump
Diffstat (limited to '')
-rw-r--r-- | vendor/github.com/gocarina/gocsv/unmarshaller.go | 20 |
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 +} |