From db3b96624e6c0507d05fb69a78c18fb02c51c905 Mon Sep 17 00:00:00 2001 From: Benjamin Cane Date: Sun, 22 Oct 2023 17:02:57 -0700 Subject: [PATCH] Updating based on ai suggestions --- Makefile | 1 + pkg/airport/airport_test.go | 3 ++- pkg/airport/parsers/csv/csv.go | 41 ++++++++++++++++++++-------------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 74cbea0..0b96297 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ build: docker run --rm -v `pwd`:/build -w /build/functions/build/init tinygo/tinygo:0.25.0 tinygo build -o /build/functions/build/init.wasm -target wasi /build/functions/src/init/main.go docker run --rm -v `pwd`:/build -w /build/functions/build/data/fetch tinygo/tinygo:0.25.0 tinygo build -o /build/functions/build/fetch.wasm -target wasi /build/functions/src/data/fetch/main.go +.PHONY: tests tests: ## Run tests mkdir -p coverage diff --git a/pkg/airport/airport_test.go b/pkg/airport/airport_test.go index df17302..57d3096 100644 --- a/pkg/airport/airport_test.go +++ b/pkg/airport/airport_test.go @@ -1,6 +1,7 @@ package airport import ( + "reflect" "testing" ) @@ -187,7 +188,7 @@ func TestAirport(t *testing.T) { return } - if result != tc.expected { + if !reflect.DeepEqual(result, tc.expected) { t.Errorf("Expected %v, got %v", tc.expected, result) } }) diff --git a/pkg/airport/parsers/csv/csv.go b/pkg/airport/parsers/csv/csv.go index 2f4fa59..56165a5 100644 --- a/pkg/airport/parsers/csv/csv.go +++ b/pkg/airport/parsers/csv/csv.go @@ -30,28 +30,35 @@ func (p *Parser) Parse() ([]airport.Airport, error) { // Create a new CSV Reader reader := csv.NewReader(p.reader) - // Read the CSV file - rec, err := reader.ReadAll() - if err != nil { - return nil, fmt.Errorf("unable to read csv data - %w", err) - } - // Create a slice of Airports - airports := make([]airport.Airport, 0, len(rec)) + var airports []airport.Airport - // Iterate over the CSV records - for _, r := range rec { - // Convert the record to an Airport - a, err := RecordToAirport(r) + // Read each line of the CSV file + for { + // Read the CSV file + rec, err := reader.Read() if err != nil { - // Skip headers and records with not enough fields - if err == ErrIsHeader || err == ErrNotEnoughFields { - continue + // Breakout if we've reached the end of the file + if err == io.EOF { + break + } + return nil, fmt.Errorf("unable to read csv data - %w", err) + } + + // Iterate over the CSV records + for _, r := range rec { + // Convert the record to an Airport + a, err := RecordToAirport(r) + if err != nil { + // Skip headers and records with not enough fields + if err == ErrIsHeader || err == ErrNotEnoughFields { + continue + } + return nil, fmt.Errorf("unable to convert record to airport - %w", err) } - return nil, fmt.Errorf("unable to convert record to airport - %w", err) + // Append the Airport to the slice + airports = append(airports, a) } - // Append the Airport to the slice - airports = append(airports, a) } // Return the slice of Airports