11 Star 11 Fork 0

Gitee 极速下载 / goa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/goadesign/goa
克隆/下载
marshal.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
Raphael Simon 提交于 2020-07-10 23:58 . OpenAPI v3 (v2) (#2608)
package openapi
import (
"encoding/json"
"gopkg.in/yaml.v2"
)
// MarshalJSON produces the JSON resulting from encoding an object composed of
// the fields in v (which must me a struct) and the keys in extensions.
func MarshalJSON(v interface{}, extensions map[string]interface{}) ([]byte, error) {
marshaled, err := json.Marshal(v)
if err != nil {
return nil, err
}
if len(extensions) == 0 {
return marshaled, nil
}
var unmarshaled interface{}
if err := json.Unmarshal(marshaled, &unmarshaled); err != nil {
return nil, err
}
asserted := unmarshaled.(map[string]interface{})
for k, v := range extensions {
asserted[k] = v
}
merged, err := json.Marshal(asserted)
if err != nil {
return nil, err
}
return merged, nil
}
// MarshalYAML produces the JSON resulting from encoding an object composed of
// the fields in v (which must me a struct) and the keys in extensions.
func MarshalYAML(v interface{}, extensions map[string]interface{}) (interface{}, error) {
if len(extensions) == 0 {
return v, nil
}
marshaled, err := yaml.Marshal(v)
if err != nil {
return nil, err
}
var unmarshaled map[string]interface{}
if err := yaml.Unmarshal(marshaled, &unmarshaled); err != nil {
return nil, err
}
for k, v := range extensions {
unmarshaled[k] = v
}
return unmarshaled, nil
}
1
https://gitee.com/mirrors/goa.git
git@gitee.com:mirrors/goa.git
mirrors
goa
goa
v2.2.5

搜索帮助