-
Notifications
You must be signed in to change notification settings - Fork 0
/
anyvaluesview.go
39 lines (34 loc) · 1.05 KB
/
anyvaluesview.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
package retable
var _ View = new(AnyValuesView)
// AnyValuesView is a View implementation
// that holds its rows as slices of value with any type.
type AnyValuesView struct {
Tit string
Cols []string
Rows [][]any
}
// NewAnyValuesViewFrom reads and caches all cells
// from the source View as ValuesView.
func NewAnyValuesViewFrom(source View) *AnyValuesView {
view := &AnyValuesView{
Tit: source.Title(),
Cols: source.Columns(),
Rows: make([][]any, source.NumRows()),
}
for row := 0; row < source.NumRows(); row++ {
view.Rows[row] = make([]any, len(source.Columns()))
for col := range view.Rows[row] {
view.Rows[row][col] = source.Cell(row, col)
}
}
return view
}
func (view *AnyValuesView) Title() string { return view.Tit }
func (view *AnyValuesView) Columns() []string { return view.Cols }
func (view *AnyValuesView) NumRows() int { return len(view.Rows) }
func (view *AnyValuesView) Cell(row, col int) any {
if row < 0 || col < 0 || row >= len(view.Rows) || col >= len(view.Rows[row]) {
return nil
}
return view.Rows[row][col]
}