You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2026/03/16 13:02:43 UTC

(dubbo-go) branch develop updated: migrate the ValidateRegistryIDs to internal (#3258)

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

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


The following commit(s) were added to refs/heads/develop by this push:
     new 06837fd98 migrate the ValidateRegistryIDs to internal (#3258)
06837fd98 is described below

commit 06837fd9858704702c955bc0013ec7d459dda1cb
Author: nanjiek <12...@users.noreply.github.com>
AuthorDate: Mon Mar 16 21:02:36 2026 +0800

    migrate the ValidateRegistryIDs to internal (#3258)
    
    * remove the config package from package client
    
    * fix(client): fail fast on invalid registry and method config
    
    * restore WithMethod in Server
    
    * fix lint of action_test
    
    * refactor: split processURL to reduce cognitive complexity
    
    * migrate the ValidateRegistryIDs to internal
    
    * fix the cmt
    
    * remove the validateRegistryIDs
---
 client/options.go       | 14 ++------------
 client/options_test.go  | 29 +++++++++++++++++++++++++++++
 internal/config.go      |  9 +++++++++
 internal/config_test.go | 22 ++++++++++++++++++++++
 server/options.go       | 14 ++------------
 server/options_test.go  | 41 +++++++++++++++++++++++++++++++++++++++++
 6 files changed, 105 insertions(+), 24 deletions(-)

diff --git a/client/options.go b/client/options.go
index e2d6f9b3f..273deeb69 100644
--- a/client/options.go
+++ b/client/options.go
@@ -18,7 +18,6 @@
 package client
 
 import (
-	"fmt"
 	"strconv"
 	"time"
 )
@@ -120,7 +119,7 @@ func (refOpts *ReferenceOptions) init(opts ...ReferenceOption) error {
 			}
 		}
 		refConf.RegistryIDs = commonCfg.TranslateIds(refConf.RegistryIDs)
-		if err := validateRegistryIDs(refConf.RegistryIDs, regs); err != nil {
+		if err := internal.ValidateRegistryIDs(refConf.RegistryIDs, regs); err != nil {
 			return err
 		}
 	}
@@ -560,7 +559,7 @@ func (cliOpts *ClientOptions) init(opts ...ClientOption) error {
 			}
 		}
 		consumerConf.RegistryIDs = commonCfg.TranslateIds(consumerConf.RegistryIDs)
-		if err := validateRegistryIDs(consumerConf.RegistryIDs, regs); err != nil {
+		if err := internal.ValidateRegistryIDs(consumerConf.RegistryIDs, regs); err != nil {
 			return err
 		}
 	}
@@ -966,15 +965,6 @@ func newDefaultCallOptions() *CallOptions {
 	return &CallOptions{}
 }
 
-func validateRegistryIDs(ids []string, regs map[string]*global.RegistryConfig) error {
-	for _, id := range ids {
-		if _, ok := regs[id]; !ok {
-			return fmt.Errorf("registry id %q not found", id)
-		}
-	}
-	return nil
-}
-
 // WithCallRequestTimeout the maximum waiting time for one specific call, only works for 'tri' and 'dubbo' protocol
 func WithCallRequestTimeout(timeout time.Duration) CallOption {
 	return func(opts *CallOptions) {
diff --git a/client/options_test.go b/client/options_test.go
index 963d3696e..7d7141023 100644
--- a/client/options_test.go
+++ b/client/options_test.go
@@ -1273,6 +1273,23 @@ func TestClientOptionsInitFailsOnMissingRegistryID(t *testing.T) {
 	assert.Contains(t, err.Error(), `registry id "missing" not found`)
 }
 
+func TestClientOptionsInitTranslatesRegistryIDs(t *testing.T) {
+	cliOpts := &ClientOptions{
+		Consumer: &global.ConsumerConfig{
+			RegistryIDs: []string{"r1,r2", "r2"},
+		},
+		Registries: map[string]*global.RegistryConfig{
+			"r1": {Protocol: "mock", Address: "127.0.0.1:2181"},
+			"r2": {Protocol: "mock", Address: "127.0.0.2:2181"},
+		},
+		overallReference: &global.ReferenceConfig{},
+	}
+
+	err := cliOpts.init()
+	require.NoError(t, err)
+	assert.Equal(t, []string{"r1", "r2"}, cliOpts.Consumer.RegistryIDs)
+}
+
 func TestReferenceOptionsInitFailsOnMissingRegistryID(t *testing.T) {
 	refOpts := defaultReferenceOptions()
 	refOpts.Registries = map[string]*global.RegistryConfig{
@@ -1284,6 +1301,18 @@ func TestReferenceOptionsInitFailsOnMissingRegistryID(t *testing.T) {
 	assert.Contains(t, err.Error(), `registry id "missing" not found`)
 }
 
+func TestReferenceOptionsInitTranslatesRegistryIDs(t *testing.T) {
+	refOpts := defaultReferenceOptions()
+	refOpts.Registries = map[string]*global.RegistryConfig{
+		"r1": {Protocol: "mock", Address: "127.0.0.1:2181"},
+		"r2": {Protocol: "mock", Address: "127.0.0.2:2181"},
+	}
+
+	err := refOpts.init(WithRegistryIDs("r1,r2", "r2"))
+	require.NoError(t, err)
+	assert.Equal(t, []string{"r1", "r2"}, refOpts.Reference.RegistryIDs)
+}
+
 func TestReferenceOptionsInitFailsOnInvalidMethodConfig(t *testing.T) {
 	refOpts := defaultReferenceOptions()
 	err := refOpts.init(WithMethod(&global.MethodConfig{
diff --git a/internal/config.go b/internal/config.go
index 0f3d286dc..0db82681a 100644
--- a/internal/config.go
+++ b/internal/config.go
@@ -206,3 +206,12 @@ func ValidateMethodConfig(method *global.MethodConfig) error {
 
 	return nil
 }
+
+func ValidateRegistryIDs(ids []string, regs map[string]*global.RegistryConfig) error {
+	for _, id := range ids {
+		if _, ok := regs[id]; !ok {
+			return fmt.Errorf("registry id %q not found", id)
+		}
+	}
+	return nil
+}
diff --git a/internal/config_test.go b/internal/config_test.go
index 15fdcf7e8..8e631ed34 100644
--- a/internal/config_test.go
+++ b/internal/config_test.go
@@ -132,6 +132,28 @@ func TestLoadRegistries_MissingRegistryID(t *testing.T) {
 	assert.Contains(t, err.Error(), `registry id "missing" not found`)
 }
 
+func TestValidateRegistryIDs(t *testing.T) {
+	t.Run("valid ids", func(t *testing.T) {
+		regs := map[string]*global.RegistryConfig{
+			"r1": {Protocol: "mock", Address: "127.0.0.1:2181"},
+			"r2": {Protocol: "mock", Address: "127.0.0.2:2181"},
+		}
+
+		err := ValidateRegistryIDs([]string{"r1", "r2"}, regs)
+		require.NoError(t, err)
+	})
+
+	t.Run("missing id", func(t *testing.T) {
+		regs := map[string]*global.RegistryConfig{
+			"r1": {Protocol: "mock", Address: "127.0.0.1:2181"},
+		}
+
+		err := ValidateRegistryIDs([]string{"r1", "missing"}, regs)
+		require.Error(t, err)
+		assert.Contains(t, err.Error(), `registry id "missing" not found`)
+	})
+}
+
 func TestToURLs_EmptyOrNA(t *testing.T) {
 	tests := []struct {
 		name    string
diff --git a/server/options.go b/server/options.go
index 62013bf8f..e04e1af65 100644
--- a/server/options.go
+++ b/server/options.go
@@ -18,7 +18,6 @@
 package server
 
 import (
-	"fmt"
 	"reflect"
 	"strconv"
 	"sync"
@@ -89,7 +88,7 @@ func (srvOpts *ServerOptions) init(opts ...ServerOption) error {
 	if len(providerConf.RegistryIDs) <= 0 {
 		providerConf.RegistryIDs = getRegistryIds(srvOpts.Registries)
 	}
-	if err := validateRegistryIDs(providerConf.RegistryIDs, srvOpts.Registries); err != nil {
+	if err := internal.ValidateRegistryIDs(providerConf.RegistryIDs, srvOpts.Registries); err != nil {
 		return err
 	}
 
@@ -563,7 +562,7 @@ func (svcOpts *ServiceOptions) init(srv *Server, opts ...ServiceOption) error {
 	}
 	if len(svc.RegistryIDs) <= 0 {
 		svc.NotRegister = true
-	} else if err := validateRegistryIDs(svc.RegistryIDs, svc.RCRegistriesMap); err != nil {
+	} else if err := internal.ValidateRegistryIDs(svc.RegistryIDs, svc.RCRegistriesMap); err != nil {
 		return err
 	}
 
@@ -635,15 +634,6 @@ func WithFilter(filter string) ServiceOption {
 	}
 }
 
-func validateRegistryIDs(ids []string, regs map[string]*global.RegistryConfig) error {
-	for _, id := range ids {
-		if _, ok := regs[id]; !ok {
-			return fmt.Errorf("registry id %q not found", id)
-		}
-	}
-	return nil
-}
-
 // todo(DMwangnima): think about a more ideal configuration style
 func WithProtocolIDs(protocolIDs []string) ServiceOption {
 	return func(cfg *ServiceOptions) {
diff --git a/server/options_test.go b/server/options_test.go
index 97408d39f..85560cbb5 100644
--- a/server/options_test.go
+++ b/server/options_test.go
@@ -75,6 +75,19 @@ func TestServerOptionsInitFailsOnMissingRegistryID(t *testing.T) {
 	assert.Contains(t, err.Error(), `registry id "missing" not found`)
 }
 
+func TestServerOptionsInitTranslatesRegistryIDs(t *testing.T) {
+	opts := defaultServerOptions()
+	opts.Provider.RegistryIDs = []string{"r1,r2", "r2"}
+	opts.Registries = map[string]*global.RegistryConfig{
+		"r1": {Protocol: "mock", Address: "127.0.0.1:2181"},
+		"r2": {Protocol: "mock", Address: "127.0.0.2:2181"},
+	}
+
+	err := opts.init()
+	require.NoError(t, err)
+	assert.Equal(t, []string{"r1", "r2"}, opts.Provider.RegistryIDs)
+}
+
 func TestServiceOptionsInitFailsOnMissingRegistryID(t *testing.T) {
 	srv := &Server{
 		cfg: &ServerOptions{
@@ -102,6 +115,34 @@ func TestServiceOptionsInitFailsOnMissingRegistryID(t *testing.T) {
 	assert.Contains(t, err.Error(), `registry id "missing" not found`)
 }
 
+func TestServiceOptionsInitTranslatesRegistryIDs(t *testing.T) {
+	srv := &Server{
+		cfg: &ServerOptions{
+			Provider: &global.ProviderConfig{
+				ProtocolIDs: []string{"triple"},
+			},
+			Registries: map[string]*global.RegistryConfig{
+				"r1": {Protocol: "mock", Address: "127.0.0.1:2181"},
+				"r2": {Protocol: "mock", Address: "127.0.0.2:2181"},
+			},
+			Protocols: map[string]*global.ProtocolConfig{
+				"triple": {Name: "triple"},
+			},
+			Application: global.DefaultApplicationConfig(),
+		},
+	}
+	svcOpts := defaultServiceOptions()
+	svcOpts.Registries = srv.cfg.Registries
+	svcOpts.Protocols = srv.cfg.Protocols
+	svcOpts.Provider = srv.cfg.Provider
+
+	err := svcOpts.init(srv, func(opts *ServiceOptions) {
+		opts.Service.RegistryIDs = []string{"r1,r2", "r2"}
+	})
+	require.NoError(t, err)
+	assert.Equal(t, []string{"r1", "r2"}, svcOpts.Service.RegistryIDs)
+}
+
 func TestServiceOptionsInitFailsOnInvalidMethodConfig(t *testing.T) {
 	srv := &Server{
 		cfg: &ServerOptions{