From 0c9ca254e6e23aaf082c0da5125f3b49fa1cd354 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Fri, 13 Jan 2023 16:49:22 +0800 Subject: [PATCH] 1 --- internal/api_to_rpc/copy.go | 42 ++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/internal/api_to_rpc/copy.go b/internal/api_to_rpc/copy.go index 5052afef9..f2a887bb3 100644 --- a/internal/api_to_rpc/copy.go +++ b/internal/api_to_rpc/copy.go @@ -6,21 +6,6 @@ import ( "reflect" ) -func CopyAny(from, to interface{}) { - t := reflect.ValueOf(to) - if t.Kind() == reflect.Ptr { - t = t.Elem() - } - if !t.CanSet() { - return - } - f := reflect.ValueOf(from) - if isBaseNil(f) { - return - } - copyAny(f, t) -} - func setBaseValue(from, to reflect.Value) { if isBaseNil(from) { return @@ -40,6 +25,21 @@ func setBaseValue(from, to reflect.Value) { to.Set(v) } +func CopyAny(from, to interface{}) { + t := reflect.ValueOf(to) + if t.Kind() == reflect.Ptr { + t = t.Elem() + } + if !t.CanSet() { + return + } + f := reflect.ValueOf(from) + if isBaseNil(f) { + return + } + copyAny(f, t) +} + func copyAny(from, to reflect.Value) { if !to.CanSet() { return @@ -47,6 +47,9 @@ func copyAny(from, to reflect.Value) { if isBaseNil(from) { return } + if isBaseNil(to) { + to.Set(getBaseZeroValue(to.Type())) + } btfrom := baseType(from.Type()) btto := baseType(to.Type()) if typeEq(btfrom, btto) { @@ -118,6 +121,15 @@ func baseType(t reflect.Type) reflect.Type { return t } +func baseLayer(t reflect.Type) int { + var layer int + for t.Kind() == reflect.Ptr { + layer++ + t = t.Elem() + } + return layer +} + func typeEq(t1, t2 reflect.Type) bool { return t1.String() == t2.String() }