frictionlessdata / tableschema-go

A Go library for working with Table Schema.
MIT License
46 stars 10 forks source link

csv.NewWriter doesn't make a file #61

Closed fils closed 6 years ago

fils commented 6 years ago

Using your code on the main README as a guide I have lines like in https://github.com/OpenCoreData/ocdGarden/blob/master/frictionlessdata/ocdFricTest1/main.go Lines 95-108

No errors and runs fine, but no file is made. Tried adding in some manual w.Flush() calls but didn't work.

I also tried

    err = w.Error()
    if err != nil {
        log.Fatal(err)
    }

but it return error as nil, so nothing to report.

Any guidance on what I am doing wrong?

Also, have you thought of a method like w.WriteAll() like in the golang csv package?

danielfireman commented 6 years ago

Is that possible that line 106 is returning an error? Could you please check and let me know how it goes?

About the WriteAll method, it does exist in the interface. Actually, csv.NewWriter returns a csv.Writer :)

fils commented 6 years ago

So I updated the code (and pushed it up.. same link) https://github.com/OpenCoreData/ocdGarden/blob/master/frictionlessdata/ocdFricTest1/main.go

I added

               err = w.Error()
        if err != nil {
            log.Fatal(err)
        }

in a few places and got nothing. Is that the correct way to look for errors?

Then I found an issue.. I had my struct type wrong. However, how this is affecting the running code is interesting.

At line 13 I had my Leg field in my struct as type "string" when in the schema.json it is "int". running with this mismatch results in no error, and no file.

If I change line 13 to int and run I get the following.

.... (removed some extra output doing simple field printing
2017/10/09 06:57:49 21 - Calib_density_m0
2017/10/09 06:57:49 22 - Calib_density_m1
2017/10/09 06:57:49 23 - Calib_density_mse
2017/10/09 06:57:49 Field Section_number exists in schema
2017/10/09 06:57:49 Unmarshal to value: 123
{208 1262 C 90}
panic: runtime error: index out of range

goroutine 1 [running]:
opencoredata.org/ocdGarden/frictionlessdata/ocdFricTest1/vendor/github.com/frictionlessdata/tableschema-go/schema.(*Schema).Encode(0xc42011e000, 0x1299a80, 0xc42007f9b0, 0xc42007f9b0, 0x0, 0x0, 0x0, 0x0)
        /Users/dfils/src/go/src/opencoredata.org/ocdGarden/frictionlessdata/ocdFricTest1/vendor/github.com/frictionlessdata/tableschema-go/schema/schema.go:249 +0x502
main.validateSchema1()
        /Users/dfils/src/go/src/opencoredata.org/ocdGarden/frictionlessdata/ocdFricTest1/main.go:109 +0x966
main.main()
        /Users/dfils/src/go/src/opencoredata.org/ocdGarden/frictionlessdata/ocdFricTest1/main.go:21 +0x20
exit status 2

Thanks for the note re: WriteAll. Oddly VSCode doesn't show it as an function option (my bad for crutching on my editor and not reading the docs.).

danielfireman commented 6 years ago

Hi @fils,Thanks for reporting this error. I fixed it on my last commit and just pushed version 0.1.4 with the fix (and some more stuff).

I also noticed that you didn't check the error returned at schema.DecodeTable. Could you please do so? This check is important because we can not guarantee the integrity of the returned value in case of errors.

fils commented 6 years ago

@danielfireman looks good.. FYI, I think in main README you want the demo code for the WriteSummary function to be

 f, _ := os.Create(path)

not os.Open.

Thanks! issue resolved for me!

danielfireman commented 6 years ago

Thanks for reporting, @fils ! Already fixed the README.