From a693bbbf9b69c1cedd13022732bb8c5df17621ec Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 18 Apr 2021 17:55:48 +0200 Subject: [PATCH] fix: reflect assign value to pointer of value in call argument --- internal/dynfunc/handler.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/internal/dynfunc/handler.go b/internal/dynfunc/handler.go index c12069c..4e36b71 100644 --- a/internal/dynfunc/handler.go +++ b/internal/dynfunc/handler.go @@ -2,6 +2,7 @@ package dynfunc import ( "fmt" + "log" "reflect" "git.xdrm.io/go/aicra/api" @@ -70,6 +71,29 @@ func (h *Handler) Handle(data map[string]interface{}) (map[string]interface{}, a if !inData { continue } + + var refvalue = reflect.ValueOf(value) + + // T to pointer of T + if field.Kind() == reflect.Ptr { + var ptrType = field.Type().Elem() + + if !refvalue.Type().ConvertibleTo(ptrType) { + log.Printf("Cannot convert %v into %v", refvalue.Type(), ptrType) + return nil, api.ErrUncallableService + } + + ptr := reflect.New(ptrType) + ptr.Elem().Set(reflect.ValueOf(value).Convert(ptrType)) + + field.Set(ptr) + continue + } + if !reflect.ValueOf(value).Type().ConvertibleTo(field.Type()) { + log.Printf("Cannot convert %v into %v", reflect.ValueOf(value).Type(), field.Type()) + return nil, api.ErrUncallableService + } + field.Set(reflect.ValueOf(value).Convert(field.Type())) } callArgs = append(callArgs, callStruct)