Under the existing design, user has to manually modify their csv string to match with csv reader defaults.
func (r *Rows) FromCSVString(s string) *Rows function initialize a csv reader with defaults. The user may have a pipe separated csv or some other token. Also, the user cannot enable lazyQoutes (see: https://cs.opensource.google/go/go/+/refs/tags/go1.22.4:src/encoding/csv/reader.go;l=136) which is sometimes needed if you have a json string in the csv file without having to modify the csv extensively to match the defaults.
My proposal is to allow the user to define their csv reader freely with their own custom options.
Easiest way to do it without breaking existing code is the following:
func (r *Rows) FromCSVReader(csvReader *csv.Reader) *Rows {
for {
res, err := csvReader.Read()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
panic(fmt.Sprintf("Parsing CSV string failed: %s", err.Error()))
}
row := make([]driver.Value, len(r.cols))
for i, v := range res {
row[i] = CSVColumnParser(strings.TrimSpace(v))
}
r.rows = append(r.rows, row)
}
return r
}
Proposal
Under the existing design, user has to manually modify their csv string to match with csv reader defaults.
func (r *Rows) FromCSVString(s string) *Rows
function initialize a csv reader with defaults. The user may have a pipe separated csv or some other token. Also, the user cannot enable lazyQoutes (see: https://cs.opensource.google/go/go/+/refs/tags/go1.22.4:src/encoding/csv/reader.go;l=136) which is sometimes needed if you have a json string in the csv file without having to modify the csv extensively to match the defaults.My proposal is to allow the user to define their csv reader freely with their own custom options. Easiest way to do it without breaking existing code is the following:
you can also check this commit that I pushed on my fork: https://github.com/stayingalivee/go-sqlmock/commit/d0736ed6b8a4052c2198ebf41cae30063e0c3d35
Use-cases
I added a test to demonstrate the use-case. see: https://github.com/stayingalivee/go-sqlmock/commit/d0736ed6b8a4052c2198ebf41cae30063e0c3d35#diff-335b77abfb8789fd51a279c7cbdbdae90b384df153ce1bbac40aa1468d5bbb89R472
for ease of access I will paste the test below:
If you find this helpful let me know so I can submit a pull request.