github.com/songzhibin97/gkit@v1.2.13/tools/bind/yaml.go (about)

     1  package bind
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"net/http"
     7  
     8  	"gopkg.in/yaml.v2"
     9  )
    10  
    11  type yamlBinding struct{}
    12  
    13  func (yamlBinding) Name() string {
    14  	return "yaml"
    15  }
    16  
    17  func (yamlBinding) Bind(req *http.Request, obj interface{}) error {
    18  	return decodeYAML(req.Body, obj)
    19  }
    20  
    21  func (yamlBinding) BindBody(body []byte, obj interface{}) error {
    22  	return decodeYAML(bytes.NewReader(body), obj)
    23  }
    24  
    25  func decodeYAML(r io.Reader, obj interface{}) error {
    26  	decoder := yaml.NewDecoder(r)
    27  	return decoder.Decode(obj)
    28  }