util/reflect: detect json.Unmarshaler
Some checks failed
Go / test (push) Failing after 6m45s
/ autoupdate (push) Successful in 1m21s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-04-02 08:51:06 +03:00
parent e89cfdc80d
commit 47497b49b3

View File

@ -1,6 +1,7 @@
package reflect // import "go.unistack.org/micro/v4/util/reflect"
package reflect
import (
"encoding/json"
"errors"
"fmt"
"reflect"
@ -45,15 +46,23 @@ func SliceAppend(b bool) Option {
// Merge merges map[string]interface{} to destination struct
func Merge(dst interface{}, mp map[string]interface{}, opts ...Option) error {
var err error
var sval reflect.Value
var fname string
options := Options{}
for _, o := range opts {
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)
if dviface.Kind() == reflect.Ptr {
dviface = dviface.Elem()