[GIN-001] - Add TOML bining for gin (#3081)

Co-authored-by: GitstartHQ <gitstart@users.noreply.github.com>
This commit is contained in:
Valentine Oragbakosi
2022-05-27 16:34:43 -08:00
committed by GitHub
parent aa6002134e
commit ed03102ef0
12 changed files with 157 additions and 3 deletions

View File

@ -30,6 +30,7 @@ var (
_ Render = Reader{}
_ Render = AsciiJSON{}
_ Render = ProtoBuf{}
_ Render = TOML{}
)
func writeContentType(w http.ResponseWriter, value []string) {

32
render/toml.go Normal file
View File

@ -0,0 +1,32 @@
package render
import (
"net/http"
"github.com/pelletier/go-toml/v2"
)
// TOML contains the given interface object.
type TOML struct {
Data any
}
var TOMLContentType = []string{"application/toml; charset=utf-8"}
// Render (TOML) marshals the given interface object and writes data with custom ContentType.
func (r TOML) Render(w http.ResponseWriter) error {
r.WriteContentType(w)
bytes, err := toml.Marshal(r.Data)
if err != nil {
return err
}
_, err = w.Write(bytes)
return err
}
// WriteContentType (TOML) writes TOML ContentType for response.
func (r TOML) WriteContentType(w http.ResponseWriter) {
writeContentType(w, TOMLContentType)
}