mirror of
https://github.com/go-kratos/kratos.git
synced 2025-02-09 13:36:57 +02:00
add yaml encoder (#924)
This commit is contained in:
parent
2de0fa330c
commit
e989bb04e3
@ -1,13 +1,20 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/encoding"
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
|
||||
// init encoding
|
||||
_ "github.com/go-kratos/kratos/v2/encoding/json"
|
||||
_ "github.com/go-kratos/kratos/v2/encoding/proto"
|
||||
_ "github.com/go-kratos/kratos/v2/encoding/xml"
|
||||
_ "github.com/go-kratos/kratos/v2/encoding/yaml"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -45,7 +52,10 @@ func New(opts ...Option) Config {
|
||||
options := options{
|
||||
logger: log.DefaultLogger,
|
||||
decoder: func(kv *KeyValue, v map[string]interface{}) error {
|
||||
return json.Unmarshal(kv.Value, &v)
|
||||
if codec := encoding.GetCodec(kv.Format); codec != nil {
|
||||
return codec.Unmarshal(kv.Value, &v)
|
||||
}
|
||||
return fmt.Errorf("unsupported key: %s format: %s", kv.Key, kv.Format)
|
||||
},
|
||||
}
|
||||
for _, o := range opts {
|
||||
|
@ -35,8 +35,9 @@ func (f *file) loadFile(path string) (*config.KeyValue, error) {
|
||||
return nil, err
|
||||
}
|
||||
return &config.KeyValue{
|
||||
Key: info.Name(),
|
||||
Value: data,
|
||||
Key: info.Name(),
|
||||
Format: format(info.Name()),
|
||||
Value: data,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
10
config/file/format.go
Normal file
10
config/file/format.go
Normal file
@ -0,0 +1,10 @@
|
||||
package file
|
||||
|
||||
import "strings"
|
||||
|
||||
func format(name string) string {
|
||||
if p := strings.Split(name, "."); len(p) > 1 {
|
||||
return p[len(p)-1]
|
||||
}
|
||||
return ""
|
||||
}
|
@ -2,9 +2,9 @@ package config
|
||||
|
||||
// KeyValue is config key value.
|
||||
type KeyValue struct {
|
||||
Key string
|
||||
Value []byte
|
||||
Metadata map[string]string
|
||||
Key string
|
||||
Value []byte
|
||||
Format string
|
||||
}
|
||||
|
||||
// Source is config source.
|
||||
|
37
encoding/yaml/yaml.go
Normal file
37
encoding/yaml/yaml.go
Normal 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
|
||||
}
|
@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/go-kratos/kratos/v2/config"
|
||||
"github.com/go-kratos/kratos/v2/config/file"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var flagconf string
|
||||
@ -21,9 +20,6 @@ func main() {
|
||||
config.WithSource(
|
||||
file.NewSource(flagconf),
|
||||
),
|
||||
config.WithDecoder(func(kv *config.KeyValue, v map[string]interface{}) error {
|
||||
return yaml.Unmarshal(kv.Value, v)
|
||||
}),
|
||||
)
|
||||
if err := c.Load(); err != nil {
|
||||
panic(err)
|
||||
|
1
go.mod
1
go.mod
@ -18,4 +18,5 @@ require (
|
||||
google.golang.org/genproto v0.0.0-20210114201628-6edceaf6022f
|
||||
google.golang.org/grpc v1.35.0
|
||||
google.golang.org/protobuf v1.25.0
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
||||
|
2
go.sum
2
go.sum
@ -125,6 +125,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
_ "github.com/go-kratos/kratos/v2/encoding/json"
|
||||
_ "github.com/go-kratos/kratos/v2/encoding/proto"
|
||||
_ "github.com/go-kratos/kratos/v2/encoding/xml"
|
||||
_ "github.com/go-kratos/kratos/v2/encoding/yaml"
|
||||
)
|
||||
|
||||
// Server is transport server.
|
||||
|
Loading…
x
Reference in New Issue
Block a user