Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MojoML opened this issue Mar 18, 2024 · 0 comments
Open

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

MojoML opened this issue Mar 18, 2024 · 0 comments

Comments

@MojoML
Copy link

MojoML commented Mar 18, 2024

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") {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant