-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.go
82 lines (73 loc) · 3.26 KB
/
render.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
70
71
72
73
74
75
76
77
78
79
80
81
82
// Package render provides a simple and flexible solutio to render a value to a
// io.Writer using different formats based on a format string argument.
//
// It is designed around using a custom type/struct to render your output.
// Thanks to Go's marshaling interfaces, you get JSON, YAML, and XML support
// almost for free. While plain text output is supported by the type
// implementing io.Reader, io.WriterTo, fmt.Stringer, or error interfaces, or by
// simply being a type which can easily be type cast to a byte slice.
//
// Originally intended to easily implement CLI tools which can output their data
// as plain text, as well as JSON/YAML with a simple switch of a format string.
// But it can just as easily render to any io.Writer.
//
// The package is designed to be flexible and extensible with a sensible set of
// defaults accessible via package level functions. You can create your own
// Renderer for custom formats, or create new handlers that support custom
// formats.
package render
import (
"fmt"
"io"
)
var (
// Err is the base error for the package. All errors returned by this
// package are wrapped with this error.
Err = fmt.Errorf("render")
ErrFailed = fmt.Errorf("%w: failed", Err)
// ErrCannotRender is returned when a value cannot be rendered. This may be
// due to the value not supporting the format, or the value itself not being
// renderable. Only Renderer implementations should return this error.
ErrCannotRender = fmt.Errorf("%w: cannot render", Err)
// Base is a renderer that supports all formats. It is used by the package
// level NewWith function to create new renderers with a sub-set of
// formats.
Base = New(map[string]Handler{
"binary": &Binary{},
"json": &JSON{},
"text": &Text{},
"xml": &XML{},
"yaml": &YAML{},
})
// Default is the default renderer that is used by package level Render,
// Compact, Pretty functions. It supports JSON, Text, and YAML formats.
Default = Base.NewWith("json", "text", "yaml")
)
// Render renders the given value to the given writer using the given format. If
// pretty is true, the value will be rendered "pretty" if the target format
// supports it, otherwise it will be rendered in a compact way.
//
// It uses the default renderer to render the value, which supports JSON, Text,
// and YAML formats out of the box.
//
// If you need to support a custom set of formats, use the New function to
// create a new Renderer with the formats you need. If you need new custom
// renderers, manually create a new Renderer.
func Render(w io.Writer, format string, pretty bool, v any) error {
return Default.Render(w, format, pretty, v)
}
// Compact is a convenience function that calls the Default renderer's Compact
// method. It is the same as calling Render with pretty set to false.
func Compact(w io.Writer, format string, v any) error {
return Default.Compact(w, format, v)
}
// Pretty is a convenience function that calls the Default renderer's Pretty
// method. It is the same as calling Render with pretty set to true.
func Pretty(w io.Writer, format string, v any) error {
return Default.Pretty(w, format, v)
}
// NewWith creates a new Renderer with the given formats. Only formats on the
// BaseRender will be supported.
func NewWith(formats ...string) *Renderer {
return Base.NewWith(formats...)
}