Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand Down
10 changes: 7 additions & 3 deletions internal/validation/txt_strategy.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package validation

import (
"errors"
"fmt"
"os"
"strings"

"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 {

Check failure on line 15 in internal/validation/txt_strategy.go

View workflow job for this annotation

GitHub Actions / Lint

calculated cyclomatic complexity for function Write is 11, max is 10 (cyclop)
if r == nil {
return errors.New("report is nil")
}
var b strings.Builder

stats := r.Statistics
Expand Down
2 changes: 1 addition & 1 deletion internal/validation/txt_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading