示例#1
0
func TestGetFullName(ot *testing.T) {
	name := kmgReflect.GetTypeFullName(reflect.TypeOf(""))
	kmgTest.Equal(name, "string")

	name = kmgReflect.GetTypeFullName(reflect.TypeOf(1))
	kmgTest.Equal(name, "int")

	name = kmgReflect.GetTypeFullName(reflect.TypeOf(&ta{}))
	kmgTest.Equal(name, "github.com/bronze1man/kmg/kmgReflect_test.ta")

	name = kmgReflect.GetTypeFullName(reflect.TypeOf([]string{}))
	kmgTest.Equal(name, "")

}
示例#2
0
func stringTransformSubType(in reflect.Value, transformTable map[string]map[string]string) (err error) {
	switch in.Kind() {
	case reflect.String:
		typeName := kmgReflect.GetTypeFullName(in.Type())
		thisTable, exist := transformTable[typeName]
		if !exist {
			return
		}
		inS := in.String()
		if inS == "" {
			in.SetString("")
			return
		}
		oVal, exist := thisTable[inS]
		if !exist {
			return fmt.Errorf(`string transform fail! from:"%s",type:"%s"`, inS, typeName)
		}
		in.SetString(oVal)
		return
	case reflect.Ptr:
		return stringTransformSubType(in.Elem(), transformTable)
	case reflect.Slice:
		len := in.Len()
		for i := 0; i < len; i++ {
			err = stringTransformSubType(in.Index(i), transformTable)
			if err != nil {
				return
			}
		}
	case reflect.Struct:
		len := in.Type().NumField()
		for i := 0; i < len; i++ {
			err = stringTransformSubType(in.Field(i), transformTable)
			if err != nil {
				return
			}
		}
	}
	return
}
示例#3
0
文件: runner.go 项目: keysonZZZ/kmg
//注册controller,请先注册后使用,该函数不能并发调用,不能在http开始处理之后再注册(data race问题)
// 允许重复注册.
func RegisterController(obj interface{}) {
	v := reflect.ValueOf(obj)
	t := v.Type()

	objName := kmgReflect.GetTypeFullName(t)
	objName = strings.Replace(objName, "/", ".", -1)
	for i := 0; i < t.NumMethod(); i++ {
		if t.Method(i).PkgPath != "" {
			// 不是public不需要注册进来.
			continue
		}
		mv := v.Method(i)
		mvt := mv.Type()
		if mvt.AssignableTo(controllerFuncType) {
			name := objName + "." + t.Method(i).Name
			//_, ok := controllerObjMap[name]
			//if ok {
			//	panic(fmt.Errorf("[RegisterController] Repeat register controller name[%s]", name))
			//}
			controllerObjMap[name] = mv.Interface().(func(ctx *kmgHttp.Context))
		}
	}
}