diff --git a/internal/validation/csv_strategy.go b/internal/validation/csv_writer.go similarity index 68% rename from internal/validation/csv_strategy.go rename to internal/validation/csv_writer.go index f82e752..72535a9 100644 --- a/internal/validation/csv_strategy.go +++ b/internal/validation/csv_writer.go @@ -2,16 +2,23 @@ package validation import ( "encoding/csv" + "errors" "os" "strings" "github.com/ChargePi/chargeflow/pkg/report" ) -// csvStrategy implements OutputStrategy for CSV output. -type csvStrategy struct{} +var headers = []string{"message_id", "type", "errors"} + +// csvWriter implements ReportWriter for CSV output. +type csvWriter struct{} + +func (csvWriter) Write(path string, r *report.Report) error { + if r == nil { + return errors.New("report is nil") + } -func (csvStrategy) Write(path string, r *report.Report) error { f, err := os.Create(path) if err != nil { return err @@ -22,7 +29,7 @@ func (csvStrategy) Write(path string, r *report.Report) error { defer w.Flush() // Header - if err = w.Write([]string{"message_id", "type", "errors"}); err != nil { + if err = w.Write(headers); err != nil { return err } diff --git a/internal/validation/csv_strategy_test.go b/internal/validation/csv_writer_test.go similarity index 98% rename from internal/validation/csv_strategy_test.go rename to internal/validation/csv_writer_test.go index 1b9a37d..273a6ec 100644 --- a/internal/validation/csv_strategy_test.go +++ b/internal/validation/csv_writer_test.go @@ -28,7 +28,7 @@ func TestCSVStrategy_Write(t *testing.T) { Statistics: report.Statistics{}, } - s := csvStrategy{} + s := csvWriter{} require.NoError(t, s.Write(path, r)) b, err := os.ReadFile(path) diff --git a/internal/validation/json_strategy.go b/internal/validation/json_writer.go similarity index 74% rename from internal/validation/json_strategy.go rename to internal/validation/json_writer.go index f86b45d..d886c08 100644 --- a/internal/validation/json_strategy.go +++ b/internal/validation/json_writer.go @@ -2,15 +2,20 @@ package validation import ( "encoding/json" + "errors" "os" "github.com/ChargePi/chargeflow/pkg/report" ) -// jsonStrategy implements OutputStrategy for JSON output. +// jsonStrategy implements ReportWriter for JSON output. type jsonStrategy struct{} func (jsonStrategy) Write(path string, r *report.Report) error { + if r == nil { + return errors.New("report is nil") + } + b, err := json.MarshalIndent(r, "", " ") if err != nil { return err diff --git a/internal/validation/json_strategy_test.go b/internal/validation/json_writer_test.go similarity index 100% rename from internal/validation/json_strategy_test.go rename to internal/validation/json_writer_test.go diff --git a/internal/validation/output_strategy.go b/internal/validation/report_writer.go similarity index 63% rename from internal/validation/output_strategy.go rename to internal/validation/report_writer.go index 6593322..34bc18d 100644 --- a/internal/validation/output_strategy.go +++ b/internal/validation/report_writer.go @@ -9,28 +9,28 @@ import ( "github.com/ChargePi/chargeflow/pkg/report" ) -// OutputStrategy defines how to write a validation report. -type OutputStrategy interface { +// ReportWriter defines how to write a validation report. +type ReportWriter interface { Write(path string, r *report.Report) error } -// outputStrategyFactory returns an OutputStrategy based on the file extension. -func outputStrategyFactory(path string) (OutputStrategy, error) { +// outputStrategyFactory returns an ReportWriter based on the file extension. +func outputStrategyFactory(path string) (ReportWriter, error) { ext := strings.ToLower(filepath.Ext(path)) switch ext { case ".json": return jsonStrategy{}, nil case ".csv": - return csvStrategy{}, nil + return csvWriter{}, nil case ".txt": - return txtStrategy{}, nil + return txtWriter{}, nil default: return nil, errors.Errorf("unsupported output extension: %s", ext) } } // WriteReport is a convenience exported helper that writes the report using the -// appropriate OutputStrategy based on the provided path extension. +// appropriate ReportWriter based on the provided path extension. func WriteReport(path string, r *report.Report) error { strat, err := outputStrategyFactory(path) if err != nil { diff --git a/internal/validation/output_strategy_test.go b/internal/validation/report_writer_test.go similarity index 89% rename from internal/validation/output_strategy_test.go rename to internal/validation/report_writer_test.go index d75a3d1..1c9e66f 100644 --- a/internal/validation/output_strategy_test.go +++ b/internal/validation/report_writer_test.go @@ -34,9 +34,9 @@ func TestOutputStrategyFactory(t *testing.T) { case ".json": require.IsType(t, jsonStrategy{}, strat) case ".csv": - require.IsType(t, csvStrategy{}, strat) + require.IsType(t, csvWriter{}, strat) case ".txt": - require.IsType(t, txtStrategy{}, strat) + require.IsType(t, txtWriter{}, strat) } }) } diff --git a/internal/validation/txt_strategy.go b/internal/validation/txt_strategy.go index 7af06cd..f244f85 100644 --- a/internal/validation/txt_strategy.go +++ b/internal/validation/txt_strategy.go @@ -1,6 +1,7 @@ package validation import ( + "errors" "fmt" "os" "strings" @@ -8,10 +9,13 @@ import ( "github.com/ChargePi/chargeflow/pkg/report" ) -// txtStrategy implements OutputStrategy for plain text output. -type txtStrategy struct{} +// txtWriter implements ReportWriter for plain text output. +type txtWriter struct{} -func (txtStrategy) Write(path string, r *report.Report) error { +func (txtWriter) Write(path string, r *report.Report) error { + if r == nil { + return errors.New("report is nil") + } var b strings.Builder stats := r.Statistics diff --git a/internal/validation/txt_strategy_test.go b/internal/validation/txt_strategy_test.go index c103134..4a0cbeb 100644 --- a/internal/validation/txt_strategy_test.go +++ b/internal/validation/txt_strategy_test.go @@ -27,7 +27,7 @@ func TestTXTStrategy_Write(t *testing.T) { Statistics: report.Statistics{ValidRequests: 0, InvalidRequests: 0, ValidResponses: 0, InvalidResponses: 1, UnparsableMessages: 1}, } - s := txtStrategy{} + s := txtWriter{} require.NoError(t, s.Write(path, r)) b, err := os.ReadFile(path)