-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpreadSheetRow.go
219 lines (172 loc) · 4.3 KB
/
SpreadSheetRow.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package asciichgolangpublic
import (
"strings"
)
type SpreadSheetRow struct {
entries []string
}
func NewSpreadSheetRow() (s *SpreadSheetRow) {
return new(SpreadSheetRow)
}
func (s *SpreadSheetRow) GetColumnValueAsString(columnIndex int) (columnValue string, err error) {
if columnIndex < 0 {
return "", TracedErrorf("Invalid columnIndex: '%d'", columnIndex)
}
entries, err := s.GetEntries()
if err != nil {
return "", err
}
nEntries := len(entries)
if columnIndex >= nEntries {
return "", TracedErrorf(
"Invalid columnIndex '%d' for a spread sheet with '%d' columns",
columnIndex,
nEntries,
)
}
columnValue = entries[columnIndex]
return columnValue, nil
}
func (s *SpreadSheetRow) GetColumnWidths() (columnWidths []int, err error) {
nColumns, err := s.GetNumberOfEntries()
if err != nil {
return nil, err
}
columnWidths = []int{}
for i := 0; i < nColumns; i++ {
content, err := s.GetColumnValueAsString(i)
if err != nil {
return nil, err
}
columnWidths = append(columnWidths, len(content))
}
return columnWidths, nil
}
func (s *SpreadSheetRow) GetEntries() (entries []string, err error) {
if s.entries == nil {
return nil, TracedErrorf("entries not set")
}
if len(s.entries) <= 0 {
return nil, TracedErrorf("entries has no elements")
}
return s.entries, nil
}
func (s *SpreadSheetRow) GetNumberOfEntries() (nEntries int, err error) {
entries, err := s.GetEntries()
if err != nil {
return -1, err
}
return len(entries), nil
}
func (s *SpreadSheetRow) MustGetColumnValueAsString(columnIndex int) (columnValue string) {
columnValue, err := s.GetColumnValueAsString(columnIndex)
if err != nil {
LogGoErrorFatal(err)
}
return columnValue
}
func (s *SpreadSheetRow) MustGetColumnWidths() (columnWidths []int) {
columnWidths, err := s.GetColumnWidths()
if err != nil {
LogGoErrorFatal(err)
}
return columnWidths
}
func (s *SpreadSheetRow) MustGetEntries() (entries []string) {
entries, err := s.GetEntries()
if err != nil {
LogGoErrorFatal(err)
}
return entries
}
func (s *SpreadSheetRow) MustGetNumberOfEntries() (nEntries int) {
nEntries, err := s.GetNumberOfEntries()
if err != nil {
LogGoErrorFatal(err)
}
return nEntries
}
func (s *SpreadSheetRow) MustRemoveElementAtIndex(index int) {
err := s.RemoveElementAtIndex(index)
if err != nil {
LogGoErrorFatal(err)
}
}
func (s *SpreadSheetRow) MustRenderAsString(options *SpreadSheetRenderRowOptions) (rendered string) {
rendered, err := s.RenderAsString(options)
if err != nil {
LogGoErrorFatal(err)
}
return rendered
}
func (s *SpreadSheetRow) MustSetEntries(entries []string) {
err := s.SetEntries(entries)
if err != nil {
LogGoErrorFatal(err)
}
}
func (s *SpreadSheetRow) RemoveElementAtIndex(index int) (err error) {
if index < 0 {
return TracedErrorf("Index '%d' is invalid.", index)
}
entries, err := s.GetEntries()
if err != nil {
return err
}
entries = Slices().RemoveStringEntryAtIndex(entries, index)
err = s.SetEntries(entries)
if err != nil {
return err
}
return nil
}
func (s *SpreadSheetRow) RenderAsString(options *SpreadSheetRenderRowOptions) (rendered string, err error) {
if options == nil {
return "", TracedError("options is nil")
}
entries, err := s.GetEntries()
if err != nil {
return "", err
}
var delimiter string = ""
if options.IsStringDelimiterSet() {
delimiter, err = options.GetStringDelimiter()
if err != nil {
return "", err
}
delimiter = " " + delimiter + " "
} else {
delimiter = " "
}
if options.IsMinColumnWidthsSet() {
minColumnWidth, err := options.GetMinColumnWidths()
if err != nil {
return "", err
}
nEntries := len(entries)
nCloumnWidths := len(minColumnWidth)
if nEntries != nCloumnWidths {
return "", TracedErrorf("nEntries = '%d' != nCloumnWidths = '%d'", nEntries, nCloumnWidths)
}
entriesFilled := []string{}
for i, entry := range entries {
entriesFilled = append(
entriesFilled,
Strings().RightFillWithSpaces(entry, minColumnWidth[i]),
)
}
entries = entriesFilled
}
rendered = strings.Join(entries, delimiter)
return rendered, nil
}
func (s *SpreadSheetRow) SetEntries(entries []string) (err error) {
if entries == nil {
return TracedErrorf("entries is nil")
}
if len(entries) <= 0 {
return TracedErrorf("entries has no elements")
}
s.entries = entries
return nil
}