feat(render): experimental package to render arbitrary values to different formats

This commit is contained in:
2024-03-17 20:40:14 +00:00
commit 2b28f96bad
20 changed files with 1098 additions and 0 deletions

45
json.go Normal file
View File

@@ -0,0 +1,45 @@
package render
import (
"encoding/json"
"fmt"
"io"
)
// JSON is a renderer that marshals values to JSON.
type JSON struct {
// Pretty specifies whether the output should be pretty-printed. If true,
// the output will be indented and newlines will be added.
Pretty bool
// Prefix is the prefix added to each level of indentation when Pretty is
// true.
Prefix string
// Indent is the string added to each level of indentation when Pretty is
// true. If empty, two spaces will be used instead.
Indent string
}
var _ Renderer = (*JSON)(nil)
// Render marshals the given value to JSON.
func (j *JSON) Render(w io.Writer, v any) error {
enc := json.NewEncoder(w)
if j.Pretty {
prefix := j.Prefix
indent := j.Indent
if indent == "" {
indent = " "
}
enc.SetIndent(prefix, indent)
}
err := enc.Encode(v)
if err != nil {
return fmt.Errorf("%w: %w", Err, err)
}
return nil
}