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

56 lines
1.2 KiB
Go

package component
import (
"reflect"
"unicode"
"unicode/utf8"
)
var (
typeOfError = reflect.TypeOf((*error)(nil)).Elem()
typeOfBytes = reflect.TypeOf(([]byte)(nil))
//typeOfSession = reflect.TypeOf(session.New(nil))
)
func isExported(name string) bool {
w, _ := utf8.DecodeRuneInString(name)
return unicode.IsUpper(w)
}
func isExportedOrBuiltinType(t reflect.Type) bool {
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
// PkgPath will be non-empty even for an exported type,
// so we need to check the type name as well.
return isExported(t.Name()) || t.PkgPath() == ""
}
// isHandlerMethod decide a method is suitable handler method
func isHandlerMethod(method reflect.Method) bool {
mt := method.Type
// Method must be exported.
if method.PkgPath != "" {
return false
}
// Method needs three ins: receiver, *Session, []byte or pointer.
if mt.NumIn() != 3 {
return false
}
// Method needs one outs: error
if mt.NumOut() != 1 {
return false
}
if t1 := mt.In(1); t1.Kind() != reflect.Ptr || t1 != typeOfSession {
return false
}
if (mt.In(2).Kind() != reflect.Ptr && mt.In(2) != typeOfBytes) || mt.Out(0) != typeOfError {
return false
}
return true
}