bump: upgrade gopkg.in/yaml.v2 to gopkg.in/yaml.v3

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero
2022-10-03 05:24:51 +08:00
parent 2a7669f8fa
commit 14ba7cd156
19 changed files with 106 additions and 52 deletions
+18
View File
@@ -0,0 +1,18 @@
package maputil
import (
"bytes"
"gopkg.in/yaml.v3"
)
func YamlMarshal(v interface{}) ([]byte, error) {
var b bytes.Buffer
yamlEncoder := yaml.NewEncoder(&b)
yamlEncoder.SetIndent(2)
err := yamlEncoder.Encode(v)
defer func() {
_ = yamlEncoder.Close()
}()
return b.Bytes(), err
}
+34
View File
@@ -0,0 +1,34 @@
package maputil
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestYamlMarshal(t *testing.T) {
tests := []struct {
Name string `yaml:"name"`
Info []struct {
Age int `yaml:"age"`
Address string `yaml:"address"`
} `yaml:"info"`
expected string
}{
{
Name: "John",
Info: []struct {
Age int `yaml:"age"`
Address string `yaml:"address"`
}{{Age: 20, Address: "New York"}},
expected: "name: John\ninfo:\n - age: 20\n address: New York\n",
},
}
for _, tt := range tests {
actual, err := YamlMarshal(tt)
require.NoError(t, err)
require.Equal(t, tt.expected, string(actual))
}
}