yaml.go 824 B

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