// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.
|
|
|
|
package container
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// 服务组件句柄
|
|
// @Implement LAPP_GAAS_GFrame_BACKEND/container/ComponentHandler
|
|
type ServiceHandler struct {
|
|
// @Inherit LAPP_GAAS_GFrame_BACKEND/container/ElementHandler
|
|
ElementHandler
|
|
// 方法句柄映射: map[方法名]方法句柄
|
|
methodMapping map[string]*ServiceMethodHandler
|
|
}
|
|
|
|
// 创建服务组件句柄
|
|
// 参数
|
|
// 1.服务组件实例
|
|
// 2.方法句柄映射: map[方法名]方法句柄
|
|
// 返回值:
|
|
// 1.服务组件句柄
|
|
// 2.错误
|
|
func NewServiceHandler(instance Instance, methodMapping map[string]*ServiceMethodHandler) (*ServiceHandler, error) {
|
|
if methodMapping == nil {
|
|
return nil, errors.New(fmt.Sprintf("方法句柄映射不能为空!"))
|
|
}
|
|
if len(methodMapping) > 0 {
|
|
for methodName, methodHandler := range methodMapping {
|
|
if methodName == "" {
|
|
return nil, errors.New(fmt.Sprintf("方法名不能为空!"))
|
|
}
|
|
if methodHandler == nil {
|
|
return nil, errors.New(fmt.Sprintf("方法句柄不能为空!"))
|
|
}
|
|
}
|
|
}
|
|
return &ServiceHandler{ElementHandler{instance}, methodMapping}, nil
|
|
}
|
|
|
|
// 创建获取方法句柄
|
|
// 参数
|
|
// 1.方法名
|
|
// 返回值:
|
|
// 1.方法句柄
|
|
func (handler *ServiceHandler) Method(methodName string) *ServiceMethodHandler {
|
|
if methodName == "" {
|
|
return nil
|
|
}
|
|
serviceMethodHandler, ok := handler.methodMapping[methodName]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return serviceMethodHandler
|
|
}
|