1 Star 0 Fork 0

kade/mcube

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
yaml.go 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
kadegolang 提交于 2023-12-13 17:31 . copy
package yaml
import (
"github.com/emicklei/go-restful/v3"
"gopkg.in/yaml.v3"
)
const (
MIME_YAML = "application/yaml"
)
// NewEntityAccessorJSON returns a new EntityReaderWriter for accessing YAML content.
// This package is already initialized with such an accessor using the MIME_YAML contentType.
func NewEntityAccessorJSON(contentType string) restful.EntityReaderWriter {
return entityYAMLAccess{ContentType: contentType}
}
type entityYAMLAccess struct {
// This is used for setting the Content-Type header when writing
ContentType string
}
// Read unmarshalls the value from YAML
func (e entityYAMLAccess) Read(req *restful.Request, v interface{}) error {
decoder := yaml.NewDecoder(req.Request.Body)
return decoder.Decode(v)
}
// Write marshalls the value to YAML and set the Content-Type Header.
func (e entityYAMLAccess) Write(resp *restful.Response, status int, v interface{}) error {
return writeYAML(resp, status, e.ContentType, v)
}
// write marshalls the value to YAML and set the Content-Type Header.
func writeYAML(resp *restful.Response, status int, contentType string, v interface{}) error {
if v == nil {
resp.WriteHeader(status)
// do not write a nil representation
return nil
}
resp.Header().Set(restful.HEADER_ContentType, contentType)
resp.WriteHeader(status)
return yaml.NewEncoder(resp).Encode(v)
}
func init() {
restful.RegisterEntityAccessor(MIME_YAML, NewEntityAccessorJSON(MIME_YAML))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/go-kade/mcube.git
git@gitee.com:go-kade/mcube.git
go-kade
mcube
mcube
1225d9a674f1

搜索帮助

D67c1975 1850385 1daf7b77 1850385