util/reflect: detect json.Unmarshaler
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
f83a29eb67
commit
e739c2d438
@ -1,6 +1,7 @@
|
|||||||
package reflect // import "go.unistack.org/micro/v3/util/reflect"
|
package reflect
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -45,15 +46,23 @@ func SliceAppend(b bool) Option {
|
|||||||
|
|
||||||
// Merge merges map[string]interface{} to destination struct
|
// Merge merges map[string]interface{} to destination struct
|
||||||
func Merge(dst interface{}, mp map[string]interface{}, opts ...Option) error {
|
func Merge(dst interface{}, mp map[string]interface{}, opts ...Option) error {
|
||||||
var err error
|
|
||||||
var sval reflect.Value
|
|
||||||
var fname string
|
|
||||||
|
|
||||||
options := Options{}
|
options := Options{}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if unmarshaler, ok := dst.(json.Unmarshaler); ok {
|
||||||
|
buf, err := json.Marshal(mp)
|
||||||
|
if err == nil {
|
||||||
|
err = unmarshaler.UnmarshalJSON(buf)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var sval reflect.Value
|
||||||
|
var fname string
|
||||||
|
|
||||||
dviface := reflect.ValueOf(dst)
|
dviface := reflect.ValueOf(dst)
|
||||||
if dviface.Kind() == reflect.Ptr {
|
if dviface.Kind() == reflect.Ptr {
|
||||||
dviface = dviface.Elem()
|
dviface = dviface.Elem()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user