mirror of
https://github.com/jimeh/go-render.git
synced 2026-02-19 03:16:39 +00:00
feat(render): experimental package to render arbitrary values to different formats
This commit is contained in:
34
binary.go
Normal file
34
binary.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user