gf/encoding/gyaml/gyaml.go

55 lines
1.3 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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,
// You can obtain one at https://github.com/gogf/gf.
// Package gyaml provides accessing and converting for YAML content.
package gyaml
import (
"gopkg.in/yaml.v3"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
)
func Encode(value interface{}) (out []byte, err error) {
if out, err = yaml.Marshal(value); err != nil {
err = gerror.Wrap(err, `yaml.Marshal failed`)
}
return
}
func Decode(value []byte) (interface{}, error) {
var (
result map[string]interface{}
err error
)
if err = yaml.Unmarshal(value, &result); err != nil {
err = gerror.Wrap(err, `yaml.Unmarshal failed`)
return nil, err
}
return gconv.MapDeep(result), nil
}
func DecodeTo(value []byte, result interface{}) (err error) {
err = yaml.Unmarshal(value, result)
if err != nil {
err = gerror.Wrap(err, `yaml.Unmarshal failed`)
}
return
}
func ToJson(value []byte) (out []byte, err error) {
var (
result interface{}
)
if result, err = Decode(value); err != nil {
return nil, err
} else {
return json.Marshal(result)
}
}