You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by la...@apache.org on 2021/10/02 08:01:26 UTC

[dubbo-go] branch 1.5 updated: Ftr: compatible with lower versions registering "default." keys (#1444)

This is an automated email from the ASF dual-hosted git repository.

laurence pushed a commit to branch 1.5
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/1.5 by this push:
     new 874f43d  Ftr: compatible with lower versions registering "default." keys (#1444)
874f43d is described below

commit 874f43d7832336a51bb151995b0e5dd24f7e6956
Author: ChangedenChan <ch...@gmail.com>
AuthorDate: Sat Oct 2 16:01:20 2021 +0800

    Ftr: compatible with lower versions registering "default." keys (#1444)
    
    * 兼容Java低版本的"default."前缀风格
    
    * 调试集成测试
    
    * 调试集成测试
    
    * 回滚代码
    
    * 调整格式
    
    * 优化代码
    
    Co-authored-by: Changeden <ch...@unizone.tech>
---
 common/constant/key.go               |  3 ++-
 common/url.go                        | 10 ++++++++++
 common/url_test.go                   | 10 ++++++++++
 protocol/invocation/rpcinvocation.go |  7 +++++++
 4 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/common/constant/key.go b/common/constant/key.go
index 917a74b..731fcd1 100644
--- a/common/constant/key.go
+++ b/common/constant/key.go
@@ -49,8 +49,9 @@ const (
 	PORT_KEY                 = "port"
 	PROTOCOL_KEY             = "protocol"
 	PATH_SEPARATOR           = "/"
+	SSL_ENABLED_KEY          = "ssl-enabled"
+	DEFAULT_KEY_PREFIX       = "default."
 	//DUBBO_KEY                = "dubbo"
-	SSL_ENABLED_KEY = "ssl-enabled"
 )
 
 const (
diff --git a/common/url.go b/common/url.go
index cdbf20b..18d0a1e 100644
--- a/common/url.go
+++ b/common/url.go
@@ -260,6 +260,16 @@ func NewURL(urlString string, opts ...Option) (*URL, error) {
 		return &s, perrors.Errorf("url.ParseQuery(raw url string{%s}),  error{%v}", serviceUrl.RawQuery, err)
 	}
 
+	// compatible "default." prefix
+	for key, value := range s.params {
+		if strings.HasPrefix(key, constant.DEFAULT_KEY_PREFIX) && len(value) > 0 {
+			key = key[len(constant.DEFAULT_KEY_PREFIX):]
+			if s.params.Get(key) == "" {
+				s.params.Set(key, value[0])
+			}
+		}
+	}
+
 	s.PrimitiveURL = urlString
 	s.Protocol = serviceUrl.Scheme
 	s.Username = serviceUrl.User.Username()
diff --git a/common/url_test.go b/common/url_test.go
index c645f1a..a062150 100644
--- a/common/url_test.go
+++ b/common/url_test.go
@@ -421,3 +421,13 @@ func TestCompareURLEqualFunc(t *testing.T) {
 func CustomCompareURLEqual(l *URL, r *URL, execludeParam ...string) bool {
 	return l.PrimitiveURL == r.PrimitiveURL
 }
+
+func TestURLWithLowerVersion(t *testing.T) {
+	url1, _ := NewURL("dubbo://127.0.0.1:20880/com.xxx.XxxService?default.version=1.0.0&default.group=test")
+	url2, _ := NewURL("dubbo://127.0.0.1:20880/com.xxx.XxxService?version=1.0.0&group=test")
+	url3, _ := NewURL("dubbo://127.0.0.1:20880/com.xxx.XxxService?default.version=1.0.1&default.group=test1&version=1.0.0&group=test")
+	assert.Equal(t, url1.GetParam(constant.VERSION_KEY, ""), "1.0.0")
+	assert.Equal(t, url1.GetParam(constant.GROUP_KEY, ""), "test")
+	assert.True(t, url1.URLEqual(url2))
+	assert.True(t, url2.URLEqual(url3))
+}
diff --git a/protocol/invocation/rpcinvocation.go b/protocol/invocation/rpcinvocation.go
index 0f879c0..8edba62 100644
--- a/protocol/invocation/rpcinvocation.go
+++ b/protocol/invocation/rpcinvocation.go
@@ -165,6 +165,13 @@ func (r *RPCInvocation) SetAttachments(key string, value interface{}) {
 	if r.attachments == nil {
 		r.attachments = make(map[string]interface{})
 	}
+	// compatible "default." prefix
+	if strings.HasPrefix(key, constant.DEFAULT_KEY_PREFIX) {
+		key = key[len(constant.DEFAULT_KEY_PREFIX):]
+		if _, ok := r.attachments[key]; ok { // ignore common key when key existed
+			return
+		}
+	}
 	r.attachments[key] = value
 }