cache.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package validator
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. "sync"
  7. "sync/atomic"
  8. )
  9. type tagType uint8
  10. const (
  11. typeDefault tagType = iota
  12. typeOmitEmpty
  13. typeIsDefault
  14. typeNoStructLevel
  15. typeStructOnly
  16. typeDive
  17. typeOr
  18. typeKeys
  19. typeEndKeys
  20. typeOmitNil
  21. )
  22. const (
  23. invalidValidation = "Invalid validation tag on field '%s'"
  24. undefinedValidation = "Undefined validation function '%s' on field '%s'"
  25. keysTagNotDefined = "'" + endKeysTag + "' tag encountered without a corresponding '" + keysTag + "' tag"
  26. )
  27. type structCache struct {
  28. lock sync.Mutex
  29. m atomic.Value // map[reflect.Type]*cStruct
  30. }
  31. func (sc *structCache) Get(key reflect.Type) (c *cStruct, found bool) {
  32. c, found = sc.m.Load().(map[reflect.Type]*cStruct)[key]
  33. return
  34. }
  35. func (sc *structCache) Set(key reflect.Type, value *cStruct) {
  36. m := sc.m.Load().(map[reflect.Type]*cStruct)
  37. nm := make(map[reflect.Type]*cStruct, len(m)+1)
  38. for k, v := range m {
  39. nm[k] = v
  40. }
  41. nm[key] = value
  42. sc.m.Store(nm)
  43. }
  44. type tagCache struct {
  45. lock sync.Mutex
  46. m atomic.Value // map[string]*cTag
  47. }
  48. func (tc *tagCache) Get(key string) (c *cTag, found bool) {
  49. c, found = tc.m.Load().(map[string]*cTag)[key]
  50. return
  51. }
  52. func (tc *tagCache) Set(key string, value *cTag) {
  53. m := tc.m.Load().(map[string]*cTag)
  54. nm := make(map[string]*cTag, len(m)+1)
  55. for k, v := range m {
  56. nm[k] = v
  57. }
  58. nm[key] = value
  59. tc.m.Store(nm)
  60. }
  61. type cStruct struct {
  62. name string
  63. fields []*cField
  64. fn StructLevelFuncCtx
  65. }
  66. type cField struct {
  67. idx int
  68. name string
  69. altName string
  70. namesEqual bool
  71. cTags *cTag
  72. }
  73. type cTag struct {
  74. tag string
  75. aliasTag string
  76. actualAliasTag string
  77. param string
  78. keys *cTag // only populated when using tag's 'keys' and 'endkeys' for map key validation
  79. next *cTag
  80. fn FuncCtx
  81. typeof tagType
  82. hasTag bool
  83. hasAlias bool
  84. hasParam bool // true if parameter used eg. eq= where the equal sign has been set
  85. isBlockEnd bool // indicates the current tag represents the last validation in the block
  86. runValidationWhenNil bool
  87. }
  88. func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStruct {
  89. v.structCache.lock.Lock()
  90. defer v.structCache.lock.Unlock() // leave as defer! because if inner panics, it will never get unlocked otherwise!
  91. typ := current.Type()
  92. // could have been multiple trying to access, but once first is done this ensures struct
  93. // isn't parsed again.
  94. cs, ok := v.structCache.Get(typ)
  95. if ok {
  96. return cs
  97. }
  98. cs = &cStruct{name: sName, fields: make([]*cField, 0), fn: v.structLevelFuncs[typ]}
  99. numFields := current.NumField()
  100. rules := v.rules[typ]
  101. var ctag *cTag
  102. var fld reflect.StructField
  103. var tag string
  104. var customName string
  105. for i := 0; i < numFields; i++ {
  106. fld = typ.Field(i)
  107. if !v.privateFieldValidation && !fld.Anonymous && len(fld.PkgPath) > 0 {
  108. continue
  109. }
  110. if rtag, ok := rules[fld.Name]; ok {
  111. tag = rtag
  112. } else {
  113. tag = fld.Tag.Get(v.tagName)
  114. }
  115. if tag == skipValidationTag {
  116. continue
  117. }
  118. customName = fld.Name
  119. if v.hasTagNameFunc {
  120. name := v.tagNameFunc(fld)
  121. if len(name) > 0 {
  122. customName = name
  123. }
  124. }
  125. // NOTE: cannot use shared tag cache, because tags may be equal, but things like alias may be different
  126. // and so only struct level caching can be used instead of combined with Field tag caching
  127. if len(tag) > 0 {
  128. ctag, _ = v.parseFieldTagsRecursive(tag, fld.Name, "", false)
  129. } else {
  130. // even if field doesn't have validations need cTag for traversing to potential inner/nested
  131. // elements of the field.
  132. ctag = new(cTag)
  133. }
  134. cs.fields = append(cs.fields, &cField{
  135. idx: i,
  136. name: fld.Name,
  137. altName: customName,
  138. cTags: ctag,
  139. namesEqual: fld.Name == customName,
  140. })
  141. }
  142. v.structCache.Set(typ, cs)
  143. return cs
  144. }
  145. func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias string, hasAlias bool) (firstCtag *cTag, current *cTag) {
  146. var t string
  147. noAlias := len(alias) == 0
  148. tags := strings.Split(tag, tagSeparator)
  149. for i := 0; i < len(tags); i++ {
  150. t = tags[i]
  151. if noAlias {
  152. alias = t
  153. }
  154. // check map for alias and process new tags, otherwise process as usual
  155. if tagsVal, found := v.aliases[t]; found {
  156. if i == 0 {
  157. firstCtag, current = v.parseFieldTagsRecursive(tagsVal, fieldName, t, true)
  158. } else {
  159. next, curr := v.parseFieldTagsRecursive(tagsVal, fieldName, t, true)
  160. current.next, current = next, curr
  161. }
  162. continue
  163. }
  164. var prevTag tagType
  165. if i == 0 {
  166. current = &cTag{aliasTag: alias, hasAlias: hasAlias, hasTag: true, typeof: typeDefault}
  167. firstCtag = current
  168. } else {
  169. prevTag = current.typeof
  170. current.next = &cTag{aliasTag: alias, hasAlias: hasAlias, hasTag: true}
  171. current = current.next
  172. }
  173. switch t {
  174. case diveTag:
  175. current.typeof = typeDive
  176. continue
  177. case keysTag:
  178. current.typeof = typeKeys
  179. if i == 0 || prevTag != typeDive {
  180. panic(fmt.Sprintf("'%s' tag must be immediately preceded by the '%s' tag", keysTag, diveTag))
  181. }
  182. current.typeof = typeKeys
  183. // need to pass along only keys tag
  184. // need to increment i to skip over the keys tags
  185. b := make([]byte, 0, 64)
  186. i++
  187. for ; i < len(tags); i++ {
  188. b = append(b, tags[i]...)
  189. b = append(b, ',')
  190. if tags[i] == endKeysTag {
  191. break
  192. }
  193. }
  194. current.keys, _ = v.parseFieldTagsRecursive(string(b[:len(b)-1]), fieldName, "", false)
  195. continue
  196. case endKeysTag:
  197. current.typeof = typeEndKeys
  198. // if there are more in tags then there was no keysTag defined
  199. // and an error should be thrown
  200. if i != len(tags)-1 {
  201. panic(keysTagNotDefined)
  202. }
  203. return
  204. case omitempty:
  205. current.typeof = typeOmitEmpty
  206. continue
  207. case omitnil:
  208. current.typeof = typeOmitNil
  209. continue
  210. case structOnlyTag:
  211. current.typeof = typeStructOnly
  212. continue
  213. case noStructLevelTag:
  214. current.typeof = typeNoStructLevel
  215. continue
  216. default:
  217. if t == isdefault {
  218. current.typeof = typeIsDefault
  219. }
  220. // if a pipe character is needed within the param you must use the utf8Pipe representation "0x7C"
  221. orVals := strings.Split(t, orSeparator)
  222. for j := 0; j < len(orVals); j++ {
  223. vals := strings.SplitN(orVals[j], tagKeySeparator, 2)
  224. if noAlias {
  225. alias = vals[0]
  226. current.aliasTag = alias
  227. } else {
  228. current.actualAliasTag = t
  229. }
  230. if j > 0 {
  231. current.next = &cTag{aliasTag: alias, actualAliasTag: current.actualAliasTag, hasAlias: hasAlias, hasTag: true}
  232. current = current.next
  233. }
  234. current.hasParam = len(vals) > 1
  235. current.tag = vals[0]
  236. if len(current.tag) == 0 {
  237. panic(strings.TrimSpace(fmt.Sprintf(invalidValidation, fieldName)))
  238. }
  239. if wrapper, ok := v.validations[current.tag]; ok {
  240. current.fn = wrapper.fn
  241. current.runValidationWhenNil = wrapper.runValidatinOnNil
  242. } else {
  243. panic(strings.TrimSpace(fmt.Sprintf(undefinedValidation, current.tag, fieldName)))
  244. }
  245. if len(orVals) > 1 {
  246. current.typeof = typeOr
  247. }
  248. if len(vals) > 1 {
  249. current.param = strings.Replace(strings.Replace(vals[1], utf8HexComma, ",", -1), utf8Pipe, "|", -1)
  250. }
  251. }
  252. current.isBlockEnd = true
  253. }
  254. }
  255. return
  256. }
  257. func (v *Validate) fetchCacheTag(tag string) *cTag {
  258. // find cached tag
  259. ctag, found := v.tagCache.Get(tag)
  260. if !found {
  261. v.tagCache.lock.Lock()
  262. defer v.tagCache.lock.Unlock()
  263. // could have been multiple trying to access, but once first is done this ensures tag
  264. // isn't parsed again.
  265. ctag, found = v.tagCache.Get(tag)
  266. if !found {
  267. ctag, _ = v.parseFieldTagsRecursive(tag, "", "", false)
  268. v.tagCache.Set(tag, ctag)
  269. }
  270. }
  271. return ctag
  272. }