From e739c2d43871ef110297f4ed2f8baf7b4c796d4b Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 2 Apr 2024 08:51:06 +0300 Subject: [PATCH] util/reflect: detect json.Unmarshaler Signed-off-by: Vasiliy Tolstov --- util/reflect/reflect.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/util/reflect/reflect.go b/util/reflect/reflect.go index 87479554..f0cda32c 100644 --- a/util/reflect/reflect.go +++ b/util/reflect/reflect.go @@ -1,6 +1,7 @@ -package reflect // import "go.unistack.org/micro/v3/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() -- 2.45.2