Skip to content

Commit

Permalink
Initial lambda commit
Browse files Browse the repository at this point in the history
  • Loading branch information
GPFAFF committed Feb 12, 2020
0 parents commit d756918
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.csv
95 changes: 95 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"bufio"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"strings"
)

// Utilization is the struct which represents vehicle data.
type Utilization struct {
VIN string `json:"VIN"`
OrigDealerID string `json:"OrigDealerID"`
*ProgramDetails
}

// ProgramDetails represents the vehicle data for active and terminated contracts.
type ProgramDetails struct {
ProgramCode string `json:"ProgramCode"`
ContractDate string `json:",omitempty"`
PayoffProcessingDate string `json:",omitempty"`
}

var (
// ErrNameNotProvided is thrown when a name is not provided
ErrNameNotProvided = errors.New("no name was provided in the HTTP body")
)

func main() {
readFile("one_active.csv")
readFile("two.csv")
}

func createVehicleReport(name string, line []string) {
var vehicle []Utilization

if strings.Contains(name, "active") {
vehicle = append(vehicle, Utilization{
VIN: line[1],
OrigDealerID: line[5],
ProgramDetails: &ProgramDetails{
ProgramCode: line[8],
ContractDate: line[9],
},
})
} else {
vehicle = append(vehicle, Utilization{
VIN: line[1],
OrigDealerID: line[2],
ProgramDetails: &ProgramDetails{
ProgramCode: line[5],
PayoffProcessingDate: line[9],
},
})
}
utilizationJSON, _ := json.Marshal(vehicle)
fmt.Println("OUTPUT", string(utilizationJSON))
}

func readFile(name string) {

csvFile, err := os.Open(name)
if err != nil {
log.Fatalf("Cannot read '%s': %s\n", name, err.Error())
}
defer csvFile.Close()
reader := csv.NewReader(bufio.NewReader(csvFile))
reader.Comma = '|'
var header = true

for {
line, err := reader.Read()

// skip header csv values
if header {
header = false
continue
}

if err == io.EOF {
break
}

if err != nil {
log.Fatalf("Cannot read '%s': %s\n", line, err.Error())
}

createVehicleReport(name, line)
}
}

0 comments on commit d756918

Please sign in to comment.