You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/07/13 10:02:54 UTC

[GitHub] [dubbo] 24kpure commented on a diff in pull request #10313: refactor parameter on consumer url logic code

24kpure commented on code in PR #10313:
URL: https://github.com/apache/dubbo/pull/10313#discussion_r919890626


##########
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:
   It looks very intuitive though it  has more than 2 depth `if else`. I will like your code more if it changes more readable.



-- 
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: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org