Alanxtl commented on code in PR #3174:
URL: https://github.com/apache/dubbo-go/pull/3174#discussion_r2724948899


##########
filter/generic/filter.go:
##########
@@ -152,8 +153,65 @@ func (f *genericFilter) Invoke(ctx context.Context, 
invoker base.Invoker, inv ba
        return invoker.Invoke(ctx, inv)
 }
 
-// OnResponse dummy process, returns the result directly
-func (f *genericFilter) OnResponse(_ context.Context, result result.Result, _ 
base.Invoker,
-       _ base.Invocation) result.Result {
-       return result
+// OnResponse deserializes the map result to the target struct if reply is 
provided.
+// If inv.Reply() is a non-nil pointer to a struct, the map result will be 
automatically
+// deserialized into it using the appropriate generalizer.
+func (f *genericFilter) OnResponse(_ context.Context, res result.Result, 
invoker base.Invoker,
+       inv base.Invocation) result.Result {
+       // Only process if this is a generic call and there's no error
+       if res.Error() != nil {
+               return res
+       }
+
+       // Check if this is a generic invocation
+       if !isGeneric(invoker.GetURL().GetParam(constant.GenericKey, "")) {
+               return res
+       }
+
+       // Get the reply from invocation
+       reply := inv.Reply()
+       if reply == nil {
+               return res
+       }
+
+       // Check if reply is a valid pointer
+       replyValue := reflect.ValueOf(reply)
+       if replyValue.Kind() != reflect.Ptr || replyValue.IsNil() {
+               return res
+       }
+
+       // Get the result data
+       data := res.Result()
+       if data == nil {
+               return res
+       }
+
+       // Check if data is a map type that needs to be deserialized
+       dataValue := reflect.ValueOf(data)
+       if dataValue.Kind() != reflect.Map && dataValue.Kind() != reflect.Slice 
{
+               // If data is not a map or slice, it's already a primitive 
type, no need to deserialize
+               return res
+       }
+
+       // Get the element type that the pointer points to
+       replyElemType := replyValue.Elem().Type()
+
+       // Get the generalizer based on the generic serialization type
+       generic := invoker.GetURL().GetParam(constant.GenericKey, 
constant.GenericSerializationDefault)
+       g := getGeneralizer(generic)
+
+       // Realize the map/slice to the target struct using shared helper
+       realized, err := realizeResult(data, replyElemType, g)
+       if err != nil {
+               logger.Warnf("failed to deserialize generic result: %v", err)
+               return res
+       }
+
+       // Set the realized value to reply
+       replyValue.Elem().Set(reflect.ValueOf(realized))

Review Comment:
   这里,输入`realizeResult`的`data`如果是`nil`,`realizeResult`会返回nil,nil,后面又把nil
   赋值了`replyValue.Elem().Set(reflect.ValueOf(realized))`,这里下游会不会panic啊
   
   同时请补一个单测:InvokeWithType 返回 nil result / OnResponse result 为 nil 时不 panic



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to