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.

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