Skip to content

Commit

Permalink
Merge pull request #3 from spring-media/add-col-header-rename
Browse files Browse the repository at this point in the history
add TargetName to cols to allow renaming header col names
  • Loading branch information
marcomayer authored Aug 4, 2022
2 parents e35b558 + dd2be24 commit 06b6a23
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/csvexport/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Columns []Column

type Column struct {
Name string
// If TargetName is set, Name gets renamed to TargetName in header
TargetName string
// If OverwriteValue is set to true, all values of the column are set to OverwriteWithValue.
OverwriteValue bool
OverwriteWithValue interface{}
Expand Down Expand Up @@ -66,6 +68,7 @@ func DynamoToCSV(db Storage, ctx context.Context, scanOpt ScanOption, opts ...Op
var csvExp CSVExporter

var keyOrder []string
var header []string

for _, opt := range opts {
opt(&csvExp)
Expand All @@ -83,6 +86,14 @@ func DynamoToCSV(db Storage, ctx context.Context, scanOpt ScanOption, opts ...Op
if count == 0 {
for _, v := range csvExp.cols {
keyOrder = append(keyOrder, v.Name)

headerName := v.Name

if v.TargetName != "" {
headerName = v.TargetName
}

header = append(header, headerName)
}

if len(keyOrder) == 0 {
Expand All @@ -91,9 +102,11 @@ func DynamoToCSV(db Storage, ctx context.Context, scanOpt ScanOption, opts ...Op
}

sort.Strings(keyOrder)

header = keyOrder
}

_ = w.Write(keyOrder)
_ = w.Write(header)
}

record := make([]string, 0, len(keyOrder))
Expand Down
18 changes: 18 additions & 0 deletions pkg/csvexport/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,21 @@ func TestDynamoToCSVWithCols(t *testing.T) {
`
assert.Equal(t, expectedCSV, string(b))
}

func TestDynamoToCSVWithColsTargetName(t *testing.T) {
t.Parallel()
db := mockScan{resp: dynamoMockResp}
cols := csvexport.Columns{
csvexport.Column{Name: "ArticleLastUpdated"},
csvexport.Column{Name: "PerformanceLastUpdated", TargetName: "PerformanceUpdatedLast"},
}
b, err := csvexport.DynamoToCSV(db, context.Background(), csvexport.ScanOption{}, csvexport.WithColumns(cols))

assert.Nil(t, err)

expectedCSV := `ArticleLastUpdated,PerformanceUpdatedLast
2022-04-27T08:36:48.386Z,2022-04-27T13:10:30Z
2022-04-28T08:36:48.386Z,2022-04-28T13:10:30Z
`
assert.Equal(t, expectedCSV, string(b))
}

0 comments on commit 06b6a23

Please sign in to comment.