From dd2be2446da63396ccae3d0bbfd5c08559424c70 Mon Sep 17 00:00:00 2001 From: Marco Mayer Date: Thu, 4 Aug 2022 12:12:46 +0200 Subject: [PATCH] add TargetName to cols to allow renaming header col names --- pkg/csvexport/csv.go | 15 ++++++++++++++- pkg/csvexport/csv_test.go | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/pkg/csvexport/csv.go b/pkg/csvexport/csv.go index 34cff4a..e1d503a 100644 --- a/pkg/csvexport/csv.go +++ b/pkg/csvexport/csv.go @@ -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{} @@ -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) @@ -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 { @@ -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)) diff --git a/pkg/csvexport/csv_test.go b/pkg/csvexport/csv_test.go index 383b2ec..7f81a0e 100644 --- a/pkg/csvexport/csv_test.go +++ b/pkg/csvexport/csv_test.go @@ -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)) +}