github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin/binding/yaml.go (about)

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