add yaml encoder (#924)

pull/925/head
Tony Chen 4 years ago committed by GitHub
parent 2de0fa330c
commit e989bb04e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      config/config.go
  2. 1
      config/file/file.go
  3. 10
      config/file/format.go
  4. 2
      config/source.go
  5. 37
      encoding/yaml/yaml.go
  6. 4
      examples/config/main.go
  7. 1
      go.mod
  8. 2
      go.sum
  9. 1
      transport/transport.go

@ -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 {

@ -36,6 +36,7 @@ func (f *file) loadFile(path string) (*config.KeyValue, error) {
}
return &config.KeyValue{
Key: info.Name(),
Format: format(info.Name()),
Value: data,
}, nil
}

@ -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 ""
}

@ -4,7 +4,7 @@ package config
type KeyValue struct {
Key string
Value []byte
Metadata map[string]string
Format string
}
// Source is config source.

@ -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)

@ -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
)

@ -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…
Cancel
Save