1// Copyright 2014 Manu Martinez-Almeida.  All rights reserved.
2// Use of this source code is governed by a MIT style
3// license that can be found in the LICENSE file.
4
5package render
6
7import (
8	"fmt"
9	"net/http"
10
11	"github.com/gin-gonic/gin/internal/bytesconv"
12)
13
14// String contains the given interface object slice and its format.
15type String struct {
16	Format string
17	Data   []interface{}
18}
19
20var plainContentType = []string{"text/plain; charset=utf-8"}
21
22// Render (String) writes data with custom ContentType.
23func (r String) Render(w http.ResponseWriter) error {
24	return WriteString(w, r.Format, r.Data)
25}
26
27// WriteContentType (String) writes Plain ContentType.
28func (r String) WriteContentType(w http.ResponseWriter) {
29	writeContentType(w, plainContentType)
30}
31
32// WriteString writes data according to its format and write custom ContentType.
33func WriteString(w http.ResponseWriter, format string, data []interface{}) (err error) {
34	writeContentType(w, plainContentType)
35	if len(data) > 0 {
36		_, err = fmt.Fprintf(w, format, data...)
37		return
38	}
39	_, err = w.Write(bytesconv.StringToBytes(format))
40	return
41}
42