-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d756918
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.csv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |