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

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


The following commit(s) were added to refs/heads/3.0 by this push:
     new c5802c5  Use class name as the default reference name (#1339)
c5802c5 is described below

commit c5802c521a1cbb2752330d2064375622fe97f54f
Author: alchemy-lee <[email protected]>
AuthorDate: Mon Aug 9 12:14:34 2021 +0800

    Use class name as the default reference name (#1339)
    
    * build(deps): bump actions/cache from v2.1.4 to v2.1.5
    
    Bumps [actions/cache](https://github.com/actions/cache) from v2.1.4 to 
v2.1.5.
    - [Release notes](https://github.com/actions/cache/releases)
    - 
[Commits](https://github.com/actions/cache/compare/v2.1.4...1a9e2138d905efd099035b49d8b7a3888c653ca8)
    
    Signed-off-by: dependabot[bot] <[email protected]>
    
    * improve etcd version and change create to put (#1203)
    
    * Remove RPC Service
    
    * use type assertion before reflect
    
    * modify comment
    
    * modify comment of BaseMetadataService
    
    * add the type alias of interface{}
    
    * modify RPCService
    
    Co-authored-by: dependabot[bot] 
<49699333+dependabot[bot]@users.noreply.github.com>
    Co-authored-by: Xin.Zh <[email protected]>
    Co-authored-by: AlexStocks <[email protected]>
    Co-authored-by: randy <[email protected]>
---
 common/rpc_service.go             | 33 +++++++++++++++++++++++++++++++--
 common/rpc_service_test.go        | 33 +++++++++++++++++++++++++++++++++
 config/config_loader.go           |  3 ++-
 config/service.go                 |  6 ++++--
 metadata/service/local_service.go |  4 ++--
 5 files changed, 72 insertions(+), 7 deletions(-)

diff --git a/common/rpc_service.go b/common/rpc_service.go
index 224f8c8..1a7cbc8 100644
--- a/common/rpc_service.go
+++ b/common/rpc_service.go
@@ -34,14 +34,43 @@ import (
        "dubbo.apache.org/dubbo-go/v3/common/logger"
 )
 
-// RPCService
+// RPCService the type alias of interface{}
+type RPCService = interface{}
+
+// ReferencedRPCService
 // rpc service interface
-type RPCService interface {
+type ReferencedRPCService interface {
        // Reference:
        // rpc service id or reference id
        Reference() string
 }
 
+// GetReference return the reference id of the service.
+// If the service implemented the ReferencedRPCService interface,
+// it will call the Reference method. If not, it will
+// return the struct name as the reference id.
+func GetReference(service RPCService) string {
+       if s, ok := service.(ReferencedRPCService); ok {
+               return s.Reference()
+       }
+
+       ref := ""
+       sType := reflect.TypeOf(service)
+       kind := sType.Kind()
+       switch kind {
+       case reflect.Struct:
+               ref = sType.Name()
+       case reflect.Ptr:
+               sName := sType.Elem().Name()
+               if sName != "" {
+                       ref = sName
+               } else {
+                       ref = sType.Elem().Field(0).Name
+               }
+       }
+       return ref
+}
+
 // AsyncCallbackService callback interface for async
 type AsyncCallbackService interface {
        // Callback: callback
diff --git a/common/rpc_service_test.go b/common/rpc_service_test.go
index 8dc984f..6143e47 100644
--- a/common/rpc_service_test.go
+++ b/common/rpc_service_test.go
@@ -218,3 +218,36 @@ func TestSuiteMethod(t *testing.T) {
        methodType = suiteMethod(method)
        assert.Nil(t, methodType)
 }
+
+type ServiceWithoutRef struct{}
+
+func TestGetReference(t *testing.T) {
+       s0 := &TestService{}
+       ref0 := GetReference(s0)
+       assert.Equal(t, referenceTestPath, ref0)
+
+       //s1 := TestService{}
+       //ref1 := GetReference(s1)
+       //assert.Equal(t, referenceTestPath, ref1)
+
+       s2 := &struct {
+               TestService
+       }{}
+       ref2 := GetReference(s2)
+       assert.Equal(t, referenceTestPath, ref2)
+
+       expectedReference := "ServiceWithoutRef"
+       s3 := &ServiceWithoutRef{}
+       ref3 := GetReference(s3)
+       assert.Equal(t, expectedReference, ref3)
+
+       s4 := ServiceWithoutRef{}
+       ref4 := GetReference(s4)
+       assert.Equal(t, expectedReference, ref4)
+
+       s5 := &struct {
+               ServiceWithoutRef
+       }{}
+       ref5 := GetReference(s5)
+       assert.Equal(t, expectedReference, ref5)
+}
diff --git a/config/config_loader.go b/config/config_loader.go
index 039d8fc..a87abf4 100644
--- a/config/config_loader.go
+++ b/config/config_loader.go
@@ -401,7 +401,8 @@ func GetRPCService(name string) common.RPCService {
 
 // RPCService create rpc service for consumer
 func RPCService(service common.RPCService) {
-       consumerConfig.References[service.Reference()].Implement(service)
+       ref := common.GetReference(service)
+       consumerConfig.References[ref].Implement(service)
 }
 
 // GetMetricConfig find the MetricConfig
diff --git a/config/service.go b/config/service.go
index 12cc91e..a487aba 100644
--- a/config/service.go
+++ b/config/service.go
@@ -28,12 +28,14 @@ var (
 
 // SetConsumerService is called by init() of implement of RPCService
 func SetConsumerService(service common.RPCService) {
-       conServices[service.Reference()] = service
+       ref := common.GetReference(service)
+       conServices[ref] = service
 }
 
 // SetProviderService is called by init() of implement of RPCService
 func SetProviderService(service common.RPCService) {
-       proServices[service.Reference()] = service
+       ref := common.GetReference(service)
+       proServices[ref] = service
 }
 
 // GetConsumerService gets ConsumerService by @name
diff --git a/metadata/service/local_service.go 
b/metadata/service/local_service.go
index 135dae0..1230e3a 100644
--- a/metadata/service/local_service.go
+++ b/metadata/service/local_service.go
@@ -30,7 +30,7 @@ import (
 // MetadataService is used to define meta data related behaviors
 // usually the implementation should be singleton
 type MetadataService interface {
-       common.RPCService
+       common.ReferencedRPCService
        // ServiceName will get the service's name in meta service , which is 
application name
        ServiceName() (string, error)
        // ExportURL will store the exported url in metadata
@@ -94,7 +94,7 @@ func (mts *BaseMetadataService) ServiceName() (string, error) 
{
        return mts.serviceName, nil
 }
 
-// Version will return the version of metadata service
+// Reference will return the reference id of metadata service
 func (mts *BaseMetadataService) Reference() string {
        return constant.SIMPLE_METADATA_SERVICE_NAME
 }

Reply via email to