-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.go
69 lines (62 loc) · 1.49 KB
/
text.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
67
68
69
package render
import (
"fmt"
"io"
)
// Text is a Handler that writes the given value to the writer as text,
// supporting multiple types and interfaces.
//
// Supports rendering the following types as text:
//
// - []byte
// - []rune
// - string
// - int, int8, int16, int32, int64
// - uint, uint8, uint16, uint32, uint64
// - float32, float64
// - bool
// - io.Reader
// - io.WriterTo
// - fmt.Stringer
// - error
//
// If the value is of any other type, a ErrCannotRender error will be returned.
type Text struct{}
var (
_ Handler = (*Text)(nil)
_ FormatsHandler = (*Text)(nil)
)
// Render writes the given value to the writer as text.
func (t *Text) Render(w io.Writer, v any) error {
var err error
switch x := v.(type) {
case []byte:
_, err = w.Write(x)
case []rune:
_, err = w.Write([]byte(string(x)))
case string:
_, err = w.Write([]byte(x))
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64, bool:
_, err = fmt.Fprintf(w, "%v", x)
case io.Reader:
_, err = io.Copy(w, x)
case io.WriterTo:
_, err = x.WriteTo(w)
case fmt.Stringer:
_, err = w.Write([]byte(x.String()))
case error:
_, err = w.Write([]byte(x.Error()))
default:
return fmt.Errorf("%w: %T", ErrCannotRender, v)
}
if err != nil {
return fmt.Errorf("%w: %w", ErrFailed, err)
}
return nil
}
// Formats returns a list of format strings that this Handler supports.
func (t *Text) Formats() []string {
return []string{"text", "txt", "plain"}
}