gocarina / gocsv

The GoCSV package aims to provide easy CSV serialization and deserialization to the golang programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot correctly marshal field that contains word "partial"

MojoML opened this issue · comments

Description:
I have a Struct with the following field and tags
PartialDeliveryNumber uint `json:"partialDeliveryNumber" csv:"partial_delivery_number"`

I have a test that reads in some json data and uploads it to Redshift. In order to upload this data,
the column names have to be extracted from the records with this function:

// columns are determined by csv tags on the model
func GetColumns[T Model](records []T) ([]string, error) {
	var b bytes.Buffer
	if err := gocsv.Marshal(records, &b); err != nil {
		return nil, fmt.Errorf("failed to marshal csv headers: %w", err)
	}
	sc := bufio.NewScanner(bytes.NewReader(b.Bytes()))
	sc.Scan()
	cols := sc.Text()
	return strings.Split(cols, ","), nil
}

Problem:
As soon as the Struct Field is named "PartialDeliveryNumber", the csv tag is ignored and "PartialDeliveryNumber" is returned as the column name. If I name the struct as follows, no issues occur:
PDN uint `json:"partialDeliveryNumber" csv:"partial_delivery_number"`

After some debugging we found that the issue probably relates to this line in the library:

} else if strings.HasPrefix(trimmedFieldTagEntry, "partial") {