示例#1
0
文件: main.go 项目: mabetle/mcore
func demoConverse() {
	a := "0"
	n, err := mcore.StrToInt(a)
	fmt.Printf("%d,%v\n", n, err)
	b := mcore.ReadInt("input int")
	fmt.Printf("%v\n", b)
}
示例#2
0
文件: valid.go 项目: mabetle/mcore
// ValidateField
func ValidateField(v interface{}, fn string) (err string) {
	tv := GetValidatorTag(v, fn)
	logger.Debugf("Validate Tag: %s", tv)
	if tv == "" {
		// field no tag, skip
		return ""
	}
	vds := strings.Split(tv, ",")
	fv := mcore.GetFieldValue(v, fn)
	logger.Debugf("Field  Value: %s", fv)

	for _, item := range vds {
		kv := strings.Split(item, "=")
		v_key := strings.TrimSpace(kv[0])
		v_key = strings.ToLower(v_key)
		v_value := ""
		if len(kv) > 1 {
			v_value = kv[1]
			v_value = strings.TrimSpace(v_value)
			v_value = strings.TrimLeft(v_value, "'")
			v_value = strings.TrimRight(v_value, "'")
		}
		switch v_key {
		case V_REQUIRED:
			if fv == "" {
				err = err + "null or blank."
			}
		case V_MIN:
			i_v_value, err1 := mcore.StrToInt(v_value)
			if err1 != nil {
				// parse int error, skip
				continue
			}
			i_f_value, err2 := mcore.StrToInt(fv)
			if err2 != nil {
				// field value parse int error, skip
				continue
			}
			if i_f_value < i_v_value {
				err = err + fmt.Sprintf("%s less than %s.", fv, v_value)
			}
		case V_MAX:
			i_v_value, err1 := mcore.StrToInt(v_value)
			if err1 != nil {
				// parse int error, skip
				continue
			}
			i_f_value, err2 := mcore.StrToInt(fv)
			if err2 != nil {
				// field value parse int error, skip
				continue
			}
			if i_f_value > i_v_value {
				err = err + fmt.Sprintf("%s great than %s.", fv, v_value)
			}
		case V_MAX_LENGTH:
			length, err1 := mcore.StrToInt(v_value)
			if err1 != nil {
				// parse int error, skip
				continue
			}
			if len(fv) > length {
				err = err + fmt.Sprintf("%s length great than %s.", fv, v_value)
			}
		case V_MIN_LENGTH:
			length, err1 := mcore.StrToInt(v_value)
			if err1 != nil {
				// parse int error, skip
				continue
			}
			if len(fv) < length {
				err = err + fmt.Sprintf("%s length less than %s.", fv, v_value)
			}
		case V_LENGTH:
			length, err1 := mcore.StrToInt(v_value)
			if err1 != nil {
				// parse int error, skip
				continue
			}
			if len(fv) != length {
				err = err + fmt.Sprintf("%s length not equals %s.", fv, v_value)
			}
		case V_EMAIL:
			if !mcore.NewString(fv).IsEmail() {
				err = err + fmt.Sprintf("%s not a Email.", fv)
			}
		case V_IDCARD:
			if !mcore.NewString(fv).IsIdCardNo() {
				err = err + fmt.Sprintf("%s not a ID Card number.", fv)
			}
		case V_NUMBER:
			if !mcore.NewString(fv).IsNumber() {
				err = err + fmt.Sprintf("%s not a number.", fv)
			}
		case V_PHONE:
			if !mcore.NewString(fv).IsPhoneNumber() {
				err = err + fmt.Sprintf("%s not a phone number.", fv)
			}
		case V_REGEX:
			if !mcore.NewString(fv).IsMatchString(v_value) {
				err = err + fmt.Sprintf("%s not match %s.", fv, v_value)
			}
		default:
			// unknow validator tag.
			logger.Warnf("Unknown validate tag: %s ", item)
		}
	}
	logger.Debugf("Validate Result: %s", err)
	return
}