AlexStocks commented on code in PR #3154:
URL: https://github.com/apache/dubbo-go/pull/3154#discussion_r2693354222


##########
server/server.go:
##########
@@ -176,6 +179,61 @@ func (s *Server) genSvcOpts(handler any, info 
*common.ServiceInfo, opts ...Servi
        return newSvcOpts, nil
 }
 
+// isNillable checks if a reflect.Value's kind supports nil checking.
+func isNillable(v reflect.Value) bool {
+       switch v.Kind() {
+       case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, 
reflect.Pointer, reflect.Slice:
+               return true
+       default:
+               return false
+       }
+}
+
+// isReflectNil safely checks if a reflect.Value is nil.
+func isReflectNil(v reflect.Value) bool {
+       return isNillable(v) && v.IsNil()
+}
+
+// CallMethodByReflection invokes the given method via reflection and 
processes its return values.
+// This is a shared helper function used by both server/server.go and 
protocol/triple/server.go.
+func CallMethodByReflection(ctx context.Context, method reflect.Method, 
handler any, args []any) (any, error) {
+       in := []reflect.Value{reflect.ValueOf(handler)}
+       in = append(in, reflect.ValueOf(ctx))
+       for _, arg := range args {
+               in = append(in, reflect.ValueOf(arg))
+       }
+       returnValues := method.Func.Call(in)
+
+       // Process return values
+       if len(returnValues) == 1 {
+               if isReflectNil(returnValues[0]) {
+                       return nil, nil
+               }
+               if err, ok := returnValues[0].Interface().(error); ok {
+                       return nil, err
+               }
+               return nil, nil
+       }
+       var result any
+       var err error
+       if !isReflectNil(returnValues[0]) {
+               result = returnValues[0].Interface()
+       }
+       if len(returnValues) > 1 && !isReflectNil(returnValues[1]) {
+               if e, ok := returnValues[1].Interface().(error); ok {
+                       err = e
+               }
+       }
+       return result, err
+}
+
+// createReflectionMethodFunc creates a MethodFunc that calls the given method 
via reflection.
+func createReflectionMethodFunc(method reflect.Method) func(ctx 
context.Context, args []any, handler any) (any, error) {
+       return func(ctx context.Context, args []any, handler any) (any, error) {
+               return CallMethodByReflection(ctx, method, handler, args)
+       }
+}
+
 // Add a method with a name of a different first-letter case
 // to achieve interoperability with java
 // TODO: The method name case sensitivity in Dubbo-java should be addressed.

Review Comment:
   建个issue,记下这个 todo 吧



-- 
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