Skip to content

Commit

Permalink
Use class name as the default reference name (#1339)
Browse files Browse the repository at this point in the history
* 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](actions/cache@v2.1.4...1a9e213)

Signed-off-by: dependabot[bot] <support@github.com>

* 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 <dragoncharlie@foxmail.com>
Co-authored-by: AlexStocks <alexstocks@foxmail.com>
Co-authored-by: randy <ztelur@gmail.com>
  • Loading branch information
5 people committed Aug 9, 2021
1 parent 0376b53 commit c5802c5
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
33 changes: 31 additions & 2 deletions common/rpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions common/rpc_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions config/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions metadata/service/local_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit c5802c5

Please sign in to comment.