Skip to content

Commit

Permalink
fix: Ability to suppress columns from output
Browse files Browse the repository at this point in the history
  • Loading branch information
codingconcepts committed Feb 6, 2024
1 parent 44c2c44 commit a0a2445
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
30 changes: 30 additions & 0 deletions dg.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/codingconcepts/dg/internal/pkg/model"
"github.com/codingconcepts/dg/internal/pkg/source"
"github.com/codingconcepts/dg/internal/pkg/ui"
"github.com/samber/lo"
)

var (
Expand Down Expand Up @@ -64,6 +65,8 @@ func main() {
log.Fatalf("error generating tables: %v", err)
}

removeSuppressedColumns(c, tt, files)

if err := writeFiles(*outputDir, files, tt); err != nil {
log.Fatalf("error writing csv files: %v", err)
}
Expand Down Expand Up @@ -216,6 +219,33 @@ 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) {
for _, table := range c.Tables {
for _, column := range table.Columns {
if !column.Suppress {
continue
}

file := files[table.Name]

// Remove suppressed column from header.
var headerIndex int
file.Header = lo.Reject(file.Header, func(v string, i int) bool {
if v == column.Name {
headerIndex = i
return true
}
return false
})

// Remove suppressed column from lines.
file.Lines = append(file.Lines[:headerIndex], file.Lines[headerIndex+1:]...)

files[table.Name] = file
}
}
}

func writeFiles(outputDir string, cfs map[string]model.CSVFile, tt ui.TimerFunc) error {
defer tt(time.Now(), "wrote all csvs")

Expand Down
16 changes: 16 additions & 0 deletions examples/supress_column/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tables:

- name: example
count: 1
columns:

- name: one
type: gen
processor:
value: ${uint8}

- name: two
suppress: true
type: gen
processor:
value: ${uint64}
1 change: 1 addition & 0 deletions internal/pkg/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Table struct {
type Column struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Suppress bool `yaml:"suppress"`
Generator RawMessage `yaml:"processor"`
}

Expand Down

0 comments on commit a0a2445

Please sign in to comment.