12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package service
- import (
- "git.bvbej.com/bvbej/base-golang/pkg/websocket/util"
- "reflect"
- "strings"
- )
- type Handler struct {
- Receiver reflect.Value // 值
- Method reflect.Method // 方法
- Type reflect.Type // 类型
- IsRawArg bool // 数据是否需要序列化
- }
- func RegisterHandler(components ...interface{}) map[string]*Handler {
- methods := make(map[string]*Handler)
- for _, component := range components {
- rt := reflect.TypeOf(component)
- rv := reflect.ValueOf(component)
- typeName := reflect.Indirect(rv).Type().Name()
- if typeName == "" {
- continue
- }
- if !util.IsExported(typeName) {
- continue
- }
- for m := 0; m < rt.NumMethod(); m++ {
- method := rt.Method(m)
- mt := method.Type
- mn := method.Name
- if isHandlerMethod(method) {
- raw := false
- if mt.In(2) == typeOfBytes {
- raw = true
- }
- mn = strings.ToLower(mn)
- methods[mn] = &Handler{Method: method, Type: mt.In(2), IsRawArg: raw}
- }
- }
- }
- return methods
- }
|