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

268 lines
7.1 KiB

  1. package utils
  2. import (
  3. "errors"
  4. "reflect"
  5. "strconv"
  6. "strings"
  7. )
  8. type Rules map[string][]string
  9. type RulesMap map[string]Rules
  10. var CustomizeMap = make(map[string]Rules)
  11. //@author: [piexlmax](https://github.com/piexlmax)
  12. //@function: RegisterRule
  13. //@description: 注册自定义规则方案建议在路由初始化层即注册
  14. //@param: key string, rule Rules
  15. //@return: err error
  16. func RegisterRule(key string, rule Rules) (err error) {
  17. if CustomizeMap[key] != nil {
  18. return errors.New(key + "已注册,无法重复注册")
  19. } else {
  20. CustomizeMap[key] = rule
  21. return nil
  22. }
  23. }
  24. //@author: [piexlmax](https://github.com/piexlmax)
  25. //@function: NotEmpty
  26. //@description: 非空 不能为其对应类型的0值
  27. //@return: string
  28. func NotEmpty() string {
  29. return "notEmpty"
  30. }
  31. //@author: [piexlmax](https://github.com/piexlmax)
  32. //@function: Lt
  33. //@description: 小于入参(<) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  34. //@param: mark string
  35. //@return: string
  36. func Lt(mark string) string {
  37. return "lt=" + mark
  38. }
  39. //@author: [piexlmax](https://github.com/piexlmax)
  40. //@function: Le
  41. //@description: 小于等于入参(<=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  42. //@param: mark string
  43. //@return: string
  44. func Le(mark string) string {
  45. return "le=" + mark
  46. }
  47. //@author: [piexlmax](https://github.com/piexlmax)
  48. //@function: Eq
  49. //@description: 等于入参(==) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  50. //@param: mark string
  51. //@return: string
  52. func Eq(mark string) string {
  53. return "eq=" + mark
  54. }
  55. //@author: [piexlmax](https://github.com/piexlmax)
  56. //@function: Ne
  57. //@description: 不等于入参(!=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  58. //@param: mark string
  59. //@return: string
  60. func Ne(mark string) string {
  61. return "ne=" + mark
  62. }
  63. //@author: [piexlmax](https://github.com/piexlmax)
  64. //@function: Ge
  65. //@description: 大于等于入参(>=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  66. //@param: mark string
  67. //@return: string
  68. func Ge(mark string) string {
  69. return "ge=" + mark
  70. }
  71. //@author: [piexlmax](https://github.com/piexlmax)
  72. //@function: Gt
  73. //@description: 大于入参(>) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
  74. //@param: mark string
  75. //@return: string
  76. func Gt(mark string) string {
  77. return "gt=" + mark
  78. }
  79. //
  80. //@author: [piexlmax](https://github.com/piexlmax)
  81. //@function: Verify
  82. //@description: 校验方法
  83. //@param: st interface{}, roleMap Rules(入参实例,规则map)
  84. //@return: err error
  85. func Verify(st interface{}, roleMap Rules) (err error) {
  86. compareMap := map[string]bool{
  87. "lt": true,
  88. "le": true,
  89. "eq": true,
  90. "ne": true,
  91. "ge": true,
  92. "gt": true,
  93. }
  94. typ := reflect.TypeOf(st)
  95. val := reflect.ValueOf(st) // 获取reflect.Type类型
  96. kd := val.Kind() // 获取到st对应的类别
  97. if kd != reflect.Struct {
  98. return errors.New("expect struct")
  99. }
  100. num := val.NumField()
  101. // 遍历结构体的所有字段
  102. for i := 0; i < num; i++ {
  103. tagVal := typ.Field(i)
  104. val := val.Field(i)
  105. if len(roleMap[tagVal.Name]) > 0 {
  106. for _, v := range roleMap[tagVal.Name] {
  107. switch {
  108. case v == "notEmpty":
  109. if isBlank(val) {
  110. return errors.New(tagVal.Name + "值不能为空")
  111. }
  112. case compareMap[strings.Split(v, "=")[0]]:
  113. if !compareVerify(val, v) {
  114. return errors.New(tagVal.Name + "长度或值不在合法范围," + v)
  115. }
  116. }
  117. }
  118. }
  119. }
  120. return nil
  121. }
  122. //@author: [piexlmax](https://github.com/piexlmax)
  123. //@function: compareVerify
  124. //@description: 长度和数字的校验方法 根据类型自动校验
  125. //@param: value reflect.Value, VerifyStr string
  126. //@return: bool
  127. func compareVerify(value reflect.Value, VerifyStr string) bool {
  128. switch value.Kind() {
  129. case reflect.String, reflect.Slice, reflect.Array:
  130. return compare(value.Len(), VerifyStr)
  131. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  132. return compare(value.Uint(), VerifyStr)
  133. case reflect.Float32, reflect.Float64:
  134. return compare(value.Float(), VerifyStr)
  135. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  136. return compare(value.Int(), VerifyStr)
  137. default:
  138. return false
  139. }
  140. }
  141. //@author: [piexlmax](https://github.com/piexlmax)
  142. //@function: isBlank
  143. //@description: 非空校验
  144. //@param: value reflect.Value
  145. //@return: bool
  146. func isBlank(value reflect.Value) bool {
  147. switch value.Kind() {
  148. case reflect.String:
  149. return value.Len() == 0
  150. case reflect.Bool:
  151. return !value.Bool()
  152. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  153. return value.Int() == 0
  154. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  155. return value.Uint() == 0
  156. case reflect.Float32, reflect.Float64:
  157. return value.Float() == 0
  158. case reflect.Interface, reflect.Ptr:
  159. return value.IsNil()
  160. }
  161. return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
  162. }
  163. //@author: [piexlmax](https://github.com/piexlmax)
  164. //@function: compare
  165. //@description: 比较函数
  166. //@param: value interface{}, VerifyStr string
  167. //@return: bool
  168. func compare(value interface{}, VerifyStr string) bool {
  169. VerifyStrArr := strings.Split(VerifyStr, "=")
  170. val := reflect.ValueOf(value)
  171. switch val.Kind() {
  172. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  173. VInt, VErr := strconv.ParseInt(VerifyStrArr[1], 10, 64)
  174. if VErr != nil {
  175. return false
  176. }
  177. switch {
  178. case VerifyStrArr[0] == "lt":
  179. return val.Int() < VInt
  180. case VerifyStrArr[0] == "le":
  181. return val.Int() <= VInt
  182. case VerifyStrArr[0] == "eq":
  183. return val.Int() == VInt
  184. case VerifyStrArr[0] == "ne":
  185. return val.Int() != VInt
  186. case VerifyStrArr[0] == "ge":
  187. return val.Int() >= VInt
  188. case VerifyStrArr[0] == "gt":
  189. return val.Int() > VInt
  190. default:
  191. return false
  192. }
  193. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  194. VInt, VErr := strconv.Atoi(VerifyStrArr[1])
  195. if VErr != nil {
  196. return false
  197. }
  198. switch {
  199. case VerifyStrArr[0] == "lt":
  200. return val.Uint() < uint64(VInt)
  201. case VerifyStrArr[0] == "le":
  202. return val.Uint() <= uint64(VInt)
  203. case VerifyStrArr[0] == "eq":
  204. return val.Uint() == uint64(VInt)
  205. case VerifyStrArr[0] == "ne":
  206. return val.Uint() != uint64(VInt)
  207. case VerifyStrArr[0] == "ge":
  208. return val.Uint() >= uint64(VInt)
  209. case VerifyStrArr[0] == "gt":
  210. return val.Uint() > uint64(VInt)
  211. default:
  212. return false
  213. }
  214. case reflect.Float32, reflect.Float64:
  215. VFloat, VErr := strconv.ParseFloat(VerifyStrArr[1], 64)
  216. if VErr != nil {
  217. return false
  218. }
  219. switch {
  220. case VerifyStrArr[0] == "lt":
  221. return val.Float() < VFloat
  222. case VerifyStrArr[0] == "le":
  223. return val.Float() <= VFloat
  224. case VerifyStrArr[0] == "eq":
  225. return val.Float() == VFloat
  226. case VerifyStrArr[0] == "ne":
  227. return val.Float() != VFloat
  228. case VerifyStrArr[0] == "ge":
  229. return val.Float() >= VFloat
  230. case VerifyStrArr[0] == "gt":
  231. return val.Float() > VFloat
  232. default:
  233. return false
  234. }
  235. default:
  236. return false
  237. }
  238. }