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

Unable to unmarshal empty list of structs after marshal #214

Closed Woody1193 closed 2 years ago

Woody1193 commented 2 years ago

If I declare a struct like this:

type Option struct {
    Additional  []*Additional
    Key string
    Value string
}

type Additional struct {
    Type string
    Value int
}

and then I marshal an object of this type:

data = []*Option{
    {
        Key: "test",
        Value: "test"
    }
}

file, _ := os.Create("test.csv")
innerWriter := csv.NewWriter(file)
innerWriter.Comma = '|'
csvWriter := gocsv.NewSafeCSVWriter(innerWriter)
if err := gocsv.MarshalCSV(data, csvWriter); err != nil {
    panic(err)
}

This will produce the following output:

Additional|Key|Value
|test|test

However, attempting to unmarshal this data using the following code:

file, _ := os.Open("test.csv")
var data []*Option
innerReader := csv.NewReader(file)
innerReader.Comma = '|'
if err := gocsv.UnmarshalCSV(innerReader, &data); err != nil {
    panic(err)
}

I get the following error:

record on line 0; parse error on line 2, column 1: unexpected end of JSON input

It seems to me that the unmarshaller is not correctly determining that an empty column equates to an empty list in the case of a list of structs.

Woody1193 commented 2 years ago

This issue has been addressed. Closing.