-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunc_csvi.go
105 lines (92 loc) · 1.88 KB
/
func_csvi.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
package fgbase
import (
"encoding/csv"
"io"
"os"
)
type csvState struct {
csvreader *csv.Reader
header []int // index of input/output based on header
record []string
}
func find(s string, v []string) int {
for i := range v {
if v[i] == s {
return i
}
}
return -1
}
func csviRdy(n *Node) bool {
if n.Aux == nil {
return false
}
if n.DefaultRdyFunc() {
r := n.Aux.(csvState).csvreader
header := n.Aux.(csvState).header
record, err := r.Read()
if err == io.EOF {
n.Aux = nil
return false
}
check(err)
n.Aux = csvState{r, header, record}
return true
}
return false
}
// FuncCSVI reads a vector of input data values from a Reader and outputs
// them downstream. enums is an optional map from field.enum to an empty interface.
func FuncCSVI(x []Edge, r io.Reader, enums map[string]interface{}) Node {
var fireFunc = func(n *Node) error {
x := n.Dsts
// process data record
record := n.Aux.(csvState).record
header := n.Aux.(csvState).header
l := len(x)
if l > len(record) {
l = len(record)
}
for i := 0; i < l; i++ {
j := header[i]
// n.Tracef("i=%d, j=%d\n", i, j)
// n.Tracef("record=%v, header=%v\n", record, header)
if j >= 0 {
if record[j] == "*" {
continue
}
var v interface{}
var ok bool
if enums != nil {
v, ok = enums[record[j]]
}
if !ok {
v = ParseDatum(record[j])
}
x[i].DstPut(v)
} else {
n.LogError("Named input missing from .csv file: %s\n", x[i].Name)
os.Exit(1)
}
}
return nil
}
var xp []*Edge
for i := range x {
xp = append(xp, &x[i])
}
node := MakeNode("csvi", nil, xp, csviRdy, fireFunc)
r2 := csv.NewReader(r)
// save headers
headers, err := r2.Read()
check(err)
var h []int
for i := range x {
ix := find(x[i].Name, headers)
if ix >= 0 {
h = append(h, ix)
}
}
node.Aux = csvState{csvreader: r2, header: h}
return node
}