toml.go 820 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2022 Gin Core Team. 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. package render
  5. import (
  6. "net/http"
  7. "github.com/pelletier/go-toml/v2"
  8. )
  9. // TOML contains the given interface object.
  10. type TOML struct {
  11. Data any
  12. }
  13. var TOMLContentType = []string{"application/toml; charset=utf-8"}
  14. // Render (TOML) marshals the given interface object and writes data with custom ContentType.
  15. func (r TOML) Render(w http.ResponseWriter) error {
  16. r.WriteContentType(w)
  17. bytes, err := toml.Marshal(r.Data)
  18. if err != nil {
  19. return err
  20. }
  21. _, err = w.Write(bytes)
  22. return err
  23. }
  24. // WriteContentType (TOML) writes TOML ContentType for response.
  25. func (r TOML) WriteContentType(w http.ResponseWriter) {
  26. writeContentType(w, TOMLContentType)
  27. }