-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglecolview.go
65 lines (56 loc) · 1.46 KB
/
singlecolview.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
package retable
import "reflect"
func SingleColView[T any](column string, rows []T) View {
return &singleColsView[T]{
columns: []string{column},
rows: rows,
isReflectValue: reflect.TypeOf(rows).Elem() == reflect.TypeOf(reflect.Value{}),
}
}
func SingleCellView[T any](title, column string, value T) View {
return &singleColsView[T]{
columns: []string{column},
rows: []T{value},
isReflectValue: reflect.TypeOf(value) == reflect.TypeOf(reflect.Value{}),
}
}
type singleColsView[T any] struct {
columns []string
rows []T
isReflectValue bool
}
func (s *singleColsView[T]) Title() string {
return s.columns[0]
}
func (s *singleColsView[T]) Columns() []string {
return s.columns
}
func (s *singleColsView[T]) NumRows() int {
return len(s.rows)
}
func (s *singleColsView[T]) Cell(row, col int) any {
if row < 0 || row >= len(s.rows) || col != 0 {
return nil
}
if !s.isReflectValue {
return s.rows[row]
}
// Lack of generic type specialization requires
// dynamic type assertion
v := any(s.rows[row]).(reflect.Value)
if !v.IsValid() {
return nil
}
return v.Interface()
}
func (s *singleColsView[T]) ReflectCell(row, col int) reflect.Value {
if row < 0 || row >= len(s.rows) || col != 0 {
return reflect.Value{}
}
if !s.isReflectValue {
return reflect.ValueOf(s.rows[row])
}
// Lack of generic type specialization requires
// dynamic type assertion
return any(s.rows[row]).(reflect.Value)
}