forked from safing/mmdbmeld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource_csv.go
112 lines (101 loc) · 2.37 KB
/
source_csv.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package mmdbmeld
import (
"bufio"
"encoding/csv"
"errors"
"fmt"
"io"
"net"
"os"
)
// CSVSource reads geoip data in csv format.
type CSVSource struct {
file string
reader *csv.Reader
fields []string
types map[string]string
err error
}
// LoadCSVSource returns a new CSVSource.
func LoadCSVSource(input DatabaseInput, types map[string]string) (*CSVSource, error) {
file, err := os.Open(input.File)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
reader := csv.NewReader(bufio.NewReader(file))
reader.FieldsPerRecord = len(input.Fields)
return &CSVSource{
file: input.File,
reader: reader,
fields: input.Fields,
types: types,
}, nil
}
// Name returns an identifying name for the source.
func (csv *CSVSource) Name() string {
return csv.file
}
// NextEntry returns the next entry of the source.
// If nil, nil is returned, stop reading and check Err().
func (csv *CSVSource) NextEntry() (*SourceEntry, error) {
// Check if there is an error, do not read if there is an error.
if csv.err != nil {
return nil, nil //nolint:nilerr
}
// Read and parse line.
row, err := csv.reader.Read()
if err != nil {
csv.err = err
return nil, nil //nolint:nilerr
}
se := &SourceEntry{
Values: make(map[string]SourceValue),
}
for i := 0; i < len(csv.fields); i++ {
fieldName := csv.fields[i]
switch fieldName {
case "from":
fromIP := net.ParseIP(row[i])
if fromIP == nil {
return nil, fmt.Errorf("failed to parse IP %q", row[i])
}
se.From = fromIP
// Force IPv4 representation for IPv4 for better further processing.
if v4 := se.From.To4(); v4 != nil {
se.From = v4
}
case "to":
toIP := net.ParseIP(row[i])
if toIP == nil {
return nil, fmt.Errorf("failed to parse IP %q", row[i])
}
se.To = toIP
// Force IPv4 representation for IPv4 for better further processing.
if v4 := se.To.To4(); v4 != nil {
se.To = v4
}
case "", "-":
// Ignore
default:
fieldType, ok := csv.types[fieldName]
if ok && fieldType != "" && fieldType != "-" {
se.Values[fieldName] = SourceValue{
Type: csv.types[fieldName],
Value: row[i],
}
}
}
}
return se, nil
}
// Err returns the processing error encountered by the source.
func (csv *CSVSource) Err() error {
switch {
case csv.err == nil:
return nil
case errors.Is(csv.err, io.EOF):
return nil
default:
return csv.err
}
}