decoder.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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) 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. v = v.Elem()
  67. t := v.Type()
  68. errors := MultiError{}
  69. for path, values := range src {
  70. if parts, err := d.cache.parsePath(path, t); err == nil {
  71. if err = d.decode(v, path, parts, values); err != nil {
  72. errors[path] = err
  73. }
  74. } else if !d.ignoreUnknownKeys {
  75. errors[path] = fmt.Errorf("schema: invalid path %q", path)
  76. }
  77. }
  78. if len(errors) > 0 {
  79. return errors
  80. }
  81. return d.checkRequired(t, src, "")
  82. }
  83. // checkRequired checks whether required fields are empty
  84. //
  85. // check type t recursively if t has struct fields, and prefix is same as parsePath: in dotted notation
  86. //
  87. // src is the source map for decoding, we use it here to see if those required fields are included in src
  88. func (d *Decoder) checkRequired(t reflect.Type, src map[string][]string, prefix string) error {
  89. struc := d.cache.get(t)
  90. if struc == nil {
  91. // unexpect, cache.get never return nil
  92. return errors.New("cache fail")
  93. }
  94. for _, f := range struc.fields {
  95. if f.typ.Kind() == reflect.Struct {
  96. err := d.checkRequired(f.typ, src, prefix+f.alias+".")
  97. if err != nil {
  98. if !f.isAnonymous {
  99. return err
  100. }
  101. // check embedded parent field.
  102. err2 := d.checkRequired(f.typ, src, prefix)
  103. if err2 != nil {
  104. return err
  105. }
  106. }
  107. }
  108. if f.isRequired {
  109. key := f.alias
  110. if prefix != "" {
  111. key = prefix + key
  112. }
  113. if isEmpty(f.typ, src[key]) {
  114. return fmt.Errorf("%v is empty", key)
  115. }
  116. }
  117. }
  118. return nil
  119. }
  120. // isEmpty returns true if value is empty for specific type
  121. func isEmpty(t reflect.Type, value []string) bool {
  122. if len(value) == 0 {
  123. return true
  124. }
  125. switch t.Kind() {
  126. case boolType, float32Type, float64Type, intType, int8Type, int32Type, int64Type, stringType, uint8Type, uint16Type, uint32Type, uint64Type:
  127. return len(value[0]) == 0
  128. }
  129. return false
  130. }
  131. // decode fills a struct field using a parsed path.
  132. func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values []string) error {
  133. // Get the field walking the struct fields by index.
  134. for _, name := range parts[0].path {
  135. if v.Type().Kind() == reflect.Ptr {
  136. if v.IsNil() {
  137. v.Set(reflect.New(v.Type().Elem()))
  138. }
  139. v = v.Elem()
  140. }
  141. v = v.FieldByName(name)
  142. }
  143. // Don't even bother for unexported fields.
  144. if !v.CanSet() {
  145. return nil
  146. }
  147. // Dereference if needed.
  148. t := v.Type()
  149. if t.Kind() == reflect.Ptr {
  150. t = t.Elem()
  151. if v.IsNil() {
  152. v.Set(reflect.New(t))
  153. }
  154. v = v.Elem()
  155. }
  156. // Slice of structs. Let's go recursive.
  157. if len(parts) > 1 {
  158. idx := parts[0].index
  159. if v.IsNil() || v.Len() < idx+1 {
  160. value := reflect.MakeSlice(t, idx+1, idx+1)
  161. if v.Len() < idx+1 {
  162. // Resize it.
  163. reflect.Copy(value, v)
  164. }
  165. v.Set(value)
  166. }
  167. return d.decode(v.Index(idx), path, parts[1:], values)
  168. }
  169. // Get the converter early in case there is one for a slice type.
  170. conv := d.cache.converter(t)
  171. m := isTextUnmarshaler(v)
  172. if conv == nil && t.Kind() == reflect.Slice && m.IsSliceElement {
  173. var items []reflect.Value
  174. elemT := t.Elem()
  175. isPtrElem := elemT.Kind() == reflect.Ptr
  176. if isPtrElem {
  177. elemT = elemT.Elem()
  178. }
  179. // Try to get a converter for the element type.
  180. conv := d.cache.converter(elemT)
  181. if conv == nil {
  182. conv = builtinConverters[elemT.Kind()]
  183. if conv == nil {
  184. // As we are not dealing with slice of structs here, we don't need to check if the type
  185. // implements TextUnmarshaler interface
  186. return fmt.Errorf("schema: converter not found for %v", elemT)
  187. }
  188. }
  189. for key, value := range values {
  190. if value == "" {
  191. if d.zeroEmpty {
  192. items = append(items, reflect.Zero(elemT))
  193. }
  194. } else if m.IsValid {
  195. u := reflect.New(elemT)
  196. if m.IsSliceElementPtr {
  197. u = reflect.New(reflect.PtrTo(elemT).Elem())
  198. }
  199. if err := u.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(value)); err != nil {
  200. return ConversionError{
  201. Key: path,
  202. Type: t,
  203. Index: key,
  204. Err: err,
  205. }
  206. }
  207. if m.IsSliceElementPtr {
  208. items = append(items, u.Elem().Addr())
  209. } else if u.Kind() == reflect.Ptr {
  210. items = append(items, u.Elem())
  211. } else {
  212. items = append(items, u)
  213. }
  214. } else if item := conv(value); item.IsValid() {
  215. if isPtrElem {
  216. ptr := reflect.New(elemT)
  217. ptr.Elem().Set(item)
  218. item = ptr
  219. }
  220. if item.Type() != elemT && !isPtrElem {
  221. item = item.Convert(elemT)
  222. }
  223. items = append(items, item)
  224. } else {
  225. if strings.Contains(value, ",") {
  226. values := strings.Split(value, ",")
  227. for _, value := range values {
  228. if value == "" {
  229. if d.zeroEmpty {
  230. items = append(items, reflect.Zero(elemT))
  231. }
  232. } else if item := conv(value); item.IsValid() {
  233. if isPtrElem {
  234. ptr := reflect.New(elemT)
  235. ptr.Elem().Set(item)
  236. item = ptr
  237. }
  238. if item.Type() != elemT && !isPtrElem {
  239. item = item.Convert(elemT)
  240. }
  241. items = append(items, item)
  242. } else {
  243. return ConversionError{
  244. Key: path,
  245. Type: elemT,
  246. Index: key,
  247. }
  248. }
  249. }
  250. } else {
  251. return ConversionError{
  252. Key: path,
  253. Type: elemT,
  254. Index: key,
  255. }
  256. }
  257. }
  258. }
  259. value := reflect.Append(reflect.MakeSlice(t, 0, 0), items...)
  260. v.Set(value)
  261. } else {
  262. val := ""
  263. // Use the last value provided if any values were provided
  264. if len(values) > 0 {
  265. val = values[len(values)-1]
  266. }
  267. if conv != nil {
  268. if value := conv(val); value.IsValid() {
  269. v.Set(value.Convert(t))
  270. } else {
  271. return ConversionError{
  272. Key: path,
  273. Type: t,
  274. Index: -1,
  275. }
  276. }
  277. } else if m.IsValid {
  278. if m.IsPtr {
  279. u := reflect.New(v.Type())
  280. if err := u.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(val)); err != nil {
  281. return ConversionError{
  282. Key: path,
  283. Type: t,
  284. Index: -1,
  285. Err: err,
  286. }
  287. }
  288. v.Set(reflect.Indirect(u))
  289. } else {
  290. // If the value implements the encoding.TextUnmarshaler interface
  291. // apply UnmarshalText as the converter
  292. if err := m.Unmarshaler.UnmarshalText([]byte(val)); err != nil {
  293. return ConversionError{
  294. Key: path,
  295. Type: t,
  296. Index: -1,
  297. Err: err,
  298. }
  299. }
  300. }
  301. } else if val == "" {
  302. if d.zeroEmpty {
  303. v.Set(reflect.Zero(t))
  304. }
  305. } else if conv := builtinConverters[t.Kind()]; conv != nil {
  306. if value := conv(val); value.IsValid() {
  307. v.Set(value.Convert(t))
  308. } else {
  309. return ConversionError{
  310. Key: path,
  311. Type: t,
  312. Index: -1,
  313. }
  314. }
  315. } else {
  316. return fmt.Errorf("schema: converter not found for %v", t)
  317. }
  318. }
  319. return nil
  320. }
  321. func isTextUnmarshaler(v reflect.Value) unmarshaler {
  322. // Create a new unmarshaller instance
  323. m := unmarshaler{}
  324. if m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler); m.IsValid {
  325. return m
  326. }
  327. // As the UnmarshalText function should be applied to the pointer of the
  328. // type, we check that type to see if it implements the necessary
  329. // method.
  330. if m.Unmarshaler, m.IsValid = reflect.New(v.Type()).Interface().(encoding.TextUnmarshaler); m.IsValid {
  331. m.IsPtr = true
  332. return m
  333. }
  334. // if v is []T or *[]T create new T
  335. t := v.Type()
  336. if t.Kind() == reflect.Ptr {
  337. t = t.Elem()
  338. }
  339. if t.Kind() == reflect.Slice {
  340. // Check if the slice implements encoding.TextUnmarshaller
  341. if m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler); m.IsValid {
  342. return m
  343. }
  344. // If t is a pointer slice, check if its elements implement
  345. // encoding.TextUnmarshaler
  346. m.IsSliceElement = true
  347. if t = t.Elem(); t.Kind() == reflect.Ptr {
  348. t = reflect.PtrTo(t.Elem())
  349. v = reflect.Zero(t)
  350. m.IsSliceElementPtr = true
  351. m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler)
  352. return m
  353. }
  354. }
  355. v = reflect.New(t)
  356. m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler)
  357. return m
  358. }
  359. // TextUnmarshaler helpers ----------------------------------------------------
  360. // unmarshaller contains information about a TextUnmarshaler type
  361. type unmarshaler struct {
  362. Unmarshaler encoding.TextUnmarshaler
  363. // IsValid indicates whether the resolved type indicated by the other
  364. // flags implements the encoding.TextUnmarshaler interface.
  365. IsValid bool
  366. // IsPtr indicates that the resolved type is the pointer of the original
  367. // type.
  368. IsPtr bool
  369. // IsSliceElement indicates that the resolved type is a slice element of
  370. // the original type.
  371. IsSliceElement bool
  372. // IsSliceElementPtr indicates that the resolved type is a pointer to a
  373. // slice element of the original type.
  374. IsSliceElementPtr bool
  375. }
  376. // Errors ---------------------------------------------------------------------
  377. // ConversionError stores information about a failed conversion.
  378. type ConversionError struct {
  379. Key string // key from the source map.
  380. Type reflect.Type // expected type of elem
  381. Index int // index for multi-value fields; -1 for single-value fields.
  382. Err error // low-level error (when it exists)
  383. }
  384. func (e ConversionError) Error() string {
  385. var output string
  386. if e.Index < 0 {
  387. output = fmt.Sprintf("schema: error converting value for %q", e.Key)
  388. } else {
  389. output = fmt.Sprintf("schema: error converting value for index %d of %q",
  390. e.Index, e.Key)
  391. }
  392. if e.Err != nil {
  393. output = fmt.Sprintf("%s. Details: %s", output, e.Err)
  394. }
  395. return output
  396. }
  397. // MultiError stores multiple decoding errors.
  398. //
  399. // Borrowed from the App Engine SDK.
  400. type MultiError map[string]error
  401. func (e MultiError) Error() string {
  402. s := ""
  403. for _, err := range e {
  404. s = err.Error()
  405. break
  406. }
  407. switch len(e) {
  408. case 0:
  409. return "(0 errors)"
  410. case 1:
  411. return s
  412. case 2:
  413. return s + " (and 1 other error)"
  414. }
  415. return fmt.Sprintf("%s (and %d other errors)", s, len(e)-1)
  416. }