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 17:12:58 +08:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2019-09-24 20:19:18 +08:00
|
|
|
"github.com/gf-third/yaml"
|
2019-08-01 17:12:58 +08:00
|
|
|
"github.com/gogf/gf/util/gconv"
|
|
|
|
)
|
2018-01-19 16:19:48 +08:00
|
|
|
|
|
|
|
func Encode(v interface{}) ([]byte, error) {
|
2019-09-24 20:19:18 +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-08-01 17:12:58 +08:00
|
|
|
var result map[string]interface{}
|
2019-09-24 20:19:18 +08:00
|
|
|
if err := yaml.Unmarshal(v, &result); err != nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-17 20:53:20 +08:00
|
|
|
return gconv.MapDeep(result), nil
|
2018-01-19 16:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeTo(v []byte, result interface{}) error {
|
2019-09-24 20:19:18 +08:00
|
|
|
return yaml.Unmarshal(v, result)
|
2018-01-19 16:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ToJson(v []byte) ([]byte, error) {
|
2019-08-01 17:12:58 +08:00
|
|
|
if r, err := Decode(v); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return json.Marshal(r)
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|