Ejemplo n.º 1
0
func main() {
	m := c.Coscms{}
	v := reflect.ValueOf(m)
	t := v.Type()
	for i := 0; i < t.NumField(); i++ {
		f := t.Field(i)
		widget := tagfast.Value(t, f, "form_widget")
		fmt.Println("widget:", widget)

		valid := tagfast.Value(t, f, "valid")
		fmt.Println("valid:", valid)

		xorm := tagfast.Value(t, f, "xorm")
		fmt.Println("xorm:", xorm)

	}
	fmt.Printf("%v", tagfast.Caches())
}
Ejemplo n.º 2
0
func (v *Validation) validExec(obj interface{}, baseName string, args ...string) (err error) {
	objT := reflect.TypeOf(obj)
	objV := reflect.ValueOf(obj)
	switch {
	case isStruct(objT):
	case isStructPtr(objT):
		objT = objT.Elem()
		objV = objV.Elem()
	default:
		err = fmt.Errorf("%v must be a struct or a struct pointer", obj)
		return
	}
	var chkFields map[string][]string = make(map[string][]string)
	var pNum int = len(args)
	//fmt.Println(objT.Name(), ":[Struct NumIn]", pNum)
	if pNum > 0 {
		//aa.b.c,ab.b.c
		for _, v := range args {
			arr := strings.SplitN(v, ".", 2)
			if _, ok := chkFields[arr[0]]; !ok {
				chkFields[arr[0]] = make([]string, 0)
			}
			if len(arr) > 1 {
				chkFields[arr[0]] = append(chkFields[arr[0]], arr[1])
			}
		}
	}
	args = make([]string, 0)
	if len(chkFields) > 0 { //检测指定字段
		for field, args := range chkFields {
			f, ok := objT.FieldByName(field)
			if !ok {
				err = fmt.Errorf("No name for the '%s' field", field)
				return
			}
			tag := tagfast.Value(objT, f, VALIDTAG)
			if tag == "-" {
				continue
			}
			var vfs []ValidFunc

			var fName string
			if baseName == "" {
				fName = f.Name
			} else {
				fName = strings.Join([]string{baseName, f.Name}, ".")
			}
			fv := objV.FieldByName(field)
			if isStruct(f.Type) || isStructPtr(f.Type) {
				if fv.CanInterface() {
					err = v.validExec(fv.Interface(), fName, args...)
				}
				continue
			}
			if vfs, err = getValidFuncs(f, objT, fName); err != nil {
				return
			}
			for _, vf := range vfs {
				if _, err = funcs.Call(vf.Name,
					mergeParam(v, fv.Interface(), vf.Params)...); err != nil {
					return
				}
			}
		}
	} else { //检测全部字段
		for i := 0; i < objT.NumField(); i++ {
			tag := tagfast.Value(objT, objT.Field(i), VALIDTAG)
			if tag == "-" {
				continue
			}
			var vfs []ValidFunc

			var fName string
			if baseName == "" {
				fName = objT.Field(i).Name
			} else {
				fName = strings.Join([]string{baseName, objT.Field(i).Name}, ".")
			}
			//fmt.Println(fName, ":[Type]:", objT.Field(i).Type.Kind())
			if isStruct(objT.Field(i).Type) || isStructPtr(objT.Field(i).Type) {
				if objV.Field(i).CanInterface() {
					err = v.validExec(objV.Field(i).Interface(), fName)
				}
				continue
			}
			if vfs, err = getValidFuncs(objT.Field(i), objT, fName); err != nil {
				return
			}
			for _, vf := range vfs {
				if _, err = funcs.Call(vf.Name,
					mergeParam(v, objV.Field(i).Interface(), vf.Params)...); err != nil {
					return
				}
			}
		}
	}
	return
}
Ejemplo n.º 3
0
func TagVal(t reflect.Type, fieldNo int, tagName string) string {
	return tagfast.Value(t, t.Field(fieldNo), tagName)
}