1
0
mirror of https://github.com/go-kratos/kratos.git synced 2026-05-22 10:15:24 +02:00

add yaml encoder (#924)

This commit is contained in:
Tony Chen
2021-05-17 21:52:23 +08:00
committed by GitHub
parent 2de0fa330c
commit e989bb04e3
9 changed files with 69 additions and 11 deletions
+37
View File
@@ -0,0 +1,37 @@
package yaml
import (
"reflect"
"github.com/go-kratos/kratos/v2/encoding"
"gopkg.in/yaml.v2"
)
// Name is the name registered for the json codec.
const Name = "yaml"
func init() {
encoding.RegisterCodec(codec{})
}
// codec is a Codec implementation with json.
type codec struct{}
func (codec) Marshal(v interface{}) ([]byte, error) {
return yaml.Marshal(v)
}
func (codec) Unmarshal(data []byte, v interface{}) error {
rv := reflect.ValueOf(v)
for rv.Kind() == reflect.Ptr {
if rv.IsNil() {
rv.Set(reflect.New(rv.Type().Elem()))
}
rv = rv.Elem()
}
return yaml.Unmarshal(data, v)
}
func (codec) Name() string {
return Name
}