2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). 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 (
|
2022-01-17 17:10:44 +08:00
|
|
|
"bytes"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2021-11-16 19:29:02 +08:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
|
2021-10-25 19:17:56 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/json"
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2019-08-01 17:12:58 +08:00
|
|
|
)
|
2018-01-19 16:19:48 +08:00
|
|
|
|
2021-10-25 19:17:56 +08:00
|
|
|
func Encode(value interface{}) (out []byte, err error) {
|
|
|
|
|
if out, err = yaml.Marshal(value); err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `yaml.Marshal failed`)
|
2021-10-25 19:17:56 +08:00
|
|
|
}
|
|
|
|
|
return
|
2018-01-19 16:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
2022-01-17 17:10:44 +08:00
|
|
|
func EncodeIndent(value interface{}, indent string) (out []byte, err error) {
|
|
|
|
|
out, err = Encode(value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if indent != "" {
|
|
|
|
|
var (
|
|
|
|
|
buffer = bytes.NewBuffer(nil)
|
|
|
|
|
array = strings.Split(strings.TrimSpace(string(out)), "\n")
|
|
|
|
|
)
|
|
|
|
|
for _, v := range array {
|
|
|
|
|
buffer.WriteString(indent)
|
|
|
|
|
buffer.WriteString(v)
|
|
|
|
|
buffer.WriteString("\n")
|
|
|
|
|
}
|
|
|
|
|
out = buffer.Bytes()
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-25 19:17:56 +08:00
|
|
|
func Decode(value []byte) (interface{}, error) {
|
|
|
|
|
var (
|
|
|
|
|
result map[string]interface{}
|
|
|
|
|
err error
|
|
|
|
|
)
|
|
|
|
|
if err = yaml.Unmarshal(value, &result); err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `yaml.Unmarshal failed`)
|
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
|
|
|
}
|
|
|
|
|
|
2021-10-25 19:17:56 +08:00
|
|
|
func DecodeTo(value []byte, result interface{}) (err error) {
|
|
|
|
|
err = yaml.Unmarshal(value, result)
|
|
|
|
|
if err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `yaml.Unmarshal failed`)
|
2021-10-25 19:17:56 +08:00
|
|
|
}
|
|
|
|
|
return
|
2018-01-19 16:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-25 19:17:56 +08:00
|
|
|
func ToJson(value []byte) (out []byte, err error) {
|
|
|
|
|
var (
|
|
|
|
|
result interface{}
|
|
|
|
|
)
|
|
|
|
|
if result, err = Decode(value); err != nil {
|
2019-08-01 17:12:58 +08:00
|
|
|
return nil, err
|
|
|
|
|
} else {
|
2021-12-21 22:59:14 +08:00
|
|
|
return json.Marshal(result)
|
2019-08-01 17:12:58 +08:00
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|