-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrows.go
112 lines (101 loc) · 2.98 KB
/
rows.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 simplecsv
// GetNumberRows returns the number of rows, including the header row
func (s SimpleCsv) GetNumberRows() int {
return len(s)
}
// GetRow returns the row rowNumber
// If rowNumber does not exist, it returns an empty slice and false
func (s SimpleCsv) GetRow(rowNumber int) ([]string, bool) {
if rowNumber >= 0 && rowNumber < len(s) {
return s[rowNumber], true
}
return []string{}, false
}
// GetRowAsMap returns the row as a map
// If the row does not exist, returns second value as false
func (s SimpleCsv) GetRowAsMap(rowNumber int) (map[string]string, bool) {
RowAsMap := make(map[string]string)
if rowNumber >= 0 && rowNumber < len(s) {
headers := s.GetHeaders()
for k, v := range headers {
RowAsMap[v] = s[rowNumber][k]
}
return RowAsMap, true
}
RowAsMap = map[string]string{"": ""}
return RowAsMap, false
}
// AddRow ads a row at the end of the csv
func (s SimpleCsv) AddRow(rowValue []string) (SimpleCsv, bool) {
var added bool
if len(rowValue) == len(s[0]) {
s = append(s, rowValue)
added = true
} else {
added = false
}
return s, added
}
// AddRowFromMap adds map values to a row.
// Ignores keys with unexistiging headers
// Fills blank where the key does not exists
func (s SimpleCsv) AddRowFromMap(rowValue map[string]string) (SimpleCsv, bool) {
headers := s.GetHeaders()
var added bool
var newRow []string
for _, v := range headers {
value, _ := rowValue[v]
newRow = append(newRow, value)
}
s, added = s.AddRow(newRow)
return s, added
}
// SetRow updates a row in the csv
func (s SimpleCsv) SetRow(rowNumber int, rowValue []string) (SimpleCsv, bool) {
if len(rowValue) != len(s[0]) || rowNumber >= len(s) || rowNumber < 0 {
return s, false
}
s[rowNumber] = rowValue
return s, true
}
// SetRowFromMap replaces a row by the maps value
// Ignores keys with unexistiging headers
// Fills blank where the key does not exists
func (s SimpleCsv) SetRowFromMap(rowNumber int, rowValue map[string]string) (SimpleCsv, bool) {
headers := s.GetHeaders()
var newRow []string
var modified bool
for _, v := range headers {
value, _ := rowValue[v]
newRow = append(newRow, value)
}
s, modified = s.SetRow(rowNumber, newRow)
return s, modified
}
// UpdateRowCellsFromMap replaces a row by the maps value
// Ignores keys with unexistiging headers
// Fills the old value where the key does not exist
func (s SimpleCsv) UpdateRowCellsFromMap(rowNumber int, rowValue map[string]string) (SimpleCsv, bool) {
headers := s.GetHeaders()
var newRow []string
var modified bool
for _, v := range headers {
value, _ := rowValue[v]
if value != "" {
newRow = append(newRow, value)
} else {
oldValue, _ := s.GetCellByField(v, rowNumber)
newRow = append(newRow, oldValue)
}
}
s, modified = s.SetRow(rowNumber, newRow)
return s, modified
}
// DeleteRow deletes the row
func (s SimpleCsv) DeleteRow(rowNumber int) (SimpleCsv, bool) {
if rowNumber >= 0 && rowNumber < len(s) {
s = append(s[:rowNumber], s[rowNumber+1:]...)
return s, true
}
return s, false
}