You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.5 KiB

  1. // Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.
  2. package container
  3. import (
  4. "errors"
  5. "fmt"
  6. )
  7. // 服务组件句柄
  8. // @Implement LAPP_LF_MOM_BACKEND/container/ComponentHandler
  9. type ServiceHandler struct {
  10. // @Inherit LAPP_LF_MOM_BACKEND/container/ElementHandler
  11. ElementHandler
  12. // 方法句柄映射: map[方法名]方法句柄
  13. methodMapping map[string]*ServiceMethodHandler
  14. }
  15. // 创建服务组件句柄
  16. // 参数
  17. // 1.服务组件实例
  18. // 2.方法句柄映射: map[方法名]方法句柄
  19. // 返回值:
  20. // 1.服务组件句柄
  21. // 2.错误
  22. func NewServiceHandler(instance Instance, methodMapping map[string]*ServiceMethodHandler) (*ServiceHandler, error) {
  23. if methodMapping == nil {
  24. return nil, errors.New(fmt.Sprintf("方法句柄映射不能为空!"))
  25. }
  26. if len(methodMapping) > 0 {
  27. for methodName, methodHandler := range methodMapping {
  28. if methodName == "" {
  29. return nil, errors.New(fmt.Sprintf("方法名不能为空!"))
  30. }
  31. if methodHandler == nil {
  32. return nil, errors.New(fmt.Sprintf("方法句柄不能为空!"))
  33. }
  34. }
  35. }
  36. return &ServiceHandler{ElementHandler{instance}, methodMapping}, nil
  37. }
  38. // 创建获取方法句柄
  39. // 参数
  40. // 1.方法名
  41. // 返回值:
  42. // 1.方法句柄
  43. func (handler *ServiceHandler) Method(methodName string) *ServiceMethodHandler {
  44. if methodName == "" {
  45. return nil
  46. }
  47. serviceMethodHandler, ok := handler.methodMapping[methodName]
  48. if !ok {
  49. return nil
  50. }
  51. return serviceMethodHandler
  52. }