cheese8 commented on code in PR #10313:
URL: https://github.com/apache/dubbo/pull/10313#discussion_r922680032


##########
dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/InstanceAddressURL.java:
##########
@@ -343,37 +312,91 @@ public boolean hasMethodParameter(String method, String 
key) {
      * @return
      */
     @Override
-    public boolean hasServiceMethodParameter(String protocolServiceKey, String 
method) {
-        URL consumerUrl = RpcContext.getServiceContext().getConsumerUrl();
-        if (consumerUrl != null) {
-            if (consumerUrl.hasServiceMethodParameter(protocolServiceKey, 
method)) {
-                return true;
-            }
+    public boolean hasServiceMethodParameter(final String protocolServiceKey, 
final String method) {
+        if 
(hasParameterOnConsumerUrl(ParameterOnConsumerUrl.ServiceMethodParameter, 
protocolServiceKey, method, null, false)) {
+            return true;
         }
-
         MetadataInfo.ServiceInfo serviceInfo = 
getServiceInfo(protocolServiceKey);
         if (null == serviceInfo) {
             return false;
         }
-
         return serviceInfo.hasMethodParameter(method);
     }
 
     @Override
-    public boolean hasMethodParameter(String method) {
-        URL consumerUrl = RpcContext.getServiceContext().getConsumerUrl();
-        if (consumerUrl != null) {
-            if (consumerUrl.hasMethodParameter(method)) {
-                return true;
-            }
+    public boolean hasMethodParameter(final String method) {
+        if (hasParameterOnConsumerUrl(ParameterOnConsumerUrl.MethodParameter, 
null, method, null, false)) {
+            return true;
         }
-
         String protocolServiceKey = getProtocolServiceKey();
         if (isEmpty(protocolServiceKey)) {
             return false;
         }
         return hasServiceMethodParameter(protocolServiceKey, method);
     }
+    
+    private String getParameterOnConsumerUrl(final ParameterOnConsumerUrl 
parameterOnConsumerUrl, final URL consumerUrl,
+                                             final String service, final 
String method, final String key) {
+        switch(parameterOnConsumerUrl) {
+            case Parameter:
+                return consumerUrl.getParameter(key);
+            case ServiceParameter:
+                return consumerUrl.getServiceParameter(service, key);
+            case ServiceMethodParameter:
+                return consumerUrl.getServiceMethodParameter(service, method, 
key);
+            case MethodParameter:
+                return consumerUrl.getMethodParameter(method, key);
+            default:
+                return null;
+        }
+    }
+    
+    private Optional<String> getParameterOnConsumerUrl(final 
ParameterOnConsumerUrl parameterOnConsumerUrl, final String service,
+                                                       final String method, 
final String key) {
+        if (!consumerParamFirst(key)) {
+            return Optional.empty();
+        }
+        URL consumerUrl = RpcContext.getServiceContext().getConsumerUrl();
+        if (consumerUrl == null) {
+            return Optional.empty();
+        }
+        String value = getParameterOnConsumerUrl(parameterOnConsumerUrl, 
consumerUrl, service, method, key);
+        if (StringUtils.isEmpty(value)) {
+            return Optional.empty();
+        }
+        return Optional.ofNullable(value);
+    }
+    
+    private Boolean hasParameterOnConsumerUrl(final ParameterOnConsumerUrl 
parameterOnConsumerUrl, final String service,
+                                              final String method, final 
String key, final Boolean checkConsumerParamFirst) {
+        if (checkConsumerParamFirst && !consumerParamFirst(key)) {
+            return false;
+        }
+        URL consumerUrl = RpcContext.getServiceContext().getConsumerUrl();
+        if (consumerUrl == null) {
+            return false;
+        }
+        return hasParameterOnConsumerUrl(parameterOnConsumerUrl, consumerUrl, 
service, method, key);
+    }
+    
+    private Boolean hasParameterOnConsumerUrl(final ParameterOnConsumerUrl 
parameterOnConsumerUrl, final URL consumerUrl,
+                                              final String service, final 
String method, final String key) {
+        switch(parameterOnConsumerUrl) {
+            case ServiceMethodParameter:
+                return consumerUrl.hasServiceMethodParameter(service, method, 
key);
+            case MethodParameter:
+                return consumerUrl.hasMethodParameter(method, key);
+            default:
+                return false;
+        }
+    }
+    
+    enum ParameterOnConsumerUrl {
+        Parameter,
+        ServiceParameter,
+        ServiceMethodParameter,
+        MethodParameter
+    }

Review Comment:
   This way? As the code in `using Function to refactor the switch`. 



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