Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: provide more guardrails for bulkins tool #157

Merged
merged 1 commit into from
Aug 22, 2024
Merged
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 @@ -7,6 +7,7 @@ import (
"io"
"math"
"os"
"reflect"
"strconv"
"time"

Expand Down Expand Up @@ -36,7 +37,7 @@ func main() {
for i, line := range records {
semaphore <- true

go func(lineNumber int, currentLine []string) {
go func(rowNumber int, currentLine []string) {
goroutineStartTime := time.Now()

tracking_code := currentLine[0]
Expand All @@ -62,7 +63,7 @@ func main() {
semaphore <- true
}

file, err := os.Create("insurance_buy_results.csv")
file, err := os.Create("bulkins_results.csv")
handleGoErr(err)
defer file.Close()
writer := csv.NewWriter(file)
Expand All @@ -85,7 +86,7 @@ func getCsvRecords() [][]string {
defer file.Close()
reader := csv.NewReader(file)

lineNumber := 0
rowNumber := 1
records := make([][]string, 0)
for {
record, err := reader.Read()
Expand All @@ -101,16 +102,33 @@ func getCsvRecords() [][]string {
}

// Skip header
if lineNumber == 0 {
lineNumber++
if rowNumber == 1 {
if !reflect.DeepEqual(record, []string{"Tracking Code", "Reference (optional)", "Carrier String", "Amount", "To Address ID (optional)", "From Address ID (optional)"}) {
handleGoErr(errors.New("csv does not contain the proper header row"))
}
rowNumber++
continue
}

// Ensure all required data is present before sending requests
tracking_code := record[0]
carrier_string := record[2]
amount := record[3]
if tracking_code == "" {
handleGoErr(fmt.Errorf("tracking code column is missing data for row %v", rowNumber))
}
if carrier_string == "" {
handleGoErr(fmt.Errorf("carrier string column is missing data for row %v", rowNumber))
}
if amount == "" {
handleGoErr(fmt.Errorf("amount column is missing data for row %v", rowNumber))
}

// Ensure `Amount` column is a valid float
_, err = strconv.ParseFloat(record[3], 64)
_, err = strconv.ParseFloat(amount, 64)
handleGoErr(err)

lineNumber++
rowNumber++
records = append(records, record)
}

Expand Down