generated from koddr/template-go
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinputs.go
60 lines (49 loc) · 1.38 KB
/
inputs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"bufio"
"encoding/csv"
"fmt"
"os"
"path/filepath"
"unicode/utf8"
)
// newInputs provides a new data input instance.
func newInputs(config *Config) (*Inputs, error) {
// Open the given file with input data.
file, err := os.Open(filepath.Clean(inputDataFilePath))
if err != nil {
return nil, fmt.Errorf("can't open file with input data: %v", err)
}
defer file.Close()
// Set up CSV reader.
csvReader := csv.NewReader(bufio.NewReader(file))
csvReader.LazyQuotes = true
// Check, if the CSV column separator is not empty in config.
if config.CSVColumnSeparator != "" {
// Decode rune from the string.
separator, _ := utf8.DecodeRuneInString(config.CSVColumnSeparator)
// Set separator to the CSV reader.
csvReader.Comma = separator
}
// Reading CSV.
records, err := csvReader.ReadAll()
if err != nil {
return nil, fmt.Errorf("can't read CSV file with input data: %v", err)
}
// Create a new Inputs struct.
inputs := &Inputs{}
// Loop for get mapping for fields of the CSV file.
for index, data := range records {
// Header is the first row.
if index == 0 {
// Collect header to mapping list.
inputs.Mapping = matchIndexes(config.ColumnsOrder, data)
continue
}
// Collect all other rows to data list.
inputs.Data = append(inputs.Data, data)
}
// Remove duplicates.
inputs.Data = removeDuplicates(inputs.Data)
return inputs, nil
}