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

34
binary.go Normal file
View File

@@ -0,0 +1,34 @@
package render
import (
"encoding"
"fmt"
"io"
)
// Binary can render values which implment the encoding.BinaryMarshaler
// interface.
type Binary struct{}
var _ Renderer = (*Binary)(nil)
// Render writes result of calling MarshalBinary() on v. If v does not implment
// encoding.BinaryMarshaler the ErrCannotRander error will be returned.
func (bm *Binary) Render(w io.Writer, v any) error {
x, ok := v.(encoding.BinaryMarshaler)
if !ok {
return ErrCannotRender
}
b, err := x.MarshalBinary()
if err != nil {
return fmt.Errorf("%w: %w", Err, err)
}
_, err = w.Write(b)
if err != nil {
return fmt.Errorf("%w: %w", Err, err)
}
return nil
}