2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-01-19 16:19:48 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-01-19 16:19:48 +08:00
|
|
|
|
2019-01-15 23:27:47 +08:00
|
|
|
// Package gyaml provides accessing and converting for YAML content.
|
2018-01-19 16:19:48 +08:00
|
|
|
package gyaml
|
|
|
|
|
2019-08-01 14:07:25 +08:00
|
|
|
import "github.com/ghodss/yaml"
|
2018-01-19 16:19:48 +08:00
|
|
|
|
|
|
|
func Encode(v interface{}) ([]byte, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
return yaml.Marshal(v)
|
2018-01-19 16:19:48 +08:00
|
|
|
}
|
|
|
|
|
2018-01-20 11:09:27 +08:00
|
|
|
func Decode(v []byte) (interface{}, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
var result interface{}
|
|
|
|
if err := yaml.Unmarshal(v, &result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
2018-01-19 16:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeTo(v []byte, result interface{}) error {
|
2019-06-19 09:06:52 +08:00
|
|
|
return yaml.Unmarshal(v, &result)
|
2018-01-19 16:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ToJson(v []byte) ([]byte, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
return yaml.YAMLToJSON(v)
|
|
|
|
}
|