feat(encoding): support yml encoding with yaml codec

pull/2811/head
yeebing 2 years ago
parent aed172b8dd
commit e2c3bb8f24
  1. 17
      encoding/yaml/yml.go
  2. 110
      encoding/yaml/yml_test.go

@ -0,0 +1,17 @@
package yaml
import "github.com/go-kratos/kratos/v2/encoding"
const NameAlias = "yml"
type codecAlias struct {
codec
}
func init() {
encoding.RegisterCodec(codecAlias{})
}
func (codecAlias) Name() string {
return NameAlias
}

@ -0,0 +1,110 @@
package yaml
import (
"math"
"reflect"
"testing"
)
func TestCodec2Name(t *testing.T) {
if (codecAlias{}).Name() != NameAlias {
t.Fatal("(codecAlias{}).Name should equal NameAlias")
}
}
func TestCodecAlias_Unmarshal(t *testing.T) {
tests := []struct {
data string
value interface{}
}{
{
"",
(*struct{})(nil),
},
{
"{}", &struct{}{},
},
{
"v: hi",
map[string]string{"v": "hi"},
},
{
"v: hi", map[string]interface{}{"v": "hi"},
},
{
"v: true",
map[string]string{"v": "true"},
},
{
"v: true",
map[string]interface{}{"v": true},
},
{
"v: 10",
map[string]interface{}{"v": 10},
},
{
"v: 0b10",
map[string]interface{}{"v": 2},
},
{
"v: 0xA",
map[string]interface{}{"v": 10},
},
{
"v: 4294967296",
map[string]int64{"v": 4294967296},
},
{
"v: 0.1",
map[string]interface{}{"v": 0.1},
},
{
"v: .1",
map[string]interface{}{"v": 0.1},
},
{
"v: .Inf",
map[string]interface{}{"v": math.Inf(+1)},
},
{
"v: -.Inf",
map[string]interface{}{"v": math.Inf(-1)},
},
{
"v: -10",
map[string]interface{}{"v": -10},
},
{
"v: -.1",
map[string]interface{}{"v": -0.1},
},
}
for _, tt := range tests {
v := reflect.ValueOf(tt.value).Type()
value := reflect.New(v)
err := (codecAlias{}).Unmarshal([]byte(tt.data), value.Interface())
if err != nil {
t.Fatalf("(codecAlias{}).Unmarshal should not return err")
}
}
spec := struct {
A string
B map[string]interface{}
}{A: "a"}
err := (codecAlias{}).Unmarshal([]byte("v: hi"), &spec.B)
if err != nil {
t.Fatalf("(codecAlias{}).Unmarshal should not return err")
}
}
func TestCodecAlias_Marshal(t *testing.T) {
value := map[string]string{"v": "hi"}
got, err := (codecAlias{}).Marshal(value)
if err != nil {
t.Fatalf("should not return err")
}
if string(got) != "v: hi\n" {
t.Fatalf("want \"v: hi\n\" return \"%s\"", string(got))
}
}
Loading…
Cancel
Save