-
Notifications
You must be signed in to change notification settings - Fork 11
/
file_scanner.go
executable file
·78 lines (69 loc) · 1.84 KB
/
file_scanner.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
package dsc
import (
"encoding/json"
"github.com/viant/toolbox"
"strings"
)
//FileScanner represents a file scanner to transfer record to destinations.
type FileScanner struct {
columns []string
columnTypes []ColumnType
converter toolbox.Converter
Values map[string]interface{}
}
//Columns returns columns of the processed record.
func (s *FileScanner) Columns() ([]string, error) {
return s.columns, nil
}
//Columns returns columns of the processed record.
func (s *FileScanner) ColumnTypes() ([]ColumnType, error) {
return s.columnTypes, nil
}
//Scan reads file record values to assign it to passed in destinations.
func (s *FileScanner) Scan(destinations ...interface{}) (err error) {
if len(destinations) == 1 {
if toolbox.IsMap(destinations[0]) {
var record = toolbox.AsMap(destinations[0])
for k, v := range s.Values {
record[k] = v
}
return nil
}
}
var columns, _ = s.Columns()
for i, dest := range destinations {
if value, found := s.Values[columns[i]]; found {
switch val := value.(type) {
case json.Number:
var number interface{}
if strings.Contains(val.String(), ".") {
number, err = val.Float64()
if err == nil {
err = s.converter.AssignConverted(dest, number)
}
} else {
number, err = val.Int64()
if err == nil {
err = s.converter.AssignConverted(dest, number)
}
}
break
default:
err = s.converter.AssignConverted(dest, value)
}
if err != nil {
return err
}
}
}
return nil
}
//NewFileScanner create a new file scanner, it takes config, and columns as parameters.
func NewFileScanner(config *Config, columns []string, columnTypes []ColumnType) *FileScanner {
converter := toolbox.NewColumnConverter(config.GetDateLayout())
return &FileScanner{
converter: *converter,
columnTypes: columnTypes,
columns: columns,
}
}