mirror of
https://github.com/jimeh/evm.git
synced 2026-02-19 07:26:40 +00:00
chore: initial commit
This commit is contained in:
52
render.go
Normal file
52
render.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func render(w io.Writer, format string, v interface{}) error {
|
||||
if format == "yaml" {
|
||||
return renderYAML(w, v)
|
||||
}
|
||||
|
||||
if format == "json" {
|
||||
return renderJSON(w, v)
|
||||
}
|
||||
|
||||
if wt, ok := v.(io.WriterTo); ok {
|
||||
_, err := wt.WriteTo(w)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
if tm, ok := v.(encoding.TextMarshaler); ok {
|
||||
b, err := tm.MarshalText()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = w.Write(b)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return renderYAML(w, v)
|
||||
}
|
||||
|
||||
func renderYAML(w io.Writer, v interface{}) error {
|
||||
enc := yaml.NewEncoder(w)
|
||||
enc.SetIndent(2)
|
||||
|
||||
return enc.Encode(v)
|
||||
}
|
||||
|
||||
func renderJSON(w io.Writer, v interface{}) error {
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetIndent("", " ")
|
||||
|
||||
return enc.Encode(v)
|
||||
}
|
||||
Reference in New Issue
Block a user