receiver.go 990 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package service
  2. import (
  3. "git.bvbej.com/bvbej/base-golang/pkg/websocket/util"
  4. "reflect"
  5. "strings"
  6. )
  7. type Handler struct {
  8. Receiver reflect.Value // 值
  9. Method reflect.Method // 方法
  10. Type reflect.Type // 类型
  11. IsRawArg bool // 数据是否需要序列化
  12. }
  13. func RegisterHandler(components ...interface{}) map[string]*Handler {
  14. methods := make(map[string]*Handler)
  15. for _, component := range components {
  16. rt := reflect.TypeOf(component)
  17. rv := reflect.ValueOf(component)
  18. typeName := reflect.Indirect(rv).Type().Name()
  19. if typeName == "" {
  20. continue
  21. }
  22. if !util.IsExported(typeName) {
  23. continue
  24. }
  25. for m := 0; m < rt.NumMethod(); m++ {
  26. method := rt.Method(m)
  27. mt := method.Type
  28. mn := method.Name
  29. if isHandlerMethod(method) {
  30. raw := false
  31. if mt.In(2) == typeOfBytes {
  32. raw = true
  33. }
  34. mn = strings.ToLower(mn)
  35. methods[mn] = &Handler{Method: method, Type: mt.In(2), IsRawArg: raw}
  36. }
  37. }
  38. }
  39. return methods
  40. }