-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.go
46 lines (36 loc) · 1.22 KB
/
formatter.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
package retable
import (
"context"
"errors"
"fmt"
"reflect"
)
type Formatter interface {
Format(reflect.Value) (string, error)
}
type FormatterFunc func(reflect.Value) (string, error)
func (f FormatterFunc) Format(v reflect.Value) (string, error) {
return f(v)
}
// SprintFormatter is a Formatter that uses fmt.Sprint to format any value.
type SprintFormatter struct{}
func (SprintFormatter) Format(v reflect.Value) (string, error) {
return fmt.Sprint(v.Interface()), nil
}
// UnsupportedFormatter is a Formatter that always returns errors.ErrUnsupported.
type UnsupportedFormatter struct{}
func (UnsupportedFormatter) Format(v reflect.Value) (string, error) {
return "", errors.ErrUnsupported
}
func CellFormatterFromFormatter(f Formatter, rawResult bool) CellFormatter {
return CellFormatterFunc(func(ctx context.Context, view View, row, col int) (str string, raw bool, err error) {
str, err = f.Format(AsReflectCellView(view).ReflectCell(row, col))
return str, rawResult, err
})
}
func FormatterFromCellFormatter(f CellFormatter) Formatter {
return FormatterFunc(func(v reflect.Value) (string, error) {
str, _, err := f.FormatCell(context.Background(), &SingleReflectValueView{Val: v}, 0, 0)
return str, err
})
}