-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
gyaml.go
40 lines (32 loc) · 925 Bytes
/
gyaml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2017 gf Author(https://github.com/gogf/gf). 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 (
"encoding/json"
"github.com/gogf/gf/util/gconv"
yaml3 "gopkg.in/yaml.v3"
)
func Encode(v interface{}) ([]byte, error) {
return yaml3.Marshal(v)
}
func Decode(v []byte) (interface{}, error) {
var result map[string]interface{}
if err := yaml3.Unmarshal(v, &result); err != nil {
return nil, err
}
return gconv.Map(result), nil
}
func DecodeTo(v []byte, result interface{}) error {
return yaml3.Unmarshal(v, result)
}
func ToJson(v []byte) ([]byte, error) {
if r, err := Decode(v); err != nil {
return nil, err
} else {
return json.Marshal(r)
}
}