mirror of
https://github.com/jimeh/go-render.git
synced 2026-02-19 11:26:39 +00:00
feat(text): expand supported value types
This commit is contained in:
37
text.go
37
text.go
@@ -5,19 +5,50 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// Text is a renderer that writes the given value to the writer as is. It
|
||||
// supports rendering the following types as plain 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 _ FormatRenderer = (*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 fmt.Stringer:
|
||||
_, err = w.Write([]byte(x.String()))
|
||||
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 ErrCannotRender
|
||||
return fmt.Errorf("%w: %T", ErrCannotRender, v)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user