Skip to content

Commit

Permalink
feat: Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
codingconcepts committed Apr 22, 2024
1 parent f75f9d9 commit da1e6a9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ go.work
# Application-specific
csvs
releases
scratchpad
scratchpad

# Code editor
.vscode/
19 changes: 15 additions & 4 deletions dg.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ func main() {
log.Fatalf("error generating tables: %v", err)
}

removeSuppressedColumns(c, tt, files)
if err = removeSuppressedColumns(c, tt, files); err != nil {
log.Fatalf("error removing supressed columns: %v", err)
}

if err := writeFiles(*outputDir, files, tt); err != nil {
log.Fatalf("error writing csv files: %v", err)
Expand Down Expand Up @@ -208,7 +210,11 @@ func generateTable(t model.Table, files map[string]model.CSVFile, tt ui.TimerFun
}
}

file := files[t.Name]
file, ok := files[t.Name]
if !ok {
return fmt.Errorf("missing table: %q", t.Name)
}

if len(file.UniqueColumns) > 0 {
file.Lines = generator.Transpose(file.Lines)
file.Lines = file.Unique()
Expand All @@ -219,14 +225,17 @@ func generateTable(t model.Table, files map[string]model.CSVFile, tt ui.TimerFun
return nil
}

func removeSuppressedColumns(c model.Config, tt ui.TimerFunc, files map[string]model.CSVFile) {
func removeSuppressedColumns(c model.Config, tt ui.TimerFunc, files map[string]model.CSVFile) error {
for _, table := range c.Tables {
for _, column := range table.Columns {
if !column.Suppress {
continue
}

file := files[table.Name]
file, ok := files[table.Name]
if !ok {
return fmt.Errorf("missing table: %q", table.Name)
}

// Remove suppressed column from header.
var headerIndex int
Expand All @@ -244,6 +253,8 @@ func removeSuppressedColumns(c model.Config, tt ui.TimerFunc, files map[string]m
files[table.Name] = file
}
}

return nil
}

func writeFiles(outputDir string, cfs map[string]model.CSVFile, tt ui.TimerFunc) error {
Expand Down
7 changes: 1 addition & 6 deletions internal/pkg/generator/const_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ func (g ConstGenerator) Generate(t model.Table, c model.Column, files map[string
return fmt.Errorf("wrong number of values provided for const generator (need %d, got %d)", count, len(g.Values))
}

var line []string
for _, value := range g.Values {
line = append(line, value)
}

AddTable(t, c.Name, line, files)
AddTable(t, c.Name, g.Values, files)
return nil
}
4 changes: 4 additions & 0 deletions internal/pkg/generator/each_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (g EachGenerator) Generate(t model.Table, files map[string]model.CSVFile) e
srcColumn := gCol.Column
srcColumnIndex := lo.IndexOf(srcTable.Header, srcColumn)

if len(srcTable.Lines)-1 < srcColumnIndex {
return fmt.Errorf("column %q out of bounds for table %q", srcColumn, srcTable.Name)
}

preCartesian = append(preCartesian, srcTable.Lines[srcColumnIndex])
}

Expand Down

0 comments on commit da1e6a9

Please sign in to comment.