gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

text.go (1091B)


      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 
      5 package render
      6 
      7 import (
      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.
     15 type String struct {
     16 	Format string
     17 	Data   []any
     18 }
     19 
     20 var plainContentType = []string{"text/plain; charset=utf-8"}
     21 
     22 // Render (String) writes data with custom ContentType.
     23 func (r String) Render(w http.ResponseWriter) error {
     24 	return WriteString(w, r.Format, r.Data)
     25 }
     26 
     27 // WriteContentType (String) writes Plain ContentType.
     28 func (r String) WriteContentType(w http.ResponseWriter) {
     29 	writeContentType(w, plainContentType)
     30 }
     31 
     32 // WriteString writes data according to its format and write custom ContentType.
     33 func WriteString(w http.ResponseWriter, format string, data []any) (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 }