cache.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // Copyright 2012 The Gorilla Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package schema
  5. import (
  6. "errors"
  7. "reflect"
  8. "strings"
  9. "sync"
  10. utils "github.com/gofiber/utils/v2"
  11. )
  12. const maxParserIndex = 1000
  13. var (
  14. errInvalidPath = errors.New("schema: invalid path")
  15. errIndexTooLarge = errors.New("schema: index exceeds parser limit")
  16. )
  17. // newCache returns a new cache.
  18. func newCache() *cache {
  19. c := cache{
  20. m: make(map[reflect.Type]*structInfo),
  21. regconv: make(map[reflect.Type]Converter),
  22. tag: "schema",
  23. }
  24. return &c
  25. }
  26. // cache caches meta-data about a struct.
  27. type cache struct {
  28. l sync.RWMutex
  29. m map[reflect.Type]*structInfo
  30. regconv map[reflect.Type]Converter
  31. tag string
  32. }
  33. // registerConverter registers a converter function for a custom type.
  34. func (c *cache) registerConverter(value interface{}, converterFunc Converter) {
  35. c.regconv[reflect.TypeOf(value)] = converterFunc
  36. }
  37. // parsePath parses a path in dotted notation verifying that it is a valid
  38. // path to a struct field.
  39. //
  40. // It returns "path parts" which contain indices to fields to be used by
  41. // reflect.Value.FieldByString(). Multiple parts are required for slices of
  42. // structs.
  43. func (c *cache) parsePath(p string, t reflect.Type) ([]pathPart, error) {
  44. var struc *structInfo
  45. var field *fieldInfo
  46. var index64 int64
  47. var parts []pathPart
  48. var path []string
  49. for keyStart := 0; ; {
  50. if t.Kind() != reflect.Struct {
  51. return nil, errInvalidPath
  52. }
  53. if struc = c.get(t); struc == nil {
  54. return nil, errInvalidPath
  55. }
  56. keyEnd, segment, err := nextPathSegment(p, keyStart)
  57. if err != nil {
  58. return nil, errInvalidPath
  59. }
  60. if field = struc.get(segment); field == nil {
  61. return nil, errInvalidPath
  62. }
  63. // Valid field. Append index.
  64. path = append(path, field.name)
  65. if field.isSliceOfStructs && !isMultipartField(field.typ) && (!field.unmarshalerInfo.IsValid || (field.unmarshalerInfo.IsValid && field.unmarshalerInfo.IsSliceElement)) {
  66. // Parse a special case: slices of structs.
  67. // i+1 must be the slice index.
  68. //
  69. // Now that struct can implements TextUnmarshaler interface,
  70. // we don't need to force the struct's fields to appear in the path.
  71. // So checking i+2 is not necessary anymore.
  72. // We can skip this part if the type is multipart.FileHeader. It is another special case too.
  73. keyStart = keyEnd + 1
  74. if keyStart >= len(p) {
  75. return nil, errInvalidPath
  76. }
  77. keyEnd, segment, err = nextPathSegment(p, keyStart)
  78. if err != nil {
  79. return nil, errInvalidPath
  80. }
  81. if index64, err = utils.ParseInt(segment); err != nil {
  82. return nil, errInvalidPath
  83. }
  84. if index64 > maxParserIndex {
  85. return nil, errIndexTooLarge
  86. }
  87. parts = append(parts, pathPart{
  88. path: path,
  89. field: field,
  90. index: int(index64),
  91. })
  92. path = nil
  93. // Get the next struct type, dropping ptrs.
  94. if field.typ.Kind() == reflect.Ptr {
  95. t = field.typ.Elem()
  96. } else {
  97. t = field.typ
  98. }
  99. if t.Kind() == reflect.Slice {
  100. t = t.Elem()
  101. if t.Kind() == reflect.Ptr {
  102. t = t.Elem()
  103. }
  104. }
  105. } else if field.typ.Kind() == reflect.Ptr {
  106. t = field.typ.Elem()
  107. } else {
  108. t = field.typ
  109. }
  110. if keyEnd == len(p) {
  111. break
  112. }
  113. keyStart = keyEnd + 1
  114. if keyStart >= len(p) {
  115. return nil, errInvalidPath
  116. }
  117. }
  118. // Add the remaining.
  119. parts = append(parts, pathPart{
  120. path: path,
  121. field: field,
  122. index: -1,
  123. })
  124. return parts, nil
  125. }
  126. func nextPathSegment(path string, start int) (int, string, error) {
  127. end := start
  128. for end < len(path) && path[end] != '.' {
  129. end++
  130. }
  131. if start == end {
  132. return 0, "", errInvalidPath
  133. }
  134. return end, path[start:end], nil
  135. }
  136. // get returns a cached structInfo, creating it if necessary.
  137. func (c *cache) get(t reflect.Type) *structInfo {
  138. c.l.RLock()
  139. info := c.m[t]
  140. c.l.RUnlock()
  141. if info == nil {
  142. info = c.create(t, "")
  143. c.l.Lock()
  144. c.m[t] = info
  145. c.l.Unlock()
  146. }
  147. return info
  148. }
  149. // create creates a structInfo with meta-data about a struct.
  150. func (c *cache) create(t reflect.Type, parentAlias string) *structInfo {
  151. info := &structInfo{}
  152. var anonymousInfos []*structInfo
  153. for i := 0; i < t.NumField(); i++ {
  154. structField := t.Field(i)
  155. if structField.Anonymous && structField.Type.Kind() == reflect.Ptr {
  156. info.anonymousPtrFields = append(info.anonymousPtrFields, i)
  157. }
  158. if f := c.createField(structField, parentAlias); f != nil {
  159. info.fields = append(info.fields, f)
  160. if ft := indirectType(f.typ); ft.Kind() == reflect.Struct && f.isAnonymous {
  161. anonymousInfos = append(anonymousInfos, c.create(ft, f.canonicalAlias))
  162. }
  163. }
  164. }
  165. for i, a := range anonymousInfos {
  166. others := []*structInfo{info}
  167. others = append(others, anonymousInfos[:i]...)
  168. others = append(others, anonymousInfos[i+1:]...)
  169. for _, f := range a.fields {
  170. if !containsAlias(others, f.alias) {
  171. info.fields = append(info.fields, f)
  172. }
  173. }
  174. }
  175. info.fieldsByName = make(map[string]*fieldInfo, len(info.fields))
  176. for _, field := range info.fields {
  177. aliasKey := utils.ToLower(field.alias)
  178. if _, exists := info.fieldsByName[aliasKey]; !exists {
  179. info.fieldsByName[aliasKey] = field
  180. }
  181. }
  182. return info
  183. }
  184. // createField creates a fieldInfo for the given field.
  185. func (c *cache) createField(field reflect.StructField, parentAlias string) *fieldInfo {
  186. alias, options := fieldAlias(field, c.tag)
  187. if alias == "-" {
  188. // Ignore this field.
  189. return nil
  190. }
  191. canonicalAlias := alias
  192. if parentAlias != "" {
  193. canonicalAlias = parentAlias + "." + alias
  194. }
  195. // Check if the type is supported and don't cache it if not.
  196. // First let's get the basic type.
  197. isSlice, isStruct := false, false
  198. ft := field.Type
  199. m := isTextUnmarshaler(reflect.Zero(ft))
  200. if ft.Kind() == reflect.Ptr {
  201. ft = ft.Elem()
  202. }
  203. if isSlice = ft.Kind() == reflect.Slice; isSlice {
  204. ft = ft.Elem()
  205. if ft.Kind() == reflect.Ptr {
  206. ft = ft.Elem()
  207. }
  208. }
  209. if ft.Kind() == reflect.Array {
  210. ft = ft.Elem()
  211. if ft.Kind() == reflect.Ptr {
  212. ft = ft.Elem()
  213. }
  214. }
  215. if isStruct = ft.Kind() == reflect.Struct; !isStruct {
  216. if c.converter(ft) == nil && builtinConverters[ft.Kind()] == nil {
  217. // Type is not supported.
  218. return nil
  219. }
  220. }
  221. return &fieldInfo{
  222. typ: field.Type,
  223. name: field.Name,
  224. alias: alias,
  225. canonicalAlias: canonicalAlias,
  226. unmarshalerInfo: m,
  227. isSliceOfStructs: isSlice && isStruct,
  228. isAnonymous: field.Anonymous,
  229. isRequired: options.Contains("required"),
  230. defaultValue: options.getDefaultOptionValue(),
  231. }
  232. }
  233. // converter returns the converter for a type.
  234. func (c *cache) converter(t reflect.Type) Converter {
  235. return c.regconv[t]
  236. }
  237. // ----------------------------------------------------------------------------
  238. type structInfo struct {
  239. fields []*fieldInfo
  240. fieldsByName map[string]*fieldInfo
  241. anonymousPtrFields []int
  242. }
  243. func (i *structInfo) get(alias string) *fieldInfo {
  244. aliasKey := utils.ToLower(alias)
  245. if field, ok := i.fieldsByName[aliasKey]; ok {
  246. return field
  247. }
  248. for _, field := range i.fields {
  249. if utils.ToLower(field.alias) == aliasKey {
  250. return field
  251. }
  252. }
  253. return nil
  254. }
  255. func containsAlias(infos []*structInfo, alias string) bool {
  256. for _, info := range infos {
  257. if info.get(alias) != nil {
  258. return true
  259. }
  260. }
  261. return false
  262. }
  263. type fieldInfo struct {
  264. typ reflect.Type
  265. // name is the field name in the struct.
  266. name string
  267. alias string
  268. // canonicalAlias is almost the same as the alias, but is prefixed with
  269. // an embedded struct field alias in dotted notation if this field is
  270. // promoted from the struct.
  271. // For instance, if the alias is "N" and this field is an embedded field
  272. // in a struct "X", canonicalAlias will be "X.N".
  273. canonicalAlias string
  274. // unmarshalerInfo contains information regarding the
  275. // encoding.TextUnmarshaler implementation of the field type.
  276. unmarshalerInfo unmarshaler
  277. // isSliceOfStructs indicates if the field type is a slice of structs.
  278. isSliceOfStructs bool
  279. // isAnonymous indicates whether the field is embedded in the struct.
  280. isAnonymous bool
  281. isRequired bool
  282. defaultValue string
  283. }
  284. func (f *fieldInfo) paths(prefix string) []string {
  285. if f.alias == f.canonicalAlias {
  286. return []string{prefix + f.alias}
  287. }
  288. return []string{prefix + f.alias, prefix + f.canonicalAlias}
  289. }
  290. type pathPart struct {
  291. field *fieldInfo
  292. path []string // path to the field: walks structs using field names.
  293. index int // struct index in slices of structs.
  294. }
  295. // ----------------------------------------------------------------------------
  296. func indirectType(typ reflect.Type) reflect.Type {
  297. if typ.Kind() == reflect.Ptr {
  298. return typ.Elem()
  299. }
  300. return typ
  301. }
  302. // fieldAlias parses a field tag to get a field alias.
  303. func fieldAlias(field reflect.StructField, tagName string) (alias string, options tagOptions) {
  304. if tag := field.Tag.Get(tagName); tag != "" {
  305. alias, options = parseTag(tag)
  306. }
  307. if alias == "" {
  308. alias = field.Name
  309. }
  310. return alias, options
  311. }
  312. // tagOptions is the string following a comma in a struct field's tag, or
  313. // the empty string. It does not include the leading comma.
  314. type tagOptions []string
  315. // parseTag splits a struct field's url tag into its name and comma-separated
  316. // options.
  317. func parseTag(tag string) (string, tagOptions) {
  318. s := strings.Split(tag, ",")
  319. return s[0], s[1:]
  320. }
  321. // Contains checks whether the tagOptions contains the specified option.
  322. func (o tagOptions) Contains(option string) bool {
  323. for _, s := range o {
  324. if s == option {
  325. return true
  326. }
  327. }
  328. return false
  329. }
  330. func (o tagOptions) getDefaultOptionValue() string {
  331. for _, s := range o {
  332. if value, ok := strings.CutPrefix(s, "default:"); ok {
  333. return value
  334. }
  335. }
  336. return ""
  337. }