mirror of
https://github.com/go-kratos/kratos.git
synced 2026-05-22 10:15:24 +02:00
+1
-6
@@ -7,9 +7,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/encoding"
|
||||
// init json encoder
|
||||
_ "github.com/go-kratos/kratos/v2/encoding/json"
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
)
|
||||
|
||||
@@ -20,8 +17,6 @@ var (
|
||||
ErrTypeAssert = errors.New("type assert error")
|
||||
|
||||
_ Config = (*config)(nil)
|
||||
|
||||
codec = encoding.GetCodec("json")
|
||||
)
|
||||
|
||||
// Observer is config observer.
|
||||
@@ -125,7 +120,7 @@ func (c *config) Scan(v interface{}) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return codec.Unmarshal(data, v)
|
||||
return unmarshalJSON(data, v)
|
||||
}
|
||||
|
||||
func (c *config) Watch(key string, o Observer) error {
|
||||
|
||||
+20
-3
@@ -1,10 +1,13 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/imdario/mergo"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// Reader is config reader.
|
||||
@@ -71,16 +74,16 @@ func (r *reader) Value(path string) (Value, bool) {
|
||||
}
|
||||
|
||||
func (r *reader) Source() ([]byte, error) {
|
||||
return codec.Marshal(r.values)
|
||||
return marshalJSON(r.values)
|
||||
}
|
||||
|
||||
func cloneMap(src map[string]interface{}) (map[string]interface{}, error) {
|
||||
data, err := codec.Marshal(src)
|
||||
data, err := marshalJSON(src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dst := make(map[string]interface{})
|
||||
if err = codec.Unmarshal(data, &dst); err != nil {
|
||||
if err = unmarshalJSON(data, &dst); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dst, nil
|
||||
@@ -104,3 +107,17 @@ func convertMap(src interface{}) interface{} {
|
||||
return src
|
||||
}
|
||||
}
|
||||
|
||||
func marshalJSON(v interface{}) ([]byte, error) {
|
||||
if m, ok := v.(proto.Message); ok {
|
||||
return protojson.Marshal(m)
|
||||
}
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
func unmarshalJSON(data []byte, v interface{}) error {
|
||||
if m, ok := v.(proto.Message); ok {
|
||||
return protojson.Unmarshal(data, m)
|
||||
}
|
||||
return json.Unmarshal(data, v)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user