-
Notifications
You must be signed in to change notification settings - Fork 0
/
gotables.go
4588 lines (3827 loc) · 123 KB
/
gotables.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017 Malcolm Gorman
// Golang tabular data format for configs and channels, with a rich set of helper functions.
// To import gotables: import "github.com/urban-wombat/gotables"
package gotables
import (
"bytes"
"encoding/csv"
"errors"
"fmt"
"io/ioutil"
"log"
"math"
"math/rand"
"os"
"reflect"
"runtime/debug"
"sort"
"strconv"
"strings"
"time"
"unicode"
)
// "golang.org/x/text/width"
// Would like to be able to call width.Narrow() on wide glyphs.
// Seems unavailable: http://docs.activestate.com/activego/1.8/pkg/golang.org/x/text/width
// https://godoc.org/golang.org/x/text/width#example-Transformer--Narrow
/*
Copyright (c) 2017 Malcolm Gorman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
const debugging bool = false
const printCaller = false
var MinTime time.Time = time.Time{} // 0001-01-01T00:00:00Z
// This MaxTime value is correct as far as I know (the alternative posited time.Unix(1<<63-1, 0) is time.Before()).
var MaxTime time.Time = time.Unix(1<<63-62135596801, 999999999) // 292277024627-12-07T02:30:07.999999999+11:00
func init() {
/*
if debugging {
log.SetFlags(log.Lshortfile)
log.SetOutput(os.Stderr)
} else {
log.SetOutput(ioutil.Discard)
}
*/
log.SetFlags(log.Lshortfile)
// log.SetOutput(os.Stderr)
}
var where = log.Print
/*
#####################################################################################
TableSet
#####################################################################################
2016.12.16 Malcolm Gorman Use bytes.Buffer to construct string() string strings.
#####################################################################################
*/
// ########
// TableSet
// ########
/*
TableSet is an ordered set of *Table pointers.
*/
type TableSet struct {
tableSetName string
fileName string
tables []*Table
}
// For GOB. Selected header information for exporting.
type TableSetExported struct {
TableSetName string
FileName string
}
// Factory function to return an initialised *TableSet pointer.
func NewTableSet(tableSetName string) (*TableSet, error) {
var newTables *TableSet = new(TableSet)
newTables.tableSetName = tableSetName
newTables.tables = []*Table{} // An empty slice of tables.
return newTables, nil
}
// Read and parse a gotables file into a TableSet.
func NewTableSetFromFile(fileName string) (*TableSet, error) {
var p parser
p.SetFileName(fileName) // Needed for printing file and line diagnostics.
tables, err := p.parseFile(fileName)
if err != nil {
return nil, err
}
return tables, nil
}
// Write a TableSet to a text file.
func (tableSet *TableSet) WriteFile(fileName string, mode os.FileMode) error {
if tableSet == nil {
return fmt.Errorf("%s tableSet.%s tableSet is <nil>", UtilFuncSource(), UtilFuncName())
}
var err error
var tableSet_String string
var tableSet_Bytes []byte
tableSet_String = tableSet.String()
tableSet_Bytes = []byte(tableSet_String)
if mode == 0 {
// No permissions set.
mode = 0666
}
err = ioutil.WriteFile(fileName, tableSet_Bytes, mode)
return err
}
// Write a Table to a text file.
func (table *Table) WriteFile(fileName string, mode os.FileMode) error {
if table == nil {
return fmt.Errorf("%s table.%s(%q, mode) table is <nil>", UtilFuncSource(), UtilFuncName(), fileName)
}
var err error
var table_String string
var table_Bytes []byte
table_String = table.String()
table_Bytes = []byte(table_String)
if mode == 0 { // No permissions set.
mode = 0666
}
err = ioutil.WriteFile(fileName, table_Bytes, mode)
return err
}
// Read and parse a gotables string into a TableSet.
func NewTableSetFromString(s string) (*TableSet, error) {
var p parser
tables, err := p.parseString(s)
if err != nil {
return nil, err
}
return tables, nil
}
/*
This function expects exactly ONE table in the string. Otherwise it's an error.
If there's more than one table in the string, use NewTableFromStringByTableName() instead.
*/
func NewTableFromString(s string) (*Table, error) {
tableSet, err := NewTableSetFromString(s)
if err != nil {
return nil, fmt.Errorf("NewTableFromString() %v", err)
}
tableCount := tableSet.TableCount()
if tableCount != 1 {
return nil, fmt.Errorf("NewTableFromString() %s expecting string to contain 1 table but found %d table%s",
UtilFuncName(), tableCount, plural(tableCount))
}
table, err := tableSet.GetTableByTableIndex(0)
if err != nil {
return nil, fmt.Errorf("NewTableFromString() %v", err)
}
return table, nil
}
/*
This function expects exactly ONE table in the string. Otherwise it's an error.
If there's more than one table in the string, use NewTableFromStringByTableName() instead.
On error, this function panics. It's for single-value contexts where you KNOW it will work.
*/
func NewTableFromStringMustMake(s string) *Table {
table, err := NewTableFromString(s)
if err != nil {
panic(err)
}
return table
}
func NewTableFromStringByTableName(s string, tableName string) (*Table, error) {
tableSet, err := NewTableSetFromString(s)
if err != nil {
return nil, fmt.Errorf("NewTableFromStringByTableName() %v", err)
}
table, err := tableSet.GetTable(tableName)
if err != nil {
return nil, fmt.Errorf("NewTableFromStringByTableName() %v", err)
}
return table, nil
}
func NewTableFromStringByTableIndex(s string, tableIndex int) (*Table, error) {
tableSet, err := NewTableSetFromString(s)
if err != nil {
return nil, fmt.Errorf("NewTableFromStringByTableName() %v", err)
}
table, err := tableSet.GetTableByTableIndex(tableIndex)
if err != nil {
return nil, fmt.Errorf("NewTableFromStringByTableIndex() %v", err)
}
return table, nil
}
/*
This function expects exactly ONE table in the file. Otherwise it's an error.
If there's more than one table in the file, use NewTableFromFileByTableName() instead.
*/
func NewTableFromFile(fileName string) (*Table, error) {
tableSet, err := NewTableSetFromFile(fileName)
if err != nil {
return nil, err
}
tableCount := tableSet.TableCount()
if tableCount != 1 {
return nil, fmt.Errorf("%s expecting file to contain 1 table but found %d table%s: %s",
UtilFuncName(), tableCount, plural(tableCount), fileName)
}
table, err := tableSet.GetTableByTableIndex(0)
if err != nil {
return nil, err
}
return table, nil
}
func NewTableFromFileByTableName(fileName string, tableName string) (*Table, error) {
tableSet, err := NewTableSetFromFile(fileName)
if err != nil {
return nil, err
}
table, err := tableSet.GetTable(tableName)
if err != nil {
return nil, err
}
return table, nil
}
/*
Returns a set of parsable tables with format right-aligned (numbers) as a string.
Note: This method does not handle nested tables (type *Table).
To serialise nested tables, use yaml:
gotables.GetTableSetAsYAML()
gotables.NewTableSetFromYAML()
*/
func (tableSet *TableSet) String() string {
if tableSet == nil {
_, _ = os.Stderr.WriteString(fmt.Sprintf("%s tableSet.%s tableSet is <nil>\n", UtilFuncSource(), UtilFuncName()))
UtilPrintCaller()
return ""
}
return tableSet.StringPadded()
}
func (tableSet *TableSet) StringPadded() string {
if tableSet == nil {
_, _ = os.Stderr.WriteString(fmt.Sprintf("%s tableSet.%s tableSet is <nil>\n", UtilFuncSource(), UtilFuncName()))
UtilPrintCaller()
return ""
}
var verticalSep string = ""
var s string
var tableSetName string = tableSet.Name()
if tableSetName != "" {
s += fmt.Sprintf("[[%s]]\n\n", tableSet.Name())
}
var table *Table
for i := 0; i < len(tableSet.tables); i++ {
table = tableSet.tables[i]
s += verticalSep
s += table.StringPadded()
verticalSep = "\n"
}
return s
}
func (tableSet *TableSet) StringUnpadded() string {
if tableSet == nil {
_, _ = os.Stderr.WriteString(fmt.Sprintf("%s tableSet.%s tableSet is <nil>\n", UtilFuncSource(), UtilFuncName()))
UtilPrintCaller()
return ""
}
var horizontalSeparator byte = ' '
return tableSet._String(horizontalSeparator)
}
// Return parsable set of tables as a string.
func (tableSet *TableSet) _String(horizontalSeparator byte) string {
if tableSet == nil {
_, _ = os.Stderr.WriteString(fmt.Sprintf("%s tableSet.%s tableSet is <nil>\n", UtilFuncSource(), UtilFuncName()))
UtilPrintCaller()
return ""
}
var buf bytes.Buffer
// buf.WriteString("# From file: \"" + tableSet.name + "\"\n\n")
var tableSep = ""
var table *Table
for i := 0; i < len(tableSet.tables); i++ {
table = tableSet.tables[i]
buf.WriteString(tableSep)
// buf.WriteString(fmt.Sprintf("%v", table))
buf.WriteString(fmt.Sprintf("%v", table._String(horizontalSeparator)))
tableSep = "\n"
}
var s string = buf.String()
return s
}
func (tableSet *TableSet) Name() string {
if tableSet == nil {
_, _ = os.Stderr.WriteString(fmt.Sprintf("%s tableSet.%s tableSet is <nil>\n", UtilFuncSource(), UtilFuncName()))
UtilPrintCaller()
return ""
}
return tableSet.tableSetName
}
func (tableSet *TableSet) SetName(tableSetName string) error {
// if tableSet == nil {
// _, _ = os.Stderr.WriteString(fmt.Sprintf("%s tableSet.%s tableSet is <nil>\n", UtilFuncSource(), UtilFuncName()))
// UtilPrintCaller()
// return
// }
if tableSet == nil {
return fmt.Errorf("%s tableSet.%s tableSet is <nil>", UtilFuncSource(), UtilFuncName())
}
tableSet.tableSetName = tableSetName
return nil
}
// The file name if this TableSet has been created from a file. Otherwise ""
func (tableSet *TableSet) FileName() string {
if tableSet == nil {
_, _ = os.Stderr.WriteString(fmt.Sprintf("%s tableSet.%s tableSet is <nil>\n", UtilFuncSource(), UtilFuncName()))
UtilPrintCaller()
return ""
}
return tableSet.fileName
}
func (tableSet *TableSet) SetFileName(fileName string) {
if tableSet == nil {
_, _ = os.Stderr.WriteString(fmt.Sprintf("%s tableSet.%s tableSet is <nil>\n", UtilFuncSource(), UtilFuncName()))
UtilPrintCaller()
return
}
tableSet.fileName = fileName
}
func (tableSet *TableSet) TableCount() int {
if tableSet == nil {
_, _ = os.Stderr.WriteString(fmt.Sprintf("%s tableSet.%s tableSet is <nil>\n", UtilFuncSource(), UtilFuncName()))
UtilPrintCaller()
return -1
}
return len(tableSet.tables)
}
func (tableSet *TableSet) Append(newTable *Table) error {
return tableSet.AppendTable(newTable)
}
// Add a table to a table set.
func (tableSet *TableSet) AppendTable(newTable *Table) error {
if tableSet == nil {
return fmt.Errorf("%s tableSet.%s tableSet is <nil>", UtilFuncSource(), UtilFuncName())
}
// Note: Could maintain a map in parallel for rapid lookup of table names.
for _, existingTable := range tableSet.tables {
if existingTable.Name() == newTable.Name() {
return fmt.Errorf("table [%s] already exists: [%s]", newTable.tableName, newTable.tableName)
}
}
tableSet.tables = append(tableSet.tables, newTable)
return nil
}
// Checks whether table exists
func (tableSet *TableSet) HasTable(tableName string) (bool, error) {
if tableSet == nil {
return false, fmt.Errorf("%s tableSet.%s tableSet is <nil>", UtilFuncSource(), UtilFuncName())
}
for _, table := range tableSet.tables {
if table.Name() == tableName {
return true, nil
}
}
return false, fmt.Errorf("table [%s] does not exist", tableName)
}
func (tableSet *TableSet) GetTable(tableName string) (*Table, error) {
if tableSet == nil {
return nil, fmt.Errorf("%s tableSet.%s tableSet is <nil>", UtilFuncSource(), UtilFuncName())
}
for _, table := range tableSet.tables {
if table.Name() == tableName {
return table, nil
}
}
return nil, fmt.Errorf("table [%s] does not exist", tableName)
}
func (tableSet *TableSet) GetTableByTableIndex(tableIndex int) (*Table, error) {
if tableSet == nil {
return nil, fmt.Errorf("%s tableSet.%s tableSet is <nil>", UtilFuncSource(), UtilFuncName())
}
if tableIndex < 0 || tableIndex > tableSet.TableCount()-1 {
// err := fmt.Errorf("in *TableSet with %d tables, table index %d does not exist",
// tableSet.TableCount(), tableIndex)
// return nil, err
return nil, fmt.Errorf("%s: in tableSet [[%s]] tableIndex [%d] out of range with TableCount() length %d",
UtilFuncName(), tableSet.tableSetName, tableIndex, tableSet.TableCount())
}
return tableSet.tables[tableIndex], nil
}
/*
#####################################################################################
Table
#####################################################################################
2016.02.03 Malcolm Gorman Add sort keys to Table.
#####################################################################################
*/
type Table struct {
tableName string
colNames []string
colTypes []string
colNamesMap map[string]int // To look up a colNames index from a col name.
rows []tableRow
sortKeys []sortKey
isStructShape bool
isNilTable bool
parentTable *Table
depth int
}
// For GOB.
type TableExported struct {
TableName string
ColNames []string
ColTypes []string
ColNamesMap map[string]int // To look up a colNames index from a col name.
Rows []tableRow
SortKeys []SortKeyExported
StructShape bool
IsNilTable bool
ParentTable *TableExported
}
func (table *Table) getColTypes() []string {
if table == nil {
_, _ = os.Stderr.WriteString(fmt.Sprintf("%s table.%s table is <nil>\n", UtilFuncSource(), UtilFuncName()))
UtilPrintCaller()
return nil
}
return table.colTypes
}
type tableRow []interface{}
type Row struct {
Table *Table
RowIndex int
}
type CellInfo struct {
Table *Table // A reference to (not a copy of) the enclosing table (not the parent table, not a cell table).
ColName string
ColType string
ColIndex int
RowIndex int
}
// Note: Reimplement this as a slice of byte for each row and a master map and/or slice to track offset.
/*
Factory function to create an empty *Table that is not yet ready to use
The table is for use as zero value, a *Table that is a placeholder without being a Go <nil>
To make it usable, give it a table name: table.SetTableName("MyTableName")
In its unusable NilTable state, table.IsValidTable() will return false
var myTable *gotables.Table = gotables.NewNilTable()
Note: does not return an error. It cannot fail.
*/
func NewNilTable() *Table {
var newTable *Table = new(Table)
newTable.colNames = []string{}
newTable.colTypes = []string{}
newTable.colNamesMap = map[string]int{}
newTable.rows = []tableRow{}
newTable.isNilTable = true
return newTable
}
/*
Factory function to create an empty *Table
var myTable *gotables.Table
myTable, err = gotables.NewTable("My_Table")
if err != nil {
panic(err)
}
*/
func NewTable(tableName string) (*Table, error) {
//UtilPrintCaller()
//where(UtilFuncName())
var newTable *Table = NewNilTable()
err := newTable.SetName(tableName)
if err != nil {
return nil, err
}
return newTable, nil
}
// For testing. Does not return error. Panics on error.
func newNonZeroTable(tableName string) *Table {
var newTable *Table = NewNilTable()
err := newTable.SetName(tableName)
if err != nil {
panic(err)
}
return newTable
}
// GOB
func newTableExported(tableName string) (*TableExported, error) {
var err error
var NewTableExported *TableExported = new(TableExported)
err = NewTableExported.setTableNameExported(tableName)
if err != nil {
return nil, err
}
NewTableExported.ColNames = []string{}
NewTableExported.ColTypes = []string{}
NewTableExported.ColNamesMap = map[string]int{}
NewTableExported.Rows = []tableRow{}
return NewTableExported, nil
}
/*
table, err := gotables.NewTableFromMetadata("Moviegoers", []string{"Age", "Mothballs"}, []string{"int", "bool"})
*/
func NewTableFromMetadata(tableName string, colNames []string, colTypes []string) (*Table, error) {
var newTable *Table
var err error
// Check for invalid input.
if len(colNames) != len(colTypes) {
return nil, fmt.Errorf("%s(colNames, colTypes) len(colNames)=%d != len(colTypes)=%d",
UtilFuncName(), len(colNames), len(colTypes))
}
newTable, err = NewTable(tableName)
if err != nil {
return nil, err
}
err = newTable.appendCols(colNames, colTypes)
if err != nil {
return nil, err
}
if debugging {
_, err = newTable.IsValidTable()
if err != nil {
return nil, err
}
}
return newTable, nil
}
/*
Add (append) a new blank row to this table. This does NOT initialise the cell values. They will be set to nil.
Note: This is used by the parser. Not for use by end-users.
*/
func (table *Table) appendRowOfNil() error {
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
return nil
}
/*
Note: Can append rows to an empty (no columns) table, and later append columns.
howMany may be 0 or more rows.
Gotcha: colType *Table cells will be populated with nil [] tables: must be named being used.
*/
func (table *Table) AppendRows(howMany int) error {
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
if table.isNilTable {
return fmt.Errorf("table.%s: table is an unnamed NilTable. Call table.SetName() to un-Nil it", UtilFuncName())
}
var err error
if debugging {
_, err = table.IsValidTable()
if err != nil {
return err
}
}
if howMany < 0 {
return fmt.Errorf("table [%s] AppendRows(%d) cannot append %d rows (must be 0 or more)", table.Name(), howMany, howMany)
}
for i := 0; i < howMany; i++ {
err = table.AppendRow()
if err != nil {
return err
}
}
if debugging {
_, err = table.IsValidTable()
if err != nil {
return err
}
}
return nil
}
/*
All cells in the new added row will be set to their zero value, such as 0, "", or false.
Note: Can append rows to an empty (no columns) table, and later append columns.
Gotcha: colType *Table cells will be populated with nil [] tables: must be named being used:
err = table.AppendRow()
lastRowIndex = table.RowCount()-1
err = table.SetName("AnyNameYouLike")
*/
func (table *Table) AppendRow() error {
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
if table.isNilTable {
return fmt.Errorf("table.%s: table is an unnamed NilTable. Call table.SetName() to un-Nil it", UtilFuncName())
}
var err error
if debugging {
_, err = table.IsValidTable()
if err != nil {
return err
}
}
// if printCaller { UtilPrintCaller() }
/*
// This is an interesting consideration. It sounds right, but it might make things less flexible unnecessarily.
if table.ColCount() == 0 {
return fmt.Errorf("in table [%s]: cannot add rows to a table that has no columns", table.Name())
}
*/
// Note: function make() sets slice values to <nil> and NOT to their zero value.
var newRow tableRow = make(tableRow, len(table.colNames))
table.rows = append(table.rows, newRow)
var rowIndex int
rowIndex, _ = table.lastRowIndex()
err = table.SetRowCellsToZeroValue(rowIndex)
if err != nil {
return err
}
if debugging {
_, err = table.IsValidTable()
if err != nil {
return err
}
}
return nil
}
// Set all float cells in this row to NaN. This is a convenience function to use NaN as a proxy for a missing value.
func (table *Table) SetRowFloatCellsToNaN(rowIndex int) error {
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
var err error
var colType string
for colIndex := 0; colIndex < table.ColCount(); colIndex++ {
colType, err = table.ColTypeByColIndex(colIndex)
if err != nil {
return err
}
switch colType {
case "float32":
err = table.SetFloat32ByColIndex(colIndex, rowIndex, float32(math.NaN()))
case "float64":
err = table.SetFloat64ByColIndex(colIndex, rowIndex, math.NaN())
}
if err != nil {
return err
}
}
return nil
}
// Set all float cells in this table to NaN
func (table *Table) SetAllFloatCellsToNaN() error {
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
for colIndex := 0; colIndex < table.ColCount(); colIndex++ {
colType, err := table.ColTypeByColIndex(colIndex)
if err != nil {
return err
}
switch colType {
case "float32", "float64":
err = table.SetColFloatCellsToNaNByColIndex(colIndex)
if err != nil {
return err
}
}
}
return nil
}
// Set all cells in this col to their zero value, such as 0, "", or false.
func (table *Table) SetColCellsToZeroValue(colName string) error {
if printCaller {
UtilPrintCaller()
}
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
colIndex, err := table.ColIndex(colName)
if err != nil {
return err
}
return table.SetColCellsToZeroValueByColIndex(colIndex)
}
// Set all cells in this col to their zero value, such as 0, "", or false.
func (table *Table) SetColCellsToZeroValueByColIndex(colIndex int) error {
if printCaller {
UtilPrintCaller()
}
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
for rowIndex := 0; rowIndex < table.RowCount(); rowIndex++ {
err := table.SetCellToZeroValueByColIndex(colIndex, rowIndex)
if err != nil {
return err
}
}
return nil
}
// Set all float cells in this col to NaN (Not a Number)
func (table *Table) SetColFloatCellsToNaN(colName string) error {
if printCaller {
UtilPrintCaller()
}
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
colIndex, err := table.ColIndex(colName)
if err != nil {
return err
}
return table.SetColFloatCellsToNaNByColIndex(colIndex)
}
// Set all float cells in this col to NaN (Not a Number)
func (table *Table) SetColFloatCellsToNaNByColIndex(colIndex int) error {
if printCaller {
UtilPrintCaller()
}
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
for rowIndex := 0; rowIndex < table.RowCount(); rowIndex++ {
err := table.SetFloatCellToNaNByColIndex(colIndex, rowIndex)
if err != nil {
return err
}
}
return nil
}
func (table *Table) SetFloatCellToNaN(colName string, rowIndex int) (err error) {
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
var colIndex int
colIndex, err = table.ColIndex(colName)
if err != nil {
return
}
return table.SetFloatCellToNaNByColIndex(colIndex, rowIndex)
}
func (table *Table) SetFloatCellToNaNByColIndex(colIndex int, rowIndex int) (err error) {
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
var colType string
colType, err = table.ColTypeByColIndex(colIndex)
if err != nil {
return
}
switch colType {
case "float32":
err = table.SetFloat32ByColIndex(colIndex, rowIndex, float32(math.NaN()))
case "float64":
err = table.SetFloat64ByColIndex(colIndex, rowIndex, math.NaN())
default:
// Return a more generous error message so callers of calling methods can see the colName
colName, err := table.ColName(colIndex)
if err != nil {
return err
}
return fmt.Errorf("%s: [%s] colIndex=%d colName=%s coltype=%s expecting colType float32 or float64 but found: %s",
UtilFuncName(), table.Name(), colIndex, colName, colType, colType)
}
if err != nil {
return
}
return
}
func (table *Table) SetCellToZeroValue(colName string, rowIndex int) error {
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
var err error
var colIndex int
colIndex, err = table.ColIndex(colName)
if err != nil {
return err
}
err = table.SetCellToZeroValueByColIndex(colIndex, rowIndex)
if err != nil {
return err
}
return nil
}
func (table *Table) appendRowSlice(rowSlice tableRow) error {
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
// We're going to assume that all error checking was done in getRowSlice()
// Append row to existing rows.
if debugging {
// where(fmt.Sprintf("BEFORE: table.rows = %v\n", table.rows))
// where(fmt.Sprintf("DURING: rowSlice = %v\n", rowSlice))
// where(fmt.Sprintf("append(%v, %v)\n", table.rows, rowSlice))
}
table.rows = append(table.rows, rowSlice)
if debugging {
// where(fmt.Sprintf("AFTER: table.rows = %v\n", table.rows))
// where(fmt.Sprintf("\n"))
}
return nil
}
func (table *Table) DeleteRow(rowIndex int) error {
if table == nil {
return fmt.Errorf("%s table.%s table is <nil>", UtilFuncSource(), UtilFuncName())
}
var err error
if debugging {
_, err := table.IsValidTable()
if err != nil {
return err
}
}