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

Fix duplicate nested function declaration and handle merging column headers #25

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions internal/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,36 @@ func (m *Merger) CombineCSVFiles(filenames []string, cols []string, outputFilena
}

func (m *Merger) combine(w *csv.Writer, files []string, columns []string) {
//first try at this will be a naive impl:
// 1. read in records of each input file, write columns with matching headers; load everything into memory
// Merge similarly named column headers to the same column
log.Debug("columns to keep", "columns", columns)
mergedColumns := make(map[string][]string)
for _, f := range files {
reader := csv.NewReader(openFile(f))
records, _ := reader.ReadAll()
rows := make([][]string, len(records))
indexes := ColumnIndexes(records[0], columns)
for i := 0; i < len(records); i++ {
var cols []string
for _, col := range indexes {
cols = append(cols, records[i][col]) //the columns we are using in the output file
}
rows[i] = append(rows[i], cols...) //the rows for this file
}
for _, col := range indexes {
header := records[0][col]
for i := 0; i < len(records); i++ {
value := records[i][col]
mergedColumns[header] = append(mergedColumns[header], value)
}
}
}

err := w.WriteAll(rows)
if err != nil {
LogPanic("", err)
}
w.Flush()
fmt.Printf("%v <- %s\n", m.OutputFileName, f)
// Write merged columns to the output file
rows := make([][]string, len(mergedColumns))
i := 0
for _, values := range mergedColumns {
rows[i] = values
i++
}

err := w.WriteAll(rows)
if err != nil {
LogPanic("", err)
}
w.Flush()
fmt.Printf("%v <- %s\n", m.OutputFileName, files)
}

// AppendCSVFiles appends the files in the array to the output file (writer)
Expand Down Expand Up @@ -134,8 +141,4 @@ func writeLine(w *csv.Writer, line []string) {
}
}

func copyTo(r *csv.Reader, w *csv.Writer) {
for line, b := readline(r); b; line, b = readline(r) {
writeLine(w, line)
}
}
// copyTo function is unused, removing it