http urls monitor.

mapstructure.go 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. // Package mapstructure exposes functionality to convert an arbitrary
  2. // 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. // DecodeHookFuncType is a DecodeHookFunc which has complete information about
  33. // the source and target types.
  34. type DecodeHookFuncType func(reflect.Type, reflect.Type, interface{}) (interface{}, error)
  35. // DecodeHookFuncKind is a DecodeHookFunc which knows only the Kinds of the
  36. // source and target types.
  37. type DecodeHookFuncKind func(reflect.Kind, reflect.Kind, interface{}) (interface{}, error)
  38. // DecoderConfig is the configuration that is used to create a new decoder
  39. // and allows customization of various aspects of decoding.
  40. type DecoderConfig struct {
  41. // DecodeHook, if set, will be called before any decoding and any
  42. // type conversion (if WeaklyTypedInput is on). This lets you modify
  43. // the values before they're set down onto the resulting struct.
  44. //
  45. // If an error is returned, the entire decode will fail with that
  46. // error.
  47. DecodeHook DecodeHookFunc
  48. // If ErrorUnused is true, then it is an error for there to exist
  49. // keys in the original map that were unused in the decoding process
  50. // (extra keys).
  51. ErrorUnused bool
  52. // ZeroFields, if set to true, will zero fields before writing them.
  53. // For example, a map will be emptied before decoded values are put in
  54. // it. If this is false, a map will be merged.
  55. ZeroFields bool
  56. // If WeaklyTypedInput is true, the decoder will make the following
  57. // "weak" conversions:
  58. //
  59. // - bools to string (true = "1", false = "0")
  60. // - numbers to string (base 10)
  61. // - bools to int/uint (true = 1, false = 0)
  62. // - strings to int/uint (base implied by prefix)
  63. // - int to bool (true if value != 0)
  64. // - string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F,
  65. // FALSE, false, False. Anything else is an error)
  66. // - empty array = empty map and vice versa
  67. // - negative numbers to overflowed uint values (base 10)
  68. // - slice of maps to a merged map
  69. // - single values are converted to slices if required. Each
  70. // element is weakly decoded. For example: "4" can become []int{4}
  71. // if the target type is an int slice.
  72. //
  73. WeaklyTypedInput bool
  74. // Metadata is the struct that will contain extra metadata about
  75. // the decoding. If this is nil, then no metadata will be tracked.
  76. Metadata *Metadata
  77. // Result is a pointer to the struct that will contain the decoded
  78. // value.
  79. Result interface{}
  80. // The tag name that mapstructure reads for field names. This
  81. // defaults to "mapstructure"
  82. TagName string
  83. }
  84. // A Decoder takes a raw interface value and turns it into structured
  85. // data, keeping track of rich error information along the way in case
  86. // anything goes wrong. Unlike the basic top-level Decode method, you can
  87. // more finely control how the Decoder behaves using the DecoderConfig
  88. // structure. The top-level Decode method is just a convenience that sets
  89. // up the most basic Decoder.
  90. type Decoder struct {
  91. config *DecoderConfig
  92. }
  93. // Metadata contains information about decoding a structure that
  94. // is tedious or difficult to get otherwise.
  95. type Metadata struct {
  96. // Keys are the keys of the structure which were successfully decoded
  97. Keys []string
  98. // Unused is a slice of keys that were found in the raw value but
  99. // weren't decoded since there was no matching field in the result interface
  100. Unused []string
  101. }
  102. // Decode takes an input structure and uses reflection to translate it to
  103. // the output structure. output must be a pointer to a map or struct.
  104. func Decode(input interface{}, output interface{}) error {
  105. config := &DecoderConfig{
  106. Metadata: nil,
  107. Result: output,
  108. }
  109. decoder, err := NewDecoder(config)
  110. if err != nil {
  111. return err
  112. }
  113. return decoder.Decode(input)
  114. }
  115. // WeakDecode is the same as Decode but is shorthand to enable
  116. // WeaklyTypedInput. See DecoderConfig for more info.
  117. func WeakDecode(input, output interface{}) error {
  118. config := &DecoderConfig{
  119. Metadata: nil,
  120. Result: output,
  121. WeaklyTypedInput: true,
  122. }
  123. decoder, err := NewDecoder(config)
  124. if err != nil {
  125. return err
  126. }
  127. return decoder.Decode(input)
  128. }
  129. // DecodeMetadata is the same as Decode, but is shorthand to
  130. // enable metadata collection. See DecoderConfig for more info.
  131. func DecodeMetadata(input interface{}, output interface{}, metadata *Metadata) error {
  132. config := &DecoderConfig{
  133. Metadata: metadata,
  134. Result: output,
  135. }
  136. decoder, err := NewDecoder(config)
  137. if err != nil {
  138. return err
  139. }
  140. return decoder.Decode(input)
  141. }
  142. // WeakDecodeMetadata is the same as Decode, but is shorthand to
  143. // enable both WeaklyTypedInput and metadata collection. See
  144. // DecoderConfig for more info.
  145. func WeakDecodeMetadata(input interface{}, output interface{}, metadata *Metadata) error {
  146. config := &DecoderConfig{
  147. Metadata: metadata,
  148. Result: output,
  149. WeaklyTypedInput: true,
  150. }
  151. decoder, err := NewDecoder(config)
  152. if err != nil {
  153. return err
  154. }
  155. return decoder.Decode(input)
  156. }
  157. // NewDecoder returns a new decoder for the given configuration. Once
  158. // a decoder has been returned, the same configuration must not be used
  159. // again.
  160. func NewDecoder(config *DecoderConfig) (*Decoder, error) {
  161. val := reflect.ValueOf(config.Result)
  162. if val.Kind() != reflect.Ptr {
  163. return nil, errors.New("result must be a pointer")
  164. }
  165. val = val.Elem()
  166. if !val.CanAddr() {
  167. return nil, errors.New("result must be addressable (a pointer)")
  168. }
  169. if config.Metadata != nil {
  170. if config.Metadata.Keys == nil {
  171. config.Metadata.Keys = make([]string, 0)
  172. }
  173. if config.Metadata.Unused == nil {
  174. config.Metadata.Unused = make([]string, 0)
  175. }
  176. }
  177. if config.TagName == "" {
  178. config.TagName = "mapstructure"
  179. }
  180. result := &Decoder{
  181. config: config,
  182. }
  183. return result, nil
  184. }
  185. // Decode decodes the given raw interface to the target pointer specified
  186. // by the configuration.
  187. func (d *Decoder) Decode(input interface{}) error {
  188. return d.decode("", input, reflect.ValueOf(d.config.Result).Elem())
  189. }
  190. // Decodes an unknown data type into a specific reflection value.
  191. func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) error {
  192. if input == nil {
  193. // If the data is nil, then we don't set anything, unless ZeroFields is set
  194. // to true.
  195. if d.config.ZeroFields {
  196. outVal.Set(reflect.Zero(outVal.Type()))
  197. if d.config.Metadata != nil && name != "" {
  198. d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
  199. }
  200. }
  201. return nil
  202. }
  203. inputVal := reflect.ValueOf(input)
  204. if !inputVal.IsValid() {
  205. // If the input value is invalid, then we just set the value
  206. // to be the zero value.
  207. outVal.Set(reflect.Zero(outVal.Type()))
  208. if d.config.Metadata != nil && name != "" {
  209. d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
  210. }
  211. return nil
  212. }
  213. if d.config.DecodeHook != nil {
  214. // We have a DecodeHook, so let's pre-process the input.
  215. var err error
  216. input, err = DecodeHookExec(
  217. d.config.DecodeHook,
  218. inputVal.Type(), outVal.Type(), input)
  219. if err != nil {
  220. return fmt.Errorf("error decoding '%s': %s", name, err)
  221. }
  222. }
  223. var err error
  224. inputKind := getKind(outVal)
  225. switch inputKind {
  226. case reflect.Bool:
  227. err = d.decodeBool(name, input, outVal)
  228. case reflect.Interface:
  229. err = d.decodeBasic(name, input, outVal)
  230. case reflect.String:
  231. err = d.decodeString(name, input, outVal)
  232. case reflect.Int:
  233. err = d.decodeInt(name, input, outVal)
  234. case reflect.Uint:
  235. err = d.decodeUint(name, input, outVal)
  236. case reflect.Float32:
  237. err = d.decodeFloat(name, input, outVal)
  238. case reflect.Struct:
  239. err = d.decodeStruct(name, input, outVal)
  240. case reflect.Map:
  241. err = d.decodeMap(name, input, outVal)
  242. case reflect.Ptr:
  243. err = d.decodePtr(name, input, outVal)
  244. case reflect.Slice:
  245. err = d.decodeSlice(name, input, outVal)
  246. case reflect.Array:
  247. err = d.decodeArray(name, input, outVal)
  248. case reflect.Func:
  249. err = d.decodeFunc(name, input, outVal)
  250. default:
  251. // If we reached this point then we weren't able to decode it
  252. return fmt.Errorf("%s: unsupported type: %s", name, inputKind)
  253. }
  254. // If we reached here, then we successfully decoded SOMETHING, so
  255. // mark the key as used if we're tracking metainput.
  256. if d.config.Metadata != nil && name != "" {
  257. d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
  258. }
  259. return err
  260. }
  261. // This decodes a basic type (bool, int, string, etc.) and sets the
  262. // value to "data" of that type.
  263. func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value) error {
  264. if val.IsValid() && val.Elem().IsValid() {
  265. return d.decode(name, data, val.Elem())
  266. }
  267. dataVal := reflect.ValueOf(data)
  268. if !dataVal.IsValid() {
  269. dataVal = reflect.Zero(val.Type())
  270. }
  271. dataValType := dataVal.Type()
  272. if !dataValType.AssignableTo(val.Type()) {
  273. return fmt.Errorf(
  274. "'%s' expected type '%s', got '%s'",
  275. name, val.Type(), dataValType)
  276. }
  277. val.Set(dataVal)
  278. return nil
  279. }
  280. func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value) error {
  281. dataVal := reflect.ValueOf(data)
  282. dataKind := getKind(dataVal)
  283. converted := true
  284. switch {
  285. case dataKind == reflect.String:
  286. val.SetString(dataVal.String())
  287. case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
  288. if dataVal.Bool() {
  289. val.SetString("1")
  290. } else {
  291. val.SetString("0")
  292. }
  293. case dataKind == reflect.Int && d.config.WeaklyTypedInput:
  294. val.SetString(strconv.FormatInt(dataVal.Int(), 10))
  295. case dataKind == reflect.Uint && d.config.WeaklyTypedInput:
  296. val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
  297. case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
  298. val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64))
  299. case dataKind == reflect.Slice && d.config.WeaklyTypedInput,
  300. dataKind == reflect.Array && d.config.WeaklyTypedInput:
  301. dataType := dataVal.Type()
  302. elemKind := dataType.Elem().Kind()
  303. switch elemKind {
  304. case reflect.Uint8:
  305. var uints []uint8
  306. if dataKind == reflect.Array {
  307. uints = make([]uint8, dataVal.Len(), dataVal.Len())
  308. for i := range uints {
  309. uints[i] = dataVal.Index(i).Interface().(uint8)
  310. }
  311. } else {
  312. uints = dataVal.Interface().([]uint8)
  313. }
  314. val.SetString(string(uints))
  315. default:
  316. converted = false
  317. }
  318. default:
  319. converted = false
  320. }
  321. if !converted {
  322. return fmt.Errorf(
  323. "'%s' expected type '%s', got unconvertible type '%s'",
  324. name, val.Type(), dataVal.Type())
  325. }
  326. return nil
  327. }
  328. func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error {
  329. dataVal := reflect.ValueOf(data)
  330. dataKind := getKind(dataVal)
  331. dataType := dataVal.Type()
  332. switch {
  333. case dataKind == reflect.Int:
  334. val.SetInt(dataVal.Int())
  335. case dataKind == reflect.Uint:
  336. val.SetInt(int64(dataVal.Uint()))
  337. case dataKind == reflect.Float32:
  338. val.SetInt(int64(dataVal.Float()))
  339. case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
  340. if dataVal.Bool() {
  341. val.SetInt(1)
  342. } else {
  343. val.SetInt(0)
  344. }
  345. case dataKind == reflect.String && d.config.WeaklyTypedInput:
  346. i, err := strconv.ParseInt(dataVal.String(), 0, val.Type().Bits())
  347. if err == nil {
  348. val.SetInt(i)
  349. } else {
  350. return fmt.Errorf("cannot parse '%s' as int: %s", name, err)
  351. }
  352. case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number":
  353. jn := data.(json.Number)
  354. i, err := jn.Int64()
  355. if err != nil {
  356. return fmt.Errorf(
  357. "error decoding json.Number into %s: %s", name, err)
  358. }
  359. val.SetInt(i)
  360. default:
  361. return fmt.Errorf(
  362. "'%s' expected type '%s', got unconvertible type '%s'",
  363. name, val.Type(), dataVal.Type())
  364. }
  365. return nil
  366. }
  367. func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error {
  368. dataVal := reflect.ValueOf(data)
  369. dataKind := getKind(dataVal)
  370. switch {
  371. case dataKind == reflect.Int:
  372. i := dataVal.Int()
  373. if i < 0 && !d.config.WeaklyTypedInput {
  374. return fmt.Errorf("cannot parse '%s', %d overflows uint",
  375. name, i)
  376. }
  377. val.SetUint(uint64(i))
  378. case dataKind == reflect.Uint:
  379. val.SetUint(dataVal.Uint())
  380. case dataKind == reflect.Float32:
  381. f := dataVal.Float()
  382. if f < 0 && !d.config.WeaklyTypedInput {
  383. return fmt.Errorf("cannot parse '%s', %f overflows uint",
  384. name, f)
  385. }
  386. val.SetUint(uint64(f))
  387. case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
  388. if dataVal.Bool() {
  389. val.SetUint(1)
  390. } else {
  391. val.SetUint(0)
  392. }
  393. case dataKind == reflect.String && d.config.WeaklyTypedInput:
  394. i, err := strconv.ParseUint(dataVal.String(), 0, val.Type().Bits())
  395. if err == nil {
  396. val.SetUint(i)
  397. } else {
  398. return fmt.Errorf("cannot parse '%s' as uint: %s", name, err)
  399. }
  400. default:
  401. return fmt.Errorf(
  402. "'%s' expected type '%s', got unconvertible type '%s'",
  403. name, val.Type(), dataVal.Type())
  404. }
  405. return nil
  406. }
  407. func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error {
  408. dataVal := reflect.ValueOf(data)
  409. dataKind := getKind(dataVal)
  410. switch {
  411. case dataKind == reflect.Bool:
  412. val.SetBool(dataVal.Bool())
  413. case dataKind == reflect.Int && d.config.WeaklyTypedInput:
  414. val.SetBool(dataVal.Int() != 0)
  415. case dataKind == reflect.Uint && d.config.WeaklyTypedInput:
  416. val.SetBool(dataVal.Uint() != 0)
  417. case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
  418. val.SetBool(dataVal.Float() != 0)
  419. case dataKind == reflect.String && d.config.WeaklyTypedInput:
  420. b, err := strconv.ParseBool(dataVal.String())
  421. if err == nil {
  422. val.SetBool(b)
  423. } else if dataVal.String() == "" {
  424. val.SetBool(false)
  425. } else {
  426. return fmt.Errorf("cannot parse '%s' as bool: %s", name, err)
  427. }
  428. default:
  429. return fmt.Errorf(
  430. "'%s' expected type '%s', got unconvertible type '%s'",
  431. name, val.Type(), dataVal.Type())
  432. }
  433. return nil
  434. }
  435. func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error {
  436. dataVal := reflect.ValueOf(data)
  437. dataKind := getKind(dataVal)
  438. dataType := dataVal.Type()
  439. switch {
  440. case dataKind == reflect.Int:
  441. val.SetFloat(float64(dataVal.Int()))
  442. case dataKind == reflect.Uint:
  443. val.SetFloat(float64(dataVal.Uint()))
  444. case dataKind == reflect.Float32:
  445. val.SetFloat(dataVal.Float())
  446. case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
  447. if dataVal.Bool() {
  448. val.SetFloat(1)
  449. } else {
  450. val.SetFloat(0)
  451. }
  452. case dataKind == reflect.String && d.config.WeaklyTypedInput:
  453. f, err := strconv.ParseFloat(dataVal.String(), val.Type().Bits())
  454. if err == nil {
  455. val.SetFloat(f)
  456. } else {
  457. return fmt.Errorf("cannot parse '%s' as float: %s", name, err)
  458. }
  459. case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number":
  460. jn := data.(json.Number)
  461. i, err := jn.Float64()
  462. if err != nil {
  463. return fmt.Errorf(
  464. "error decoding json.Number into %s: %s", name, err)
  465. }
  466. val.SetFloat(i)
  467. default:
  468. return fmt.Errorf(
  469. "'%s' expected type '%s', got unconvertible type '%s'",
  470. name, val.Type(), dataVal.Type())
  471. }
  472. return nil
  473. }
  474. func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) error {
  475. valType := val.Type()
  476. valKeyType := valType.Key()
  477. valElemType := valType.Elem()
  478. // By default we overwrite keys in the current map
  479. valMap := val
  480. // If the map is nil or we're purposely zeroing fields, make a new map
  481. if valMap.IsNil() || d.config.ZeroFields {
  482. // Make a new map to hold our result
  483. mapType := reflect.MapOf(valKeyType, valElemType)
  484. valMap = reflect.MakeMap(mapType)
  485. }
  486. // Check input type and based on the input type jump to the proper func
  487. dataVal := reflect.Indirect(reflect.ValueOf(data))
  488. switch dataVal.Kind() {
  489. case reflect.Map:
  490. return d.decodeMapFromMap(name, dataVal, val, valMap)
  491. case reflect.Struct:
  492. return d.decodeMapFromStruct(name, dataVal, val, valMap)
  493. case reflect.Array, reflect.Slice:
  494. if d.config.WeaklyTypedInput {
  495. return d.decodeMapFromSlice(name, dataVal, val, valMap)
  496. }
  497. fallthrough
  498. default:
  499. return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind())
  500. }
  501. }
  502. func (d *Decoder) decodeMapFromSlice(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
  503. // Special case for BC reasons (covered by tests)
  504. if dataVal.Len() == 0 {
  505. val.Set(valMap)
  506. return nil
  507. }
  508. for i := 0; i < dataVal.Len(); i++ {
  509. err := d.decode(
  510. fmt.Sprintf("%s[%d]", name, i),
  511. dataVal.Index(i).Interface(), val)
  512. if err != nil {
  513. return err
  514. }
  515. }
  516. return nil
  517. }
  518. func (d *Decoder) decodeMapFromMap(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
  519. valType := val.Type()
  520. valKeyType := valType.Key()
  521. valElemType := valType.Elem()
  522. // Accumulate errors
  523. errors := make([]string, 0)
  524. for _, k := range dataVal.MapKeys() {
  525. fieldName := fmt.Sprintf("%s[%s]", name, k)
  526. // First decode the key into the proper type
  527. currentKey := reflect.Indirect(reflect.New(valKeyType))
  528. if err := d.decode(fieldName, k.Interface(), currentKey); err != nil {
  529. errors = appendErrors(errors, err)
  530. continue
  531. }
  532. // Next decode the data into the proper type
  533. v := dataVal.MapIndex(k).Interface()
  534. currentVal := reflect.Indirect(reflect.New(valElemType))
  535. if err := d.decode(fieldName, v, currentVal); err != nil {
  536. errors = appendErrors(errors, err)
  537. continue
  538. }
  539. valMap.SetMapIndex(currentKey, currentVal)
  540. }
  541. // Set the built up map to the value
  542. val.Set(valMap)
  543. // If we had errors, return those
  544. if len(errors) > 0 {
  545. return &Error{errors}
  546. }
  547. return nil
  548. }
  549. func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
  550. typ := dataVal.Type()
  551. for i := 0; i < typ.NumField(); i++ {
  552. // Get the StructField first since this is a cheap operation. If the
  553. // field is unexported, then ignore it.
  554. f := typ.Field(i)
  555. if f.PkgPath != "" {
  556. continue
  557. }
  558. // Next get the actual value of this field and verify it is assignable
  559. // to the map value.
  560. v := dataVal.Field(i)
  561. if !v.Type().AssignableTo(valMap.Type().Elem()) {
  562. return fmt.Errorf("cannot assign type '%s' to map value field of type '%s'", v.Type(), valMap.Type().Elem())
  563. }
  564. tagValue := f.Tag.Get(d.config.TagName)
  565. tagParts := strings.Split(tagValue, ",")
  566. // Determine the name of the key in the map
  567. keyName := f.Name
  568. if tagParts[0] != "" {
  569. if tagParts[0] == "-" {
  570. continue
  571. }
  572. keyName = tagParts[0]
  573. }
  574. // If "squash" is specified in the tag, we squash the field down.
  575. squash := false
  576. for _, tag := range tagParts[1:] {
  577. if tag == "squash" {
  578. squash = true
  579. break
  580. }
  581. }
  582. if squash && v.Kind() != reflect.Struct {
  583. return fmt.Errorf("cannot squash non-struct type '%s'", v.Type())
  584. }
  585. switch v.Kind() {
  586. // this is an embedded struct, so handle it differently
  587. case reflect.Struct:
  588. x := reflect.New(v.Type())
  589. x.Elem().Set(v)
  590. vType := valMap.Type()
  591. vKeyType := vType.Key()
  592. vElemType := vType.Elem()
  593. mType := reflect.MapOf(vKeyType, vElemType)
  594. vMap := reflect.MakeMap(mType)
  595. err := d.decode(keyName, x.Interface(), vMap)
  596. if err != nil {
  597. return err
  598. }
  599. if squash {
  600. for _, k := range vMap.MapKeys() {
  601. valMap.SetMapIndex(k, vMap.MapIndex(k))
  602. }
  603. } else {
  604. valMap.SetMapIndex(reflect.ValueOf(keyName), vMap)
  605. }
  606. default:
  607. valMap.SetMapIndex(reflect.ValueOf(keyName), v)
  608. }
  609. }
  610. if val.CanAddr() {
  611. val.Set(valMap)
  612. }
  613. return nil
  614. }
  615. func (d *Decoder) decodePtr(name string, data interface{}, val reflect.Value) error {
  616. // Create an element of the concrete (non pointer) type and decode
  617. // into that. Then set the value of the pointer to this type.
  618. valType := val.Type()
  619. valElemType := valType.Elem()
  620. if val.CanSet() {
  621. realVal := val
  622. if realVal.IsNil() || d.config.ZeroFields {
  623. realVal = reflect.New(valElemType)
  624. }
  625. if err := d.decode(name, data, reflect.Indirect(realVal)); err != nil {
  626. return err
  627. }
  628. val.Set(realVal)
  629. } else {
  630. if err := d.decode(name, data, reflect.Indirect(val)); err != nil {
  631. return err
  632. }
  633. }
  634. return nil
  635. }
  636. func (d *Decoder) decodeFunc(name string, data interface{}, val reflect.Value) error {
  637. // Create an element of the concrete (non pointer) type and decode
  638. // into that. Then set the value of the pointer to this type.
  639. dataVal := reflect.Indirect(reflect.ValueOf(data))
  640. if val.Type() != dataVal.Type() {
  641. return fmt.Errorf(
  642. "'%s' expected type '%s', got unconvertible type '%s'",
  643. name, val.Type(), dataVal.Type())
  644. }
  645. val.Set(dataVal)
  646. return nil
  647. }
  648. func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value) error {
  649. dataVal := reflect.Indirect(reflect.ValueOf(data))
  650. dataValKind := dataVal.Kind()
  651. valType := val.Type()
  652. valElemType := valType.Elem()
  653. sliceType := reflect.SliceOf(valElemType)
  654. valSlice := val
  655. if valSlice.IsNil() || d.config.ZeroFields {
  656. // Check input type
  657. if dataValKind != reflect.Array && dataValKind != reflect.Slice {
  658. if d.config.WeaklyTypedInput {
  659. switch {
  660. // Empty maps turn into empty slices
  661. case dataValKind == reflect.Map:
  662. if dataVal.Len() == 0 {
  663. val.Set(reflect.MakeSlice(sliceType, 0, 0))
  664. return nil
  665. }
  666. // Create slice of maps of other sizes
  667. return d.decodeSlice(name, []interface{}{data}, val)
  668. case dataValKind == reflect.String && valElemType.Kind() == reflect.Uint8:
  669. return d.decodeSlice(name, []byte(dataVal.String()), val)
  670. // All other types we try to convert to the slice type
  671. // and "lift" it into it. i.e. a string becomes a string slice.
  672. default:
  673. // Just re-try this function with data as a slice.
  674. return d.decodeSlice(name, []interface{}{data}, val)
  675. }
  676. }
  677. return fmt.Errorf(
  678. "'%s': source data must be an array or slice, got %s", name, dataValKind)
  679. }
  680. // Make a new slice to hold our result, same size as the original data.
  681. valSlice = reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len())
  682. }
  683. // Accumulate any errors
  684. errors := make([]string, 0)
  685. for i := 0; i < dataVal.Len(); i++ {
  686. currentData := dataVal.Index(i).Interface()
  687. for valSlice.Len() <= i {
  688. valSlice = reflect.Append(valSlice, reflect.Zero(valElemType))
  689. }
  690. currentField := valSlice.Index(i)
  691. fieldName := fmt.Sprintf("%s[%d]", name, i)
  692. if err := d.decode(fieldName, currentData, currentField); err != nil {
  693. errors = appendErrors(errors, err)
  694. }
  695. }
  696. // Finally, set the value to the slice we built up
  697. val.Set(valSlice)
  698. // If there were errors, we return those
  699. if len(errors) > 0 {
  700. return &Error{errors}
  701. }
  702. return nil
  703. }
  704. func (d *Decoder) decodeArray(name string, data interface{}, val reflect.Value) error {
  705. dataVal := reflect.Indirect(reflect.ValueOf(data))
  706. dataValKind := dataVal.Kind()
  707. valType := val.Type()
  708. valElemType := valType.Elem()
  709. arrayType := reflect.ArrayOf(valType.Len(), valElemType)
  710. valArray := val
  711. if valArray.Interface() == reflect.Zero(valArray.Type()).Interface() || d.config.ZeroFields {
  712. // Check input type
  713. if dataValKind != reflect.Array && dataValKind != reflect.Slice {
  714. if d.config.WeaklyTypedInput {
  715. switch {
  716. // Empty maps turn into empty arrays
  717. case dataValKind == reflect.Map:
  718. if dataVal.Len() == 0 {
  719. val.Set(reflect.Zero(arrayType))
  720. return nil
  721. }
  722. // All other types we try to convert to the array type
  723. // and "lift" it into it. i.e. a string becomes a string array.
  724. default:
  725. // Just re-try this function with data as a slice.
  726. return d.decodeArray(name, []interface{}{data}, val)
  727. }
  728. }
  729. return fmt.Errorf(
  730. "'%s': source data must be an array or slice, got %s", name, dataValKind)
  731. }
  732. if dataVal.Len() > arrayType.Len() {
  733. return fmt.Errorf(
  734. "'%s': expected source data to have length less or equal to %d, got %d", name, arrayType.Len(), dataVal.Len())
  735. }
  736. // Make a new array to hold our result, same size as the original data.
  737. valArray = reflect.New(arrayType).Elem()
  738. }
  739. // Accumulate any errors
  740. errors := make([]string, 0)
  741. for i := 0; i < dataVal.Len(); i++ {
  742. currentData := dataVal.Index(i).Interface()
  743. currentField := valArray.Index(i)
  744. fieldName := fmt.Sprintf("%s[%d]", name, i)
  745. if err := d.decode(fieldName, currentData, currentField); err != nil {
  746. errors = appendErrors(errors, err)
  747. }
  748. }
  749. // Finally, set the value to the array we built up
  750. val.Set(valArray)
  751. // If there were errors, we return those
  752. if len(errors) > 0 {
  753. return &Error{errors}
  754. }
  755. return nil
  756. }
  757. func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value) error {
  758. dataVal := reflect.Indirect(reflect.ValueOf(data))
  759. // If the type of the value to write to and the data match directly,
  760. // then we just set it directly instead of recursing into the structure.
  761. if dataVal.Type() == val.Type() {
  762. val.Set(dataVal)
  763. return nil
  764. }
  765. dataValKind := dataVal.Kind()
  766. if dataValKind != reflect.Map {
  767. return fmt.Errorf("'%s' expected a map, got '%s'", name, dataValKind)
  768. }
  769. dataValType := dataVal.Type()
  770. if kind := dataValType.Key().Kind(); kind != reflect.String && kind != reflect.Interface {
  771. return fmt.Errorf(
  772. "'%s' needs a map with string keys, has '%s' keys",
  773. name, dataValType.Key().Kind())
  774. }
  775. dataValKeys := make(map[reflect.Value]struct{})
  776. dataValKeysUnused := make(map[interface{}]struct{})
  777. for _, dataValKey := range dataVal.MapKeys() {
  778. dataValKeys[dataValKey] = struct{}{}
  779. dataValKeysUnused[dataValKey.Interface()] = struct{}{}
  780. }
  781. errors := make([]string, 0)
  782. // This slice will keep track of all the structs we'll be decoding.
  783. // There can be more than one struct if there are embedded structs
  784. // that are squashed.
  785. structs := make([]reflect.Value, 1, 5)
  786. structs[0] = val
  787. // Compile the list of all the fields that we're going to be decoding
  788. // from all the structs.
  789. type field struct {
  790. field reflect.StructField
  791. val reflect.Value
  792. }
  793. fields := []field{}
  794. for len(structs) > 0 {
  795. structVal := structs[0]
  796. structs = structs[1:]
  797. structType := structVal.Type()
  798. for i := 0; i < structType.NumField(); i++ {
  799. fieldType := structType.Field(i)
  800. fieldKind := fieldType.Type.Kind()
  801. // If "squash" is specified in the tag, we squash the field down.
  802. squash := false
  803. tagParts := strings.Split(fieldType.Tag.Get(d.config.TagName), ",")
  804. for _, tag := range tagParts[1:] {
  805. if tag == "squash" {
  806. squash = true
  807. break
  808. }
  809. }
  810. if squash {
  811. if fieldKind != reflect.Struct {
  812. errors = appendErrors(errors,
  813. fmt.Errorf("%s: unsupported type for squash: %s", fieldType.Name, fieldKind))
  814. } else {
  815. structs = append(structs, structVal.FieldByName(fieldType.Name))
  816. }
  817. continue
  818. }
  819. // Normal struct field, store it away
  820. fields = append(fields, field{fieldType, structVal.Field(i)})
  821. }
  822. }
  823. // for fieldType, field := range fields {
  824. for _, f := range fields {
  825. field, fieldValue := f.field, f.val
  826. fieldName := field.Name
  827. tagValue := field.Tag.Get(d.config.TagName)
  828. tagValue = strings.SplitN(tagValue, ",", 2)[0]
  829. if tagValue != "" {
  830. fieldName = tagValue
  831. }
  832. rawMapKey := reflect.ValueOf(fieldName)
  833. rawMapVal := dataVal.MapIndex(rawMapKey)
  834. if !rawMapVal.IsValid() {
  835. // Do a slower search by iterating over each key and
  836. // doing case-insensitive search.
  837. for dataValKey := range dataValKeys {
  838. mK, ok := dataValKey.Interface().(string)
  839. if !ok {
  840. // Not a string key
  841. continue
  842. }
  843. if strings.EqualFold(mK, fieldName) {
  844. rawMapKey = dataValKey
  845. rawMapVal = dataVal.MapIndex(dataValKey)
  846. break
  847. }
  848. }
  849. if !rawMapVal.IsValid() {
  850. // There was no matching key in the map for the value in
  851. // the struct. Just ignore.
  852. continue
  853. }
  854. }
  855. // Delete the key we're using from the unused map so we stop tracking
  856. delete(dataValKeysUnused, rawMapKey.Interface())
  857. if !fieldValue.IsValid() {
  858. // This should never happen
  859. panic("field is not valid")
  860. }
  861. // If we can't set the field, then it is unexported or something,
  862. // and we just continue onwards.
  863. if !fieldValue.CanSet() {
  864. continue
  865. }
  866. // If the name is empty string, then we're at the root, and we
  867. // don't dot-join the fields.
  868. if name != "" {
  869. fieldName = fmt.Sprintf("%s.%s", name, fieldName)
  870. }
  871. if err := d.decode(fieldName, rawMapVal.Interface(), fieldValue); err != nil {
  872. errors = appendErrors(errors, err)
  873. }
  874. }
  875. if d.config.ErrorUnused && len(dataValKeysUnused) > 0 {
  876. keys := make([]string, 0, len(dataValKeysUnused))
  877. for rawKey := range dataValKeysUnused {
  878. keys = append(keys, rawKey.(string))
  879. }
  880. sort.Strings(keys)
  881. err := fmt.Errorf("'%s' has invalid keys: %s", name, strings.Join(keys, ", "))
  882. errors = appendErrors(errors, err)
  883. }
  884. if len(errors) > 0 {
  885. return &Error{errors}
  886. }
  887. // Add the unused keys to the list of unused keys if we're tracking metadata
  888. if d.config.Metadata != nil {
  889. for rawKey := range dataValKeysUnused {
  890. key := rawKey.(string)
  891. if name != "" {
  892. key = fmt.Sprintf("%s.%s", name, key)
  893. }
  894. d.config.Metadata.Unused = append(d.config.Metadata.Unused, key)
  895. }
  896. }
  897. return nil
  898. }
  899. func getKind(val reflect.Value) reflect.Kind {
  900. kind := val.Kind()
  901. switch {
  902. case kind >= reflect.Int && kind <= reflect.Int64:
  903. return reflect.Int
  904. case kind >= reflect.Uint && kind <= reflect.Uint64:
  905. return reflect.Uint
  906. case kind >= reflect.Float32 && kind <= reflect.Float64:
  907. return reflect.Float32
  908. default:
  909. return kind
  910. }
  911. }