decoder.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. "encoding"
  7. "errors"
  8. "fmt"
  9. "reflect"
  10. "strings"
  11. )
  12. // NewDecoder returns a new Decoder.
  13. func NewDecoder() *Decoder {
  14. return &Decoder{cache: newCache()}
  15. }
  16. // Decoder decodes values from a map[string][]string to a struct.
  17. type Decoder struct {
  18. cache *cache
  19. zeroEmpty bool
  20. ignoreUnknownKeys bool
  21. }
  22. // SetAliasTag changes the tag used to locate custom field aliases.
  23. // The default tag is "schema".
  24. func (d *Decoder) SetAliasTag(tag string) {
  25. d.cache.tag = tag
  26. }
  27. // ZeroEmpty controls the behaviour when the decoder encounters empty values
  28. // in a map.
  29. // If z is true and a key in the map has the empty string as a value
  30. // then the corresponding struct field is set to the zero value.
  31. // If z is false then empty strings are ignored.
  32. //
  33. // The default value is false, that is empty values do not change
  34. // the value of the struct field.
  35. func (d *Decoder) ZeroEmpty(z bool) {
  36. d.zeroEmpty = z
  37. }
  38. // IgnoreUnknownKeys controls the behaviour when the decoder encounters unknown
  39. // keys in the map.
  40. // If i is true and an unknown field is encountered, it is ignored. This is
  41. // similar to how unknown keys are handled by encoding/json.
  42. // If i is false then Decode will return an error. Note that any valid keys
  43. // will still be decoded in to the target struct.
  44. //
  45. // To preserve backwards compatibility, the default value is false.
  46. func (d *Decoder) IgnoreUnknownKeys(i bool) {
  47. d.ignoreUnknownKeys = i
  48. }
  49. // RegisterConverter registers a converter function for a custom type.
  50. func (d *Decoder) RegisterConverter(value interface{}, converterFunc Converter) {
  51. d.cache.registerConverter(value, converterFunc)
  52. }
  53. // Decode decodes a map[string][]string to a struct.
  54. //
  55. // The first parameter must be a pointer to a struct.
  56. //
  57. // The second parameter is a map, typically url.Values from an HTTP request.
  58. // Keys are "paths" in dotted notation to the struct fields and nested structs.
  59. //
  60. // See the package documentation for a full explanation of the mechanics.
  61. func (d *Decoder) Decode(dst interface{}, src map[string][]string) (err error) {
  62. v := reflect.ValueOf(dst)
  63. if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
  64. return errors.New("schema: interface must be a pointer to struct")
  65. }
  66. // Catch panics from the decoder and return them as an error.
  67. // This is needed because the decoder calls reflect and reflect panics
  68. defer func() {
  69. if r := recover(); r != nil {
  70. if e, ok := r.(error); ok {
  71. err = e
  72. } else {
  73. err = fmt.Errorf("schema: panic while decoding: %v", r)
  74. }
  75. }
  76. }()
  77. v = v.Elem()
  78. t := v.Type()
  79. multiError := MultiError{}
  80. for path, values := range src {
  81. if parts, err := d.cache.parsePath(path, t); err == nil {
  82. if err = d.decode(v, path, parts, values); err != nil {
  83. multiError[path] = err
  84. }
  85. } else {
  86. if errors.Is(err, errIndexTooLarge) {
  87. multiError[path] = err
  88. } else if !d.ignoreUnknownKeys {
  89. multiError[path] = UnknownKeyError{Key: path}
  90. }
  91. }
  92. }
  93. multiError.merge(d.checkRequired(t, src))
  94. if len(multiError) > 0 {
  95. return multiError
  96. }
  97. return nil
  98. }
  99. // checkRequired checks whether required fields are empty
  100. //
  101. // check type t recursively if t has struct fields.
  102. //
  103. // src is the source map for decoding, we use it here to see if those required fields are included in src
  104. func (d *Decoder) checkRequired(t reflect.Type, src map[string][]string) MultiError {
  105. m, errs := d.findRequiredFields(t, "", "")
  106. for key, fields := range m {
  107. if isEmptyFields(fields, src) {
  108. errs[key] = EmptyFieldError{Key: key}
  109. }
  110. }
  111. return errs
  112. }
  113. // findRequiredFields recursively searches the struct type t for required fields.
  114. //
  115. // canonicalPrefix and searchPrefix are used to resolve full paths in dotted notation
  116. // for nested struct fields. canonicalPrefix is a complete path which never omits
  117. // any embedded struct fields. searchPrefix is a user-friendly path which may omit
  118. // some embedded struct fields to point promoted fields.
  119. func (d *Decoder) findRequiredFields(t reflect.Type, canonicalPrefix, searchPrefix string) (map[string][]fieldWithPrefix, MultiError) {
  120. struc := d.cache.get(t)
  121. if struc == nil {
  122. // unexpect, cache.get never return nil
  123. return nil, MultiError{canonicalPrefix + "*": errors.New("cache fail")}
  124. }
  125. m := map[string][]fieldWithPrefix{}
  126. errs := MultiError{}
  127. for _, f := range struc.fields {
  128. if f.typ.Kind() == reflect.Struct {
  129. fcprefix := canonicalPrefix + f.canonicalAlias + "."
  130. for _, fspath := range f.paths(searchPrefix) {
  131. fm, ferrs := d.findRequiredFields(f.typ, fcprefix, fspath+".")
  132. for key, fields := range fm {
  133. m[key] = append(m[key], fields...)
  134. }
  135. errs.merge(ferrs)
  136. }
  137. }
  138. if f.isRequired {
  139. key := canonicalPrefix + f.canonicalAlias
  140. m[key] = append(m[key], fieldWithPrefix{
  141. fieldInfo: f,
  142. prefix: searchPrefix,
  143. })
  144. }
  145. }
  146. return m, errs
  147. }
  148. type fieldWithPrefix struct {
  149. *fieldInfo
  150. prefix string
  151. }
  152. // isEmptyFields returns true if all of specified fields are empty.
  153. func isEmptyFields(fields []fieldWithPrefix, src map[string][]string) bool {
  154. for _, f := range fields {
  155. for _, path := range f.paths(f.prefix) {
  156. v, ok := src[path]
  157. if ok && !isEmpty(f.typ, v) {
  158. return false
  159. }
  160. for key := range src {
  161. // issue references:
  162. // https://github.com/gofiber/fiber/issues/1414
  163. // https://github.com/gorilla/schema/issues/176
  164. nested := strings.IndexByte(key, '.') != -1
  165. // for non required nested structs
  166. c1 := strings.HasSuffix(f.prefix, ".") && key == path
  167. // for required nested structs
  168. c2 := f.prefix == "" && nested && strings.HasPrefix(key, path)
  169. // for non nested fields
  170. c3 := f.prefix == "" && !nested && key == path
  171. if !isEmpty(f.typ, src[key]) && (c1 || c2 || c3) {
  172. return false
  173. }
  174. }
  175. }
  176. }
  177. return true
  178. }
  179. // isEmpty returns true if value is empty for specific type
  180. func isEmpty(t reflect.Type, value []string) bool {
  181. if len(value) == 0 {
  182. return true
  183. }
  184. switch t.Kind() {
  185. case boolType, float32Type, float64Type, intType, int8Type, int32Type, int64Type, stringType, uint8Type, uint16Type, uint32Type, uint64Type:
  186. return len(value[0]) == 0
  187. }
  188. return false
  189. }
  190. // decode fills a struct field using a parsed path.
  191. func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values []string) error {
  192. // Get the field walking the struct fields by index.
  193. for _, name := range parts[0].path {
  194. if v.Type().Kind() == reflect.Ptr {
  195. if v.IsNil() {
  196. v.Set(reflect.New(v.Type().Elem()))
  197. }
  198. v = v.Elem()
  199. }
  200. // alloc embedded structs
  201. if v.Type().Kind() == reflect.Struct {
  202. for i := 0; i < v.NumField(); i++ {
  203. field := v.Field(i)
  204. if field.Type().Kind() == reflect.Ptr && field.IsNil() && v.Type().Field(i).Anonymous {
  205. field.Set(reflect.New(field.Type().Elem()))
  206. }
  207. }
  208. }
  209. v = v.FieldByName(name)
  210. }
  211. // Don't even bother for unexported fields.
  212. if !v.CanSet() {
  213. return nil
  214. }
  215. // Dereference if needed.
  216. t := v.Type()
  217. if t.Kind() == reflect.Ptr {
  218. t = t.Elem()
  219. if v.IsNil() {
  220. v.Set(reflect.New(t))
  221. }
  222. v = v.Elem()
  223. }
  224. // Slice of structs. Let's go recursive.
  225. if len(parts) > 1 {
  226. idx := parts[0].index
  227. if v.IsNil() || v.Len() < idx+1 {
  228. value := reflect.MakeSlice(t, idx+1, idx+1)
  229. if v.Len() < idx+1 {
  230. // Resize it.
  231. reflect.Copy(value, v)
  232. }
  233. v.Set(value)
  234. }
  235. return d.decode(v.Index(idx), path, parts[1:], values)
  236. }
  237. // Get the converter early in case there is one for a slice type.
  238. conv := d.cache.converter(t)
  239. m := isTextUnmarshaler(v)
  240. if conv == nil && t.Kind() == reflect.Slice && m.IsSliceElement {
  241. var items []reflect.Value
  242. elemT := t.Elem()
  243. isPtrElem := elemT.Kind() == reflect.Ptr
  244. if isPtrElem {
  245. elemT = elemT.Elem()
  246. }
  247. // Try to get a converter for the element type.
  248. conv := d.cache.converter(elemT)
  249. if conv == nil {
  250. conv = builtinConverters[elemT.Kind()]
  251. if conv == nil {
  252. // As we are not dealing with slice of structs here, we don't need to check if the type
  253. // implements TextUnmarshaler interface
  254. return fmt.Errorf("schema: converter not found for %v", elemT)
  255. }
  256. }
  257. for key, value := range values {
  258. if value == "" {
  259. if d.zeroEmpty {
  260. items = append(items, reflect.Zero(elemT))
  261. }
  262. } else if m.IsValid {
  263. u := reflect.New(elemT)
  264. if m.IsSliceElementPtr {
  265. u = reflect.New(reflect.PtrTo(elemT).Elem())
  266. }
  267. if err := u.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(value)); err != nil {
  268. return ConversionError{
  269. Key: path,
  270. Type: t,
  271. Index: key,
  272. Err: err,
  273. }
  274. }
  275. if m.IsSliceElementPtr {
  276. items = append(items, u.Elem().Addr())
  277. } else if u.Kind() == reflect.Ptr {
  278. items = append(items, u.Elem())
  279. } else {
  280. items = append(items, u)
  281. }
  282. } else if item := conv(value); item.IsValid() {
  283. if isPtrElem {
  284. ptr := reflect.New(elemT)
  285. ptr.Elem().Set(item)
  286. item = ptr
  287. }
  288. if item.Type() != elemT && !isPtrElem {
  289. item = item.Convert(elemT)
  290. }
  291. items = append(items, item)
  292. } else {
  293. if strings.Contains(value, ",") {
  294. values := strings.Split(value, ",")
  295. for _, value := range values {
  296. if value == "" {
  297. if d.zeroEmpty {
  298. items = append(items, reflect.Zero(elemT))
  299. }
  300. } else if item := conv(value); item.IsValid() {
  301. if isPtrElem {
  302. ptr := reflect.New(elemT)
  303. ptr.Elem().Set(item)
  304. item = ptr
  305. }
  306. if item.Type() != elemT && !isPtrElem {
  307. item = item.Convert(elemT)
  308. }
  309. items = append(items, item)
  310. } else {
  311. return ConversionError{
  312. Key: path,
  313. Type: elemT,
  314. Index: key,
  315. }
  316. }
  317. }
  318. } else {
  319. return ConversionError{
  320. Key: path,
  321. Type: elemT,
  322. Index: key,
  323. }
  324. }
  325. }
  326. }
  327. value := reflect.Append(reflect.MakeSlice(t, 0, 0), items...)
  328. v.Set(value)
  329. } else {
  330. val := ""
  331. // Use the last value provided if any values were provided
  332. if len(values) > 0 {
  333. val = values[len(values)-1]
  334. }
  335. if conv != nil {
  336. if value := conv(val); value.IsValid() {
  337. v.Set(value.Convert(t))
  338. } else {
  339. return ConversionError{
  340. Key: path,
  341. Type: t,
  342. Index: -1,
  343. }
  344. }
  345. } else if m.IsValid {
  346. if m.IsPtr {
  347. u := reflect.New(v.Type())
  348. if err := u.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(val)); err != nil {
  349. return ConversionError{
  350. Key: path,
  351. Type: t,
  352. Index: -1,
  353. Err: err,
  354. }
  355. }
  356. v.Set(reflect.Indirect(u))
  357. } else {
  358. // If the value implements the encoding.TextUnmarshaler interface
  359. // apply UnmarshalText as the converter
  360. if err := m.Unmarshaler.UnmarshalText([]byte(val)); err != nil {
  361. return ConversionError{
  362. Key: path,
  363. Type: t,
  364. Index: -1,
  365. Err: err,
  366. }
  367. }
  368. }
  369. } else if val == "" {
  370. if d.zeroEmpty {
  371. v.Set(reflect.Zero(t))
  372. }
  373. } else if conv := builtinConverters[t.Kind()]; conv != nil {
  374. if value := conv(val); value.IsValid() {
  375. v.Set(value.Convert(t))
  376. } else {
  377. return ConversionError{
  378. Key: path,
  379. Type: t,
  380. Index: -1,
  381. }
  382. }
  383. } else {
  384. return fmt.Errorf("schema: converter not found for %v", t)
  385. }
  386. }
  387. return nil
  388. }
  389. func isTextUnmarshaler(v reflect.Value) unmarshaler {
  390. // Create a new unmarshaller instance
  391. m := unmarshaler{}
  392. if m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler); m.IsValid {
  393. return m
  394. }
  395. // As the UnmarshalText function should be applied to the pointer of the
  396. // type, we check that type to see if it implements the necessary
  397. // method.
  398. if m.Unmarshaler, m.IsValid = reflect.New(v.Type()).Interface().(encoding.TextUnmarshaler); m.IsValid {
  399. m.IsPtr = true
  400. return m
  401. }
  402. // if v is []T or *[]T create new T
  403. t := v.Type()
  404. if t.Kind() == reflect.Ptr {
  405. t = t.Elem()
  406. }
  407. if t.Kind() == reflect.Slice {
  408. // Check if the slice implements encoding.TextUnmarshaller
  409. if m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler); m.IsValid {
  410. return m
  411. }
  412. // If t is a pointer slice, check if its elements implement
  413. // encoding.TextUnmarshaler
  414. m.IsSliceElement = true
  415. if t = t.Elem(); t.Kind() == reflect.Ptr {
  416. t = reflect.PtrTo(t.Elem())
  417. v = reflect.Zero(t)
  418. m.IsSliceElementPtr = true
  419. m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler)
  420. return m
  421. }
  422. }
  423. v = reflect.New(t)
  424. m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler)
  425. return m
  426. }
  427. // TextUnmarshaler helpers ----------------------------------------------------
  428. // unmarshaller contains information about a TextUnmarshaler type
  429. type unmarshaler struct {
  430. Unmarshaler encoding.TextUnmarshaler
  431. // IsValid indicates whether the resolved type indicated by the other
  432. // flags implements the encoding.TextUnmarshaler interface.
  433. IsValid bool
  434. // IsPtr indicates that the resolved type is the pointer of the original
  435. // type.
  436. IsPtr bool
  437. // IsSliceElement indicates that the resolved type is a slice element of
  438. // the original type.
  439. IsSliceElement bool
  440. // IsSliceElementPtr indicates that the resolved type is a pointer to a
  441. // slice element of the original type.
  442. IsSliceElementPtr bool
  443. }
  444. // Errors ---------------------------------------------------------------------
  445. // ConversionError stores information about a failed conversion.
  446. type ConversionError struct {
  447. Key string // key from the source map.
  448. Type reflect.Type // expected type of elem
  449. Index int // index for multi-value fields; -1 for single-value fields.
  450. Err error // low-level error (when it exists)
  451. }
  452. func (e ConversionError) Error() string {
  453. var output string
  454. if e.Index < 0 {
  455. output = fmt.Sprintf("schema: error converting value for %q", e.Key)
  456. } else {
  457. output = fmt.Sprintf("schema: error converting value for index %d of %q",
  458. e.Index, e.Key)
  459. }
  460. if e.Err != nil {
  461. output = fmt.Sprintf("%s. Details: %s", output, e.Err)
  462. }
  463. return output
  464. }
  465. // UnknownKeyError stores information about an unknown key in the source map.
  466. type UnknownKeyError struct {
  467. Key string // key from the source map.
  468. }
  469. func (e UnknownKeyError) Error() string {
  470. return fmt.Sprintf("schema: invalid path %q", e.Key)
  471. }
  472. // EmptyFieldError stores information about an empty required field.
  473. type EmptyFieldError struct {
  474. Key string // required key in the source map.
  475. }
  476. func (e EmptyFieldError) Error() string {
  477. return fmt.Sprintf("%v is empty", e.Key)
  478. }
  479. // MultiError stores multiple decoding errors.
  480. //
  481. // Borrowed from the App Engine SDK.
  482. type MultiError map[string]error
  483. func (e MultiError) Error() string {
  484. s := ""
  485. for _, err := range e {
  486. s = err.Error()
  487. break
  488. }
  489. switch len(e) {
  490. case 0:
  491. return "(0 errors)"
  492. case 1:
  493. return s
  494. case 2:
  495. return s + " (and 1 other error)"
  496. }
  497. return fmt.Sprintf("%s (and %d other errors)", s, len(e)-1)
  498. }
  499. func (e MultiError) merge(errors MultiError) {
  500. for key, err := range errors {
  501. if e[key] == nil {
  502. e[key] = err
  503. }
  504. }
  505. }