Skip to content

Commit b44053b

Browse files
committed
rename View.AnyValue to Cell and move View.ReflectValue to ReflectCellView.ReflectCell
1 parent f3173bd commit b44053b

21 files changed

+102
-156
lines changed

anyvaluesview.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package retable
22

3-
import (
4-
"reflect"
5-
)
6-
73
var _ View = new(AnyValuesView)
84

95
// AnyValuesView is a View implementation
@@ -25,7 +21,7 @@ func NewAnyValuesViewFrom(source View) *AnyValuesView {
2521
for row := 0; row < source.NumRows(); row++ {
2622
view.Rows[row] = make([]any, len(source.Columns()))
2723
for col := range view.Rows[row] {
28-
view.Rows[row][col] = source.AnyValue(row, col)
24+
view.Rows[row][col] = source.Cell(row, col)
2925
}
3026
}
3127
return view
@@ -35,16 +31,9 @@ func (view *AnyValuesView) Title() string { return view.Tit }
3531
func (view *AnyValuesView) Columns() []string { return view.Cols }
3632
func (view *AnyValuesView) NumRows() int { return len(view.Rows) }
3733

38-
func (view *AnyValuesView) AnyValue(row, col int) any {
34+
func (view *AnyValuesView) Cell(row, col int) any {
3935
if row < 0 || col < 0 || row >= len(view.Rows) || col >= len(view.Rows[row]) {
4036
return nil
4137
}
4238
return view.Rows[row][col]
4339
}
44-
45-
func (view *AnyValuesView) ReflectValue(row, col int) reflect.Value {
46-
if row < 0 || col < 0 || row >= len(view.Rows) || col >= len(view.Rows[row]) {
47-
return reflect.Value{}
48-
}
49-
return reflect.ValueOf(view.Rows[row][col])
50-
}

cellformatter.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (f CellFormatterFunc) FormatCell(ctx context.Context, view View, row, col i
3030
type PrintfCellFormatter string
3131

3232
func (format PrintfCellFormatter) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error) {
33-
return fmt.Sprintf(string(format), view.AnyValue(row, col)), false, nil
33+
return fmt.Sprintf(string(format), view.Cell(row, col)), false, nil
3434
}
3535

3636
// PrintfRawCellFormatter implements CellFormatter by calling
@@ -39,15 +39,15 @@ func (format PrintfCellFormatter) FormatCell(ctx context.Context, view View, row
3939
type PrintfRawCellFormatter string
4040

4141
func (format PrintfRawCellFormatter) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error) {
42-
return fmt.Sprintf(string(format), view.AnyValue(row, col)), true, nil
42+
return fmt.Sprintf(string(format), view.Cell(row, col)), true, nil
4343
}
4444

4545
// SprintCellFormatter returns a CellFormatter
4646
// that formats a cell's value using fmt.Sprint
4747
// and returns the result together with the rawResult argument.
4848
func SprintCellFormatter(rawResult bool) CellFormatter {
4949
return CellFormatterFunc(func(ctx context.Context, view View, row, col int) (string, bool, error) {
50-
return fmt.Sprint(view.AnyValue(row, col)), rawResult, nil
50+
return fmt.Sprint(view.Cell(row, col)), rawResult, nil
5151
})
5252
}
5353

@@ -80,7 +80,7 @@ func TryFormattersOrSprint(formatters ...CellFormatter) CellFormatter {
8080

8181
// Fallback for no formatters passed or when
8282
// all formatters returned errors.ErrUnsupported
83-
v := view.ReflectValue(row, col)
83+
v := AsReflectCellView(view).ReflectCell(row, col)
8484
if IsNullLike(v) {
8585
return "", false, nil
8686
}
@@ -106,9 +106,9 @@ func (rawStr RawCellString) FormatCell(ctx context.Context, view View, row, col
106106
type LayoutFormatter string
107107

108108
func (f LayoutFormatter) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error) {
109-
formatter, ok := view.AnyValue(row, col).(interface{ Format(string) string })
109+
formatter, ok := view.Cell(row, col).(interface{ Format(string) string })
110110
if !ok {
111-
return "", false, fmt.Errorf("%T does not implement interface{ Format(string) string }", view.AnyValue(row, col))
111+
return "", false, fmt.Errorf("%T does not implement interface{ Format(string) string }", view.Cell(row, col))
112112
}
113113
return formatter.Format(string(f)), false, nil
114114
}
@@ -119,7 +119,7 @@ func (f LayoutFormatter) FormatCell(ctx context.Context, view View, row, col int
119119
type StringIfTrue string
120120

121121
func (f StringIfTrue) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error) {
122-
if view.ReflectValue(row, col).Bool() {
122+
if view.Cell(row, col).(bool) {
123123
return string(f), false, nil
124124
}
125125
return "", false, nil
@@ -131,7 +131,7 @@ func (f StringIfTrue) FormatCell(ctx context.Context, view View, row, col int) (
131131
type RawStringIfTrue string
132132

133133
func (f RawStringIfTrue) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error) {
134-
if view.ReflectValue(row, col).Bool() {
134+
if view.Cell(row, col).(bool) {
135135
return string(f), true, nil
136136
}
137137
return "", true, nil
@@ -203,7 +203,7 @@ func ReflectCellFormatterFunc(function any, rawResult bool) (formatter CellForma
203203
args[ctxIndex] = reflect.ValueOf(ctx)
204204
}
205205
if valIndex != -1 {
206-
args[valIndex] = view.ReflectValue(row, col)
206+
args[valIndex] = AsReflectCellView(view).ReflectCell(row, col)
207207
}
208208
res := fv.Call(args)
209209
if errIndex != -1 && !res[errIndex].IsNil() {

csvtable/writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func (w *Writer[T]) cellString(ctx context.Context, view retable.View, row, col
309309
// Continue after errors.ErrUnsupported
310310

311311
// Use fallback methods for formatting
312-
v := view.ReflectValue(row, col)
312+
v := retable.AsReflectCellView(view).ReflectCell(row, col)
313313
if retable.IsNullLike(v) {
314314
return w.escapeString(w.nilValue, false), nil
315315
}

derefview.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ import "reflect"
77
// by calling reflect.Value.Elem()
88
// wich might panic if the contained value
99
// does not support calling the Elem method.
10-
func DerefView(source View) View {
11-
return derefView{source: source}
10+
func DerefView(source View) ReflectCellView {
11+
return derefView{source: AsReflectCellView(source)}
1212
}
1313

1414
type derefView struct {
15-
source View
15+
source ReflectCellView
1616
}
1717

1818
func (v derefView) Title() string { return v.source.Title() }
1919
func (v derefView) Columns() []string { return v.source.Columns() }
2020
func (v derefView) NumRows() int { return v.source.NumRows() }
2121

22-
func (v derefView) AnyValue(row, col int) any {
23-
return v.source.ReflectValue(row, col).Elem().Interface()
22+
func (v derefView) Cell(row, col int) any {
23+
return v.source.ReflectCell(row, col).Elem().Interface()
2424
}
2525

26-
func (v derefView) ReflectValue(row, col int) reflect.Value {
27-
return v.source.ReflectValue(row, col).Elem()
26+
func (v derefView) ReflectCell(row, col int) reflect.Value {
27+
return v.source.ReflectCell(row, col).Elem()
2828
}

exceltable/read.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ func (view *sheetStringsView) Title() string { return view.sheet }
114114
func (view *sheetStringsView) Columns() []string { return view.columns }
115115
func (view *sheetStringsView) NumRows() int { return len(view.rows) }
116116

117-
func (view *sheetStringsView) AnyValue(row, col int) any {
117+
func (view *sheetStringsView) Cell(row, col int) any {
118118
if row < 0 || col < 0 || row >= len(view.rows) || col >= len(view.rows[row]) {
119119
return nil
120120
}
121121
return view.rows[row][col]
122122
}
123123

124-
func (view *sheetStringsView) ReflectValue(row, col int) reflect.Value {
124+
func (view *sheetStringsView) ReflectCell(row, col int) reflect.Value {
125125
if row < 0 || col < 0 || row >= len(view.rows) || col >= len(view.rows[row]) {
126126
return reflect.Value{}
127127
}
@@ -139,14 +139,14 @@ func (view *sheetStringsView) ReflectValue(row, col int) reflect.Value {
139139
// func (view *sheetView) Columns() []string { return view.columns }
140140
// func (view *sheetView) NumRows() int { return view.numRows }
141141

142-
// func (view *sheetView) AnyValue(row, col int) any {
142+
// func (view *sheetView) Cell(row, col int) any {
143143
// if row < 0 || col < 0 || row >= view.numRows || col >= len(view.columns) {
144144
// return nil
145145
// }
146146
// panic("TODO")
147147
// }
148148

149-
// func (view *sheetView) ReflectValue(row, col int) reflect.Value {
149+
// func (view *sheetView) ReflectCell(row, col int) reflect.Value {
150150
// if row < 0 || col < 0 || row >= view.numRows || col >= len(view.columns) {
151151
// return reflect.Value{}
152152
// }

extracolsfuncview.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"reflect"
55
)
66

7-
func ExtraColsAnyValueFuncView(left View, columns []string, anyValue func(row, col int) any) View {
7+
func ExtraColsAnyValueFuncView(left View, columns []string, anyValue func(row, col int) any) ReflectCellView {
88
return &extraColsFuncView{
9-
left: left,
9+
left: AsReflectCellView(left),
1010
columns: columns,
1111
anyValue: anyValue,
1212
reflectValue: func(row, col int) reflect.Value {
@@ -15,9 +15,9 @@ func ExtraColsAnyValueFuncView(left View, columns []string, anyValue func(row, c
1515
}
1616
}
1717

18-
func ExtraColsReflectValueFuncView(left View, columns []string, reflectValue func(row, col int) reflect.Value) View {
18+
func ExtraColsReflectValueFuncView(left View, columns []string, reflectValue func(row, col int) reflect.Value) ReflectCellView {
1919
return &extraColsFuncView{
20-
left: left,
20+
left: AsReflectCellView(left),
2121
columns: columns,
2222
anyValue: func(row, col int) any {
2323
v := reflectValue(row, col)
@@ -31,7 +31,7 @@ func ExtraColsReflectValueFuncView(left View, columns []string, reflectValue fun
3131
}
3232

3333
type extraColsFuncView struct {
34-
left View
34+
left ReflectCellView
3535
columns []string
3636
anyValue func(row, col int) any
3737
reflectValue func(row, col int) reflect.Value
@@ -49,18 +49,18 @@ func (e *extraColsFuncView) NumRows() int {
4949
return e.left.NumRows()
5050
}
5151

52-
func (e *extraColsFuncView) AnyValue(row, col int) any {
52+
func (e *extraColsFuncView) Cell(row, col int) any {
5353
numLeftCols := len(e.left.Columns())
5454
if col < numLeftCols {
55-
return e.left.AnyValue(row, col)
55+
return e.left.Cell(row, col)
5656
}
5757
return e.anyValue(row, col-numLeftCols)
5858
}
5959

60-
func (e *extraColsFuncView) ReflectValue(row, col int) reflect.Value {
60+
func (e *extraColsFuncView) ReflectCell(row, col int) reflect.Value {
6161
numLeftCols := len(e.left.Columns())
6262
if col < numLeftCols {
63-
return e.left.ReflectValue(row, col)
63+
return e.left.ReflectCell(row, col)
6464
}
6565
return e.reflectValue(row, col-numLeftCols)
6666
}

extracolsview.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package retable
22

3-
import (
4-
"reflect"
5-
)
6-
73
var _ View = ExtraColsView(nil)
84

95
type ExtraColsView []View
@@ -31,7 +27,7 @@ func (e ExtraColsView) NumRows() int {
3127
return maxNumRows
3228
}
3329

34-
func (e ExtraColsView) AnyValue(row, col int) any {
30+
func (e ExtraColsView) Cell(row, col int) any {
3531
if row < 0 || col < 0 {
3632
return nil
3733
}
@@ -40,25 +36,9 @@ func (e ExtraColsView) AnyValue(row, col int) any {
4036
numCols := len(view.Columns())
4137
colRight := colLeft + numCols
4238
if col < colRight {
43-
return view.AnyValue(row, col-colLeft)
39+
return view.Cell(row, col-colLeft)
4440
}
4541
colLeft = colRight
4642
}
4743
return nil
4844
}
49-
50-
func (e ExtraColsView) ReflectValue(row, col int) reflect.Value {
51-
if row < 0 || col < 0 {
52-
return reflect.Value{}
53-
}
54-
colLeft := 0
55-
for _, view := range e {
56-
numCols := len(view.Columns())
57-
colRight := colLeft + numCols
58-
if col < colRight {
59-
return view.ReflectValue(row, col-colLeft)
60-
}
61-
colLeft = colRight
62-
}
63-
return reflect.Value{}
64-
}

extrarowsview.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package retable
22

3-
import (
4-
"reflect"
5-
)
6-
73
var _ View = ExtraRowView(nil)
84

95
type ExtraRowView []View
@@ -30,7 +26,7 @@ func (e ExtraRowView) NumRows() int {
3026
return numRows
3127
}
3228

33-
func (e ExtraRowView) AnyValue(row, col int) any {
29+
func (e ExtraRowView) Cell(row, col int) any {
3430
if row < 0 || col < 0 || col >= len(e.Columns()) {
3531
return nil
3632
}
@@ -39,25 +35,9 @@ func (e ExtraRowView) AnyValue(row, col int) any {
3935
numRows := view.NumRows()
4036
rowBottom := rowTop + numRows
4137
if row < rowBottom {
42-
return view.AnyValue(row-rowTop, col)
38+
return view.Cell(row-rowTop, col)
4339
}
4440
rowTop = rowBottom
4541
}
4642
return nil
4743
}
48-
49-
func (e ExtraRowView) ReflectValue(row, col int) reflect.Value {
50-
if row < 0 || col < 0 || col >= len(e.Columns()) {
51-
return reflect.Value{}
52-
}
53-
rowTop := 0
54-
for _, view := range e {
55-
numRows := view.NumRows()
56-
rowBottom := rowTop + numRows
57-
if row < rowBottom {
58-
return view.ReflectValue(row-rowTop, col)
59-
}
60-
rowTop = rowBottom
61-
}
62-
return reflect.Value{}
63-
}

filteredview.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package retable
22

3-
import "reflect"
4-
53
var _ View = new(FilteredView)
64

75
type FilteredView struct {
@@ -51,7 +49,7 @@ func (view *FilteredView) NumRows() int {
5149
return n
5250
}
5351

54-
func (view *FilteredView) AnyValue(row, col int) any {
52+
func (view *FilteredView) Cell(row, col int) any {
5553
numRows := view.NumRows()
5654
numCols := view.NumCols()
5755
if row < 0 || col < 0 || row >= numRows || col >= numCols {
@@ -61,18 +59,5 @@ func (view *FilteredView) AnyValue(row, col int) any {
6159
if view.ColumnMapping != nil {
6260
col = view.ColumnMapping[col]
6361
}
64-
return view.Source.AnyValue(row, col)
65-
}
66-
67-
func (view *FilteredView) ReflectValue(row, col int) reflect.Value {
68-
numRows := view.NumRows()
69-
numCols := view.NumCols()
70-
if row < 0 || col < 0 || row >= numRows || col >= numCols {
71-
return reflect.Value{}
72-
}
73-
row += max(view.RowOffset, 0)
74-
if view.ColumnMapping != nil {
75-
col = view.ColumnMapping[col]
76-
}
77-
return view.Source.ReflectValue(row, col)
62+
return view.Source.Cell(row, col)
7863
}

formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (UnsupportedFormatter) Format(v reflect.Value) (string, error) {
3333

3434
func CellFormatterFromFormatter(f Formatter, rawResult bool) CellFormatter {
3535
return CellFormatterFunc(func(ctx context.Context, view View, row, col int) (str string, raw bool, err error) {
36-
str, err = f.Format(view.ReflectValue(row, col))
36+
str, err = f.Format(AsReflectCellView(view).ReflectCell(row, col))
3737
return str, rawResult, err
3838
})
3939
}

0 commit comments

Comments
 (0)