-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.go
66 lines (61 loc) · 1.74 KB
/
data.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
package data
import (
"fmt"
)
type Source struct {
Header []string
Record []string
ColumnNumber uint
ColumnName string
}
func (s Source) Value() (interface{}, error) {
if s.ColumnNumber < 1 {
if s.Header == nil || len(s.Header) < 1 || s.ColumnName == "" {
return nil, fmt.Errorf("either a ColumnNumber must be specifed in the range 1 to %d or a Header and ColumnName provided", len(s.Record))
}
for index, value := range s.Header {
if value == s.ColumnName {
s.ColumnNumber = uint(index + 1)
break
}
}
if s.ColumnNumber < 1 {
return nil, fmt.Errorf("ColumnName %s does not exist in Header", s.ColumnName)
}
}
if int(s.ColumnNumber) > len(s.Record) {
return nil, fmt.Errorf("invalid column %d, record only contains %d columns", s.ColumnNumber, len(s.Record))
}
return s.Record[s.ColumnNumber-1], nil
}
type Target struct {
Header []string
Record []string
ColumnNumber uint
ColumnName string
}
func (t Target) SetValue(v interface{}) error {
if t.ColumnNumber < 1 {
if t.Header == nil || len(t.Header) < 1 || t.ColumnName == "" {
return fmt.Errorf("either a ColumnNumber must be specifed in the range 1 to %d or a Header and ColumnName provided", len(t.Record))
}
for index, value := range t.Header {
if value == t.ColumnName {
t.ColumnNumber = uint(index + 1)
break
}
}
if t.ColumnNumber < 1 {
return fmt.Errorf("ColumnName %s does not exist in Header", t.ColumnName)
}
}
if int(t.ColumnNumber) > len(t.Record) {
return fmt.Errorf("invalid column %d, record only contains %d columns", t.ColumnNumber, len(t.Record))
}
s, ok := v.(string)
if !ok {
return fmt.Errorf("value has invalid type %T, expected string", v)
}
t.Record[t.ColumnNumber-1] = s
return nil
}