From 2db8293160576947122eb9a928f893e3ed968060 Mon Sep 17 00:00:00 2001 From: fancl Date: Tue, 12 Sep 2023 16:50:45 +0800 Subject: [PATCH] add interface supported --- util/reflect/reflect.go | 2 ++ util/reflection/reflection_test.go | 36 ++++++++++++++++-------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/util/reflect/reflect.go b/util/reflect/reflect.go index d6b1130..6bf6087 100644 --- a/util/reflect/reflect.go +++ b/util/reflect/reflect.go @@ -114,6 +114,8 @@ func safeAssignment(variable reflect.Value, value interface{}) (err error) { default: variable.SetString(fmt.Sprint(value)) } + case reflect.Interface: + variable.Set(rv) default: err = fmt.Errorf("unsupported kind %s", kind) } diff --git a/util/reflection/reflection_test.go b/util/reflection/reflection_test.go index 4841c7a..7ba7a86 100644 --- a/util/reflection/reflection_test.go +++ b/util/reflection/reflection_test.go @@ -28,26 +28,28 @@ type fake struct { func TestSetter(t *testing.T) { dst := &fake{} - ms := map[string]any{ - "name": "aa", - "age": "5", - "usage": []map[string]any{ - { - "in": 15, - "bs": map[string]any{ - "aa": "vv", - }, + vs := map[string]any{ + "name": "xxx", + } + vvs := []map[string]any{ + { + "in": 15, + "bs": map[string]any{ + "aa": "vv", }, }, - "xx": map[string]any{"in": 45}, - "ax": map[string]any{"in": 55}, - "ss": []string{"11", "ss"}, - "ds": []int{55, 55, 34}, - "ms": map[string]any{"aa": "23"}, + } + ms := map[string]any{ + "name": "aa", + "age": "5", + "usage": vvs, + "xx": map[string]any{"in": 45}, + "ax": map[string]any{"in": 55}, + "ss": []string{"11", "ss"}, + "ds": []int{55, 55, 34}, + "ms": map[string]any{"aa": "23"}, "ab": map[string]any{ - "xx": map[string]any{ - "name": "xxx", - }, + "xx": vs, }, } err := Setter(dst, ms)