mapstructure.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. // The mapstructure package exposes functionality to convert an
  2. // abitrary map[string]interface{} into a native Go structure.
  3. //
  4. // The Go structure can be arbitrarily complex, containing slices,
  5. // other structs, etc. and the decoder will properly decode nested
  6. // maps and so on into the proper structures in the native Go struct.
  7. // See the examples to see what the decoder is capable of.
  8. package mapstructure
  9. import (
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "reflect"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. )
  18. // DecodeHookFunc is the callback function that can be used for
  19. // data transformations. See "DecodeHook" in the DecoderConfig
  20. // struct.
  21. //
  22. // The type should be DecodeHookFuncType or DecodeHookFuncKind.
  23. // Either is accepted. Types are a superset of Kinds (Types can return
  24. // Kinds) and are generally a richer thing to use, but Kinds are simpler
  25. // if you only need those.
  26. //
  27. // The reason DecodeHookFunc is multi-typed is for backwards compatibility:
  28. // we started with Kinds and then realized Types were the better solution,
  29. // but have a promise to not break backwards compat so we now support
  30. // both.
  31. type DecodeHookFunc interface{}
  32. type DecodeHookFuncType func(reflect.Type, reflect.Type, interface{}) (interface{}, error)
  33. type DecodeHookFuncKind func(reflect.Kind, reflect.Kind, interface{}) (interface{}, error)
  34. // DecoderConfig is the configuration that is used to create a new decoder
  35. // and allows customization of various aspects of decoding.
  36. type DecoderConfig struct {
  37. // DecodeHook, if set, will be called before any decoding and any
  38. // type conversion (if WeaklyTypedInput is on). This lets you modify
  39. // the values before they're set down onto the resulting struct.
  40. //
  41. // If an error is returned, the entire decode will fail with that
  42. // error.
  43. DecodeHook DecodeHookFunc
  44. // If ErrorUnused is true, then it is an error for there to exist
  45. // keys in the original map that were unused in the decoding process
  46. // (extra keys).
  47. ErrorUnused bool
  48. // ZeroFields, if set to true, will zero fields before writing them.
  49. // For example, a map will be emptied before decoded values are put in
  50. // it. If this is false, a map will be merged.
  51. ZeroFields bool
  52. // If WeaklyTypedInput is true, the decoder will make the following
  53. // "weak" conversions:
  54. //
  55. // - bools to string (true = "1", false = "0")
  56. // - numbers to string (base 10)
  57. // - bools to int/uint (true = 1, false = 0)
  58. // - strings to int/uint (base implied by prefix)
  59. // - int to bool (true if value != 0)
  60. // - string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F,
  61. // FALSE, false, False. Anything else is an error)
  62. // - empty array = empty map and vice versa
  63. // - negative numbers to overflowed uint values (base 10)
  64. // - slice of maps to a merged map
  65. //
  66. WeaklyTypedInput bool
  67. // Metadata is the struct that will contain extra metadata about
  68. // the decoding. If this is nil, then no metadata will be tracked.
  69. Metadata *Metadata
  70. // Result is a pointer to the struct that will contain the decoded
  71. // value.
  72. Result interface{}
  73. // The tag name that mapstructure reads for field names. This
  74. // defaults to "mapstructure"
  75. TagName string
  76. }
  77. // A Decoder takes a raw interface value and turns it into structured
  78. // data, keeping track of rich error information along the way in case
  79. // anything goes wrong. Unlike the basic top-level Decode method, you can
  80. // more finely control how the Decoder behaves using the DecoderConfig
  81. // structure. The top-level Decode method is just a convenience that sets
  82. // up the most basic Decoder.
  83. type Decoder struct {
  84. config *DecoderConfig
  85. }
  86. // Metadata contains information about decoding a structure that
  87. // is tedious or difficult to get otherwise.
  88. type Metadata struct {
  89. // Keys are the keys of the structure which were successfully decoded
  90. Keys []string
  91. // Unused is a slice of keys that were found in the raw value but
  92. // weren't decoded since there was no matching field in the result interface
  93. Unused []string
  94. }
  95. // Decode takes a map and uses reflection to convert it into the
  96. // given Go native structure. val must be a pointer to a struct.
  97. func Decode(m interface{}, rawVal interface{}) error {
  98. config := &DecoderConfig{
  99. Metadata: nil,
  100. Result: rawVal,
  101. }
  102. decoder, err := NewDecoder(config)
  103. if err != nil {
  104. return err
  105. }
  106. return decoder.Decode(m)
  107. }
  108. // WeakDecode is the same as Decode but is shorthand to enable
  109. // WeaklyTypedInput. See DecoderConfig for more info.
  110. func WeakDecode(input, output interface{}) error {
  111. config := &DecoderConfig{
  112. Metadata: nil,
  113. Result: output,
  114. WeaklyTypedInput: true,
  115. }
  116. decoder, err := NewDecoder(config)
  117. if err != nil {
  118. return err
  119. }
  120. return decoder.Decode(input)
  121. }
  122. // NewDecoder returns a new decoder for the given configuration. Once
  123. // a decoder has been returned, the same configuration must not be used
  124. // again.
  125. func NewDecoder(config *DecoderConfig) (*Decoder, error) {
  126. val := reflect.ValueOf(config.Result)
  127. if val.Kind() != reflect.Ptr {
  128. return nil, errors.New("result must be a pointer")
  129. }
  130. val = val.Elem()
  131. if !val.CanAddr() {
  132. return nil, errors.New("result must be addressable (a pointer)")
  133. }
  134. if config.Metadata != nil {
  135. if config.Metadata.Keys == nil {
  136. config.Metadata.Keys = make([]string, 0)
  137. }
  138. if config.Metadata.Unused == nil {
  139. config.Metadata.Unused = make([]string, 0)
  140. }
  141. }
  142. if config.TagName == "" {
  143. config.TagName = "mapstructure"
  144. }
  145. result := &Decoder{
  146. config: config,
  147. }
  148. return result, nil
  149. }
  150. // Decode decodes the given raw interface to the target pointer specified
  151. // by the configuration.
  152. func (d *Decoder) Decode(raw interface{}) error {
  153. return d.decode("", raw, reflect.ValueOf(d.config.Result).Elem())
  154. }
  155. // Decodes an unknown data type into a specific reflection value.
  156. func (d *Decoder) decode(name string, data interface{}, val reflect.Value) error {
  157. if data == nil {
  158. // If the data is nil, then we don't set anything.
  159. return nil
  160. }
  161. dataVal := reflect.ValueOf(data)
  162. if !dataVal.IsValid() {
  163. // If the data value is invalid, then we just set the value
  164. // to be the zero value.
  165. val.Set(reflect.Zero(val.Type()))
  166. return nil
  167. }
  168. if d.config.DecodeHook != nil {
  169. // We have a DecodeHook, so let's pre-process the data.
  170. var err error
  171. data, err = DecodeHookExec(
  172. d.config.DecodeHook,
  173. dataVal.Type(), val.Type(), data)
  174. if err != nil {
  175. return err
  176. }
  177. }
  178. var err error
  179. dataKind := getKind(val)
  180. switch dataKind {
  181. case reflect.Bool:
  182. err = d.decodeBool(name, data, val)
  183. case reflect.Interface:
  184. err = d.decodeBasic(name, data, val)
  185. case reflect.String:
  186. err = d.decodeString(name, data, val)
  187. case reflect.Int:
  188. err = d.decodeInt(name, data, val)
  189. case reflect.Uint:
  190. err = d.decodeUint(name, data, val)
  191. case reflect.Float32:
  192. err = d.decodeFloat(name, data, val)
  193. case reflect.Struct:
  194. err = d.decodeStruct(name, data, val)
  195. case reflect.Map:
  196. err = d.decodeMap(name, data, val)
  197. case reflect.Ptr:
  198. err = d.decodePtr(name, data, val)
  199. case reflect.Slice:
  200. err = d.decodeSlice(name, data, val)
  201. default:
  202. // If we reached this point then we weren't able to decode it
  203. return fmt.Errorf("%s: unsupported type: %s", name, dataKind)
  204. }
  205. // If we reached here, then we successfully decoded SOMETHING, so
  206. // mark the key as used if we're tracking metadata.
  207. if d.config.Metadata != nil && name != "" {
  208. d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
  209. }
  210. return err
  211. }
  212. // This decodes a basic type (bool, int, string, etc.) and sets the
  213. // value to "data" of that type.
  214. func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value) error {
  215. dataVal := reflect.ValueOf(data)
  216. if !dataVal.IsValid() {
  217. dataVal = reflect.Zero(val.Type())
  218. }
  219. dataValType := dataVal.Type()
  220. if !dataValType.AssignableTo(val.Type()) {
  221. return fmt.Errorf(
  222. "'%s' expected type '%s', got '%s'",
  223. name, val.Type(), dataValType)
  224. }
  225. val.Set(dataVal)
  226. return nil
  227. }
  228. func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value) error {
  229. dataVal := reflect.ValueOf(data)
  230. dataKind := getKind(dataVal)
  231. converted := true
  232. switch {
  233. case dataKind == reflect.String:
  234. val.SetString(dataVal.String())
  235. case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
  236. if dataVal.Bool() {
  237. val.SetString("1")
  238. } else {
  239. val.SetString("0")
  240. }
  241. case dataKind == reflect.Int && d.config.WeaklyTypedInput:
  242. val.SetString(strconv.FormatInt(dataVal.Int(), 10))
  243. case dataKind == reflect.Uint && d.config.WeaklyTypedInput:
  244. val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
  245. case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
  246. val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64))
  247. case dataKind == reflect.Slice && d.config.WeaklyTypedInput:
  248. dataType := dataVal.Type()
  249. elemKind := dataType.Elem().Kind()
  250. switch {
  251. case elemKind == reflect.Uint8:
  252. val.SetString(string(dataVal.Interface().([]uint8)))
  253. default:
  254. converted = false
  255. }
  256. default:
  257. converted = false
  258. }
  259. if !converted {
  260. return fmt.Errorf(
  261. "'%s' expected type '%s', got unconvertible type '%s'",
  262. name, val.Type(), dataVal.Type())
  263. }
  264. return nil
  265. }
  266. func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error {
  267. dataVal := reflect.ValueOf(data)
  268. dataKind := getKind(dataVal)
  269. dataType := dataVal.Type()
  270. switch {
  271. case dataKind == reflect.Int:
  272. val.SetInt(dataVal.Int())
  273. case dataKind == reflect.Uint:
  274. val.SetInt(int64(dataVal.Uint()))
  275. case dataKind == reflect.Float32:
  276. val.SetInt(int64(dataVal.Float()))
  277. case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
  278. if dataVal.Bool() {
  279. val.SetInt(1)
  280. } else {
  281. val.SetInt(0)
  282. }
  283. case dataKind == reflect.String && d.config.WeaklyTypedInput:
  284. i, err := strconv.ParseInt(dataVal.String(), 0, val.Type().Bits())
  285. if err == nil {
  286. val.SetInt(i)
  287. } else {
  288. return fmt.Errorf("cannot parse '%s' as int: %s", name, err)
  289. }
  290. case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number":
  291. jn := data.(json.Number)
  292. i, err := jn.Int64()
  293. if err != nil {
  294. return fmt.Errorf(
  295. "error decoding json.Number into %s: %s", name, err)
  296. }
  297. val.SetInt(i)
  298. default:
  299. return fmt.Errorf(
  300. "'%s' expected type '%s', got unconvertible type '%s'",
  301. name, val.Type(), dataVal.Type())
  302. }
  303. return nil
  304. }
  305. func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error {
  306. dataVal := reflect.ValueOf(data)
  307. dataKind := getKind(dataVal)
  308. switch {
  309. case dataKind == reflect.Int:
  310. i := dataVal.Int()
  311. if i < 0 && !d.config.WeaklyTypedInput {
  312. return fmt.Errorf("cannot parse '%s', %d overflows uint",
  313. name, i)
  314. }
  315. val.SetUint(uint64(i))
  316. case dataKind == reflect.Uint:
  317. val.SetUint(dataVal.Uint())
  318. case dataKind == reflect.Float32:
  319. f := dataVal.Float()
  320. if f < 0 && !d.config.WeaklyTypedInput {
  321. return fmt.Errorf("cannot parse '%s', %f overflows uint",
  322. name, f)
  323. }
  324. val.SetUint(uint64(f))
  325. case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
  326. if dataVal.Bool() {
  327. val.SetUint(1)
  328. } else {
  329. val.SetUint(0)
  330. }
  331. case dataKind == reflect.String && d.config.WeaklyTypedInput:
  332. i, err := strconv.ParseUint(dataVal.String(), 0, val.Type().Bits())
  333. if err == nil {
  334. val.SetUint(i)
  335. } else {
  336. return fmt.Errorf("cannot parse '%s' as uint: %s", name, err)
  337. }
  338. default:
  339. return fmt.Errorf(
  340. "'%s' expected type '%s', got unconvertible type '%s'",
  341. name, val.Type(), dataVal.Type())
  342. }
  343. return nil
  344. }
  345. func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error {
  346. dataVal := reflect.ValueOf(data)
  347. dataKind := getKind(dataVal)
  348. switch {
  349. case dataKind == reflect.Bool:
  350. val.SetBool(dataVal.Bool())
  351. case dataKind == reflect.Int && d.config.WeaklyTypedInput:
  352. val.SetBool(dataVal.Int() != 0)
  353. case dataKind == reflect.Uint && d.config.WeaklyTypedInput:
  354. val.SetBool(dataVal.Uint() != 0)
  355. case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
  356. val.SetBool(dataVal.Float() != 0)
  357. case dataKind == reflect.String && d.config.WeaklyTypedInput:
  358. b, err := strconv.ParseBool(dataVal.String())
  359. if err == nil {
  360. val.SetBool(b)
  361. } else if dataVal.String() == "" {
  362. val.SetBool(false)
  363. } else {
  364. return fmt.Errorf("cannot parse '%s' as bool: %s", name, err)
  365. }
  366. default:
  367. return fmt.Errorf(
  368. "'%s' expected type '%s', got unconvertible type '%s'",
  369. name, val.Type(), dataVal.Type())
  370. }
  371. return nil
  372. }
  373. func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error {
  374. dataVal := reflect.ValueOf(data)
  375. dataKind := getKind(dataVal)
  376. dataType := dataVal.Type()
  377. switch {
  378. case dataKind == reflect.Int:
  379. val.SetFloat(float64(dataVal.Int()))
  380. case dataKind == reflect.Uint:
  381. val.SetFloat(float64(dataVal.Uint()))
  382. case dataKind == reflect.Float32:
  383. val.SetFloat(float64(dataVal.Float()))
  384. case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
  385. if dataVal.Bool() {
  386. val.SetFloat(1)
  387. } else {
  388. val.SetFloat(0)
  389. }
  390. case dataKind == reflect.String && d.config.WeaklyTypedInput:
  391. f, err := strconv.ParseFloat(dataVal.String(), val.Type().Bits())
  392. if err == nil {
  393. val.SetFloat(f)
  394. } else {
  395. return fmt.Errorf("cannot parse '%s' as float: %s", name, err)
  396. }
  397. case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number":
  398. jn := data.(json.Number)
  399. i, err := jn.Float64()
  400. if err != nil {
  401. return fmt.Errorf(
  402. "error decoding json.Number into %s: %s", name, err)
  403. }
  404. val.SetFloat(i)
  405. default:
  406. return fmt.Errorf(
  407. "'%s' expected type '%s', got unconvertible type '%s'",
  408. name, val.Type(), dataVal.Type())
  409. }
  410. return nil
  411. }
  412. func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) error {
  413. valType := val.Type()
  414. valKeyType := valType.Key()
  415. valElemType := valType.Elem()
  416. // By default we overwrite keys in the current map
  417. valMap := val
  418. // If the map is nil or we're purposely zeroing fields, make a new map
  419. if valMap.IsNil() || d.config.ZeroFields {
  420. // Make a new map to hold our result
  421. mapType := reflect.MapOf(valKeyType, valElemType)
  422. valMap = reflect.MakeMap(mapType)
  423. }
  424. // Check input type
  425. dataVal := reflect.Indirect(reflect.ValueOf(data))
  426. if dataVal.Kind() != reflect.Map {
  427. // In weak mode, we accept a slice of maps as an input...
  428. if d.config.WeaklyTypedInput {
  429. switch dataVal.Kind() {
  430. case reflect.Array, reflect.Slice:
  431. // Special case for BC reasons (covered by tests)
  432. if dataVal.Len() == 0 {
  433. val.Set(valMap)
  434. return nil
  435. }
  436. for i := 0; i < dataVal.Len(); i++ {
  437. err := d.decode(
  438. fmt.Sprintf("%s[%d]", name, i),
  439. dataVal.Index(i).Interface(), val)
  440. if err != nil {
  441. return err
  442. }
  443. }
  444. return nil
  445. }
  446. }
  447. return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind())
  448. }
  449. // Accumulate errors
  450. errors := make([]string, 0)
  451. for _, k := range dataVal.MapKeys() {
  452. fieldName := fmt.Sprintf("%s[%s]", name, k)
  453. // First decode the key into the proper type
  454. currentKey := reflect.Indirect(reflect.New(valKeyType))
  455. if err := d.decode(fieldName, k.Interface(), currentKey); err != nil {
  456. errors = appendErrors(errors, err)
  457. continue
  458. }
  459. // Next decode the data into the proper type
  460. v := dataVal.MapIndex(k).Interface()
  461. currentVal := reflect.Indirect(reflect.New(valElemType))
  462. if err := d.decode(fieldName, v, currentVal); err != nil {
  463. errors = appendErrors(errors, err)
  464. continue
  465. }
  466. valMap.SetMapIndex(currentKey, currentVal)
  467. }
  468. // Set the built up map to the value
  469. val.Set(valMap)
  470. // If we had errors, return those
  471. if len(errors) > 0 {
  472. return &Error{errors}
  473. }
  474. return nil
  475. }
  476. func (d *Decoder) decodePtr(name string, data interface{}, val reflect.Value) error {
  477. // Create an element of the concrete (non pointer) type and decode
  478. // into that. Then set the value of the pointer to this type.
  479. valType := val.Type()
  480. valElemType := valType.Elem()
  481. realVal := reflect.New(valElemType)
  482. if err := d.decode(name, data, reflect.Indirect(realVal)); err != nil {
  483. return err
  484. }
  485. val.Set(realVal)
  486. return nil
  487. }
  488. func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value) error {
  489. dataVal := reflect.Indirect(reflect.ValueOf(data))
  490. dataValKind := dataVal.Kind()
  491. valType := val.Type()
  492. valElemType := valType.Elem()
  493. sliceType := reflect.SliceOf(valElemType)
  494. // Check input type
  495. if dataValKind != reflect.Array && dataValKind != reflect.Slice {
  496. // Accept empty map instead of array/slice in weakly typed mode
  497. if d.config.WeaklyTypedInput && dataVal.Kind() == reflect.Map && dataVal.Len() == 0 {
  498. val.Set(reflect.MakeSlice(sliceType, 0, 0))
  499. return nil
  500. } else {
  501. return fmt.Errorf(
  502. "'%s': source data must be an array or slice, got %s", name, dataValKind)
  503. }
  504. }
  505. // Make a new slice to hold our result, same size as the original data.
  506. valSlice := reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len())
  507. // Accumulate any errors
  508. errors := make([]string, 0)
  509. for i := 0; i < dataVal.Len(); i++ {
  510. currentData := dataVal.Index(i).Interface()
  511. currentField := valSlice.Index(i)
  512. fieldName := fmt.Sprintf("%s[%d]", name, i)
  513. if err := d.decode(fieldName, currentData, currentField); err != nil {
  514. errors = appendErrors(errors, err)
  515. }
  516. }
  517. // Finally, set the value to the slice we built up
  518. val.Set(valSlice)
  519. // If there were errors, we return those
  520. if len(errors) > 0 {
  521. return &Error{errors}
  522. }
  523. return nil
  524. }
  525. func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value) error {
  526. dataVal := reflect.Indirect(reflect.ValueOf(data))
  527. // If the type of the value to write to and the data match directly,
  528. // then we just set it directly instead of recursing into the structure.
  529. if dataVal.Type() == val.Type() {
  530. val.Set(dataVal)
  531. return nil
  532. }
  533. dataValKind := dataVal.Kind()
  534. if dataValKind != reflect.Map {
  535. return fmt.Errorf("'%s' expected a map, got '%s'", name, dataValKind)
  536. }
  537. dataValType := dataVal.Type()
  538. if kind := dataValType.Key().Kind(); kind != reflect.String && kind != reflect.Interface {
  539. return fmt.Errorf(
  540. "'%s' needs a map with string keys, has '%s' keys",
  541. name, dataValType.Key().Kind())
  542. }
  543. dataValKeys := make(map[reflect.Value]struct{})
  544. dataValKeysUnused := make(map[interface{}]struct{})
  545. for _, dataValKey := range dataVal.MapKeys() {
  546. dataValKeys[dataValKey] = struct{}{}
  547. dataValKeysUnused[dataValKey.Interface()] = struct{}{}
  548. }
  549. errors := make([]string, 0)
  550. // This slice will keep track of all the structs we'll be decoding.
  551. // There can be more than one struct if there are embedded structs
  552. // that are squashed.
  553. structs := make([]reflect.Value, 1, 5)
  554. structs[0] = val
  555. // Compile the list of all the fields that we're going to be decoding
  556. // from all the structs.
  557. fields := make(map[*reflect.StructField]reflect.Value)
  558. for len(structs) > 0 {
  559. structVal := structs[0]
  560. structs = structs[1:]
  561. structType := structVal.Type()
  562. for i := 0; i < structType.NumField(); i++ {
  563. fieldType := structType.Field(i)
  564. fieldKind := fieldType.Type.Kind()
  565. // If "squash" is specified in the tag, we squash the field down.
  566. squash := false
  567. tagParts := strings.Split(fieldType.Tag.Get(d.config.TagName), ",")
  568. for _, tag := range tagParts[1:] {
  569. if tag == "squash" {
  570. squash = true
  571. break
  572. }
  573. }
  574. if squash {
  575. if fieldKind != reflect.Struct {
  576. errors = appendErrors(errors,
  577. fmt.Errorf("%s: unsupported type for squash: %s", fieldType.Name, fieldKind))
  578. } else {
  579. structs = append(structs, val.FieldByName(fieldType.Name))
  580. }
  581. continue
  582. }
  583. // Normal struct field, store it away
  584. fields[&fieldType] = structVal.Field(i)
  585. }
  586. }
  587. for fieldType, field := range fields {
  588. fieldName := fieldType.Name
  589. tagValue := fieldType.Tag.Get(d.config.TagName)
  590. tagValue = strings.SplitN(tagValue, ",", 2)[0]
  591. if tagValue != "" {
  592. fieldName = tagValue
  593. }
  594. rawMapKey := reflect.ValueOf(fieldName)
  595. rawMapVal := dataVal.MapIndex(rawMapKey)
  596. if !rawMapVal.IsValid() {
  597. // Do a slower search by iterating over each key and
  598. // doing case-insensitive search.
  599. for dataValKey := range dataValKeys {
  600. mK, ok := dataValKey.Interface().(string)
  601. if !ok {
  602. // Not a string key
  603. continue
  604. }
  605. if strings.EqualFold(mK, fieldName) {
  606. rawMapKey = dataValKey
  607. rawMapVal = dataVal.MapIndex(dataValKey)
  608. break
  609. }
  610. }
  611. if !rawMapVal.IsValid() {
  612. // There was no matching key in the map for the value in
  613. // the struct. Just ignore.
  614. continue
  615. }
  616. }
  617. // Delete the key we're using from the unused map so we stop tracking
  618. delete(dataValKeysUnused, rawMapKey.Interface())
  619. if !field.IsValid() {
  620. // This should never happen
  621. panic("field is not valid")
  622. }
  623. // If we can't set the field, then it is unexported or something,
  624. // and we just continue onwards.
  625. if !field.CanSet() {
  626. continue
  627. }
  628. // If the name is empty string, then we're at the root, and we
  629. // don't dot-join the fields.
  630. if name != "" {
  631. fieldName = fmt.Sprintf("%s.%s", name, fieldName)
  632. }
  633. if err := d.decode(fieldName, rawMapVal.Interface(), field); err != nil {
  634. errors = appendErrors(errors, err)
  635. }
  636. }
  637. if d.config.ErrorUnused && len(dataValKeysUnused) > 0 {
  638. keys := make([]string, 0, len(dataValKeysUnused))
  639. for rawKey := range dataValKeysUnused {
  640. keys = append(keys, rawKey.(string))
  641. }
  642. sort.Strings(keys)
  643. err := fmt.Errorf("'%s' has invalid keys: %s", name, strings.Join(keys, ", "))
  644. errors = appendErrors(errors, err)
  645. }
  646. if len(errors) > 0 {
  647. return &Error{errors}
  648. }
  649. // Add the unused keys to the list of unused keys if we're tracking metadata
  650. if d.config.Metadata != nil {
  651. for rawKey := range dataValKeysUnused {
  652. key := rawKey.(string)
  653. if name != "" {
  654. key = fmt.Sprintf("%s.%s", name, key)
  655. }
  656. d.config.Metadata.Unused = append(d.config.Metadata.Unused, key)
  657. }
  658. }
  659. return nil
  660. }
  661. func getKind(val reflect.Value) reflect.Kind {
  662. kind := val.Kind()
  663. switch {
  664. case kind >= reflect.Int && kind <= reflect.Int64:
  665. return reflect.Int
  666. case kind >= reflect.Uint && kind <= reflect.Uint64:
  667. return reflect.Uint
  668. case kind >= reflect.Float32 && kind <= reflect.Float64:
  669. return reflect.Float32
  670. default:
  671. return kind
  672. }
  673. }