yaml.go 708 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2018 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 binding
  5. import (
  6. "bytes"
  7. "io"
  8. "net/http"
  9. "gopkg.in/yaml.v2"
  10. )
  11. type yamlBinding struct{}
  12. func (yamlBinding) Name() string {
  13. return "yaml"
  14. }
  15. func (yamlBinding) Bind(req *http.Request, obj interface{}) error {
  16. return decodeYAML(req.Body, obj)
  17. }
  18. func (yamlBinding) BindBody(body []byte, obj interface{}) error {
  19. return decodeYAML(bytes.NewReader(body), obj)
  20. }
  21. func decodeYAML(r io.Reader, obj interface{}) error {
  22. decoder := yaml.NewDecoder(r)
  23. if err := decoder.Decode(obj); err != nil {
  24. return err
  25. }
  26. return validate(obj)
  27. }