/
type_table.go
66 lines (59 loc) · 1.17 KB
/
type_table.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 args
import (
"fmt"
"github.com/spyzhov/safe"
)
type Table [][]interface{}
func (a Table) Validate() (err error) {
if a == nil {
return nil
}
var width int
for row, values := range a {
if row == 0 {
width = len(values)
} else if width != len(values) {
return fmt.Errorf("row %d has wrong count of elements", row)
}
}
return nil
}
func (a Table) Match(table [][]interface{}, null string) (err error) {
if a == nil || len(a) == 0 {
return nil
}
if len(table) != len(a) {
return fmt.Errorf("wrong length")
}
if len(table[0]) != len(a[0]) {
return fmt.Errorf("wrong width")
}
if err = Table(table).Validate(); err != nil {
return safe.Wrap(err, "examine")
}
for row, values := range a {
for col, value := range values {
if !same(value, table[row][col], null) {
return fmt.Errorf("wrong value at (%d, %d)", row, col)
}
}
}
return nil
}
func (a Table) Row(index int) Slice {
if len(a) > index {
return a[index]
}
return nil
}
func (a Table) Column(index int) Slice {
column := make(Slice, 0)
if len(a) > 0 {
if len(a[0]) > index {
for _, row := range a {
column = append(column, row[index])
}
}
}
return column
}