gocarina / gocsv

The GoCSV package aims to provide easy CSV serialization and deserialization to the golang programming language
MIT License
1.99k stars 245 forks source link

Cannot correctly marshal field that contains word "partial" #274

Open MojoML opened 7 months ago

MojoML commented 7 months ago

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: https://github.com/gocarina/gocsv/blob/b87c2d0e983ae4eea4ee48f33de22d51ddd22f03/reflect.go#L228