mapstructure.go 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. // Package mapstructure exposes functionality to convert one arbitrary
  2. // Go type into another, typically to convert a map[string]interface{}
  3. // into a native Go structure.
  4. //
  5. // The Go structure can be arbitrarily complex, containing slices,
  6. // other structs, etc. and the decoder will properly decode nested
  7. // maps and so on into the proper structures in the native Go struct.
  8. // See the examples to see what the decoder is capable of.
  9. //
  10. // The simplest function to start with is Decode.
  11. //
  12. // Field Tags
  13. //
  14. // When decoding to a struct, mapstructure will use the field name by
  15. // default to perform the mapping. For example, if a struct has a field
  16. // "Username" then mapstructure will look for a key in the source value
  17. // of "username" (case insensitive).
  18. //
  19. // type User struct {
  20. // Username string
  21. // }
  22. //
  23. // You can change the behavior of mapstructure by using struct tags.
  24. // The default struct tag that mapstructure looks for is "mapstructure"
  25. // but you can customize it using DecoderConfig.
  26. //
  27. // Renaming Fields
  28. //
  29. // To rename the key that mapstructure looks for, use the "mapstructure"
  30. // tag and set a value directly. For example, to change the "username" example
  31. // above to "user":
  32. //
  33. // type User struct {
  34. // Username string `mapstructure:"user"`
  35. // }
  36. //
  37. // Embedded Structs and Squashing
  38. //
  39. // Embedded structs are treated as if they're another field with that name.
  40. // By default, the two structs below are equivalent when decoding with
  41. // mapstructure:
  42. //
  43. // type Person struct {
  44. // Name string
  45. // }
  46. //
  47. // type Friend struct {
  48. // Person
  49. // }
  50. //
  51. // type Friend struct {
  52. // Person Person
  53. // }
  54. //
  55. // This would require an input that looks like below:
  56. //
  57. // map[string]interface{}{
  58. // "person": map[string]interface{}{"name": "alice"},
  59. // }
  60. //
  61. // If your "person" value is NOT nested, then you can append ",squash" to
  62. // your tag value and mapstructure will treat it as if the embedded struct
  63. // were part of the struct directly. Example:
  64. //
  65. // type Friend struct {
  66. // Person `mapstructure:",squash"`
  67. // }
  68. //
  69. // Now the following input would be accepted:
  70. //
  71. // map[string]interface{}{
  72. // "name": "alice",
  73. // }
  74. //
  75. // When decoding from a struct to a map, the squash tag squashes the struct
  76. // fields into a single map. Using the example structs from above:
  77. //
  78. // Friend{Person: Person{Name: "alice"}}
  79. //
  80. // Will be decoded into a map:
  81. //
  82. // map[string]interface{}{
  83. // "name": "alice",
  84. // }
  85. //
  86. // DecoderConfig has a field that changes the behavior of mapstructure
  87. // to always squash embedded structs.
  88. //
  89. // Remainder Values
  90. //
  91. // If there are any unmapped keys in the source value, mapstructure by
  92. // default will silently ignore them. You can error by setting ErrorUnused
  93. // in DecoderConfig. If you're using Metadata you can also maintain a slice
  94. // of the unused keys.
  95. //
  96. // You can also use the ",remain" suffix on your tag to collect all unused
  97. // values in a map. The field with this tag MUST be a map type and should
  98. // probably be a "map[string]interface{}" or "map[interface{}]interface{}".
  99. // See example below:
  100. //
  101. // type Friend struct {
  102. // Name string
  103. // Other map[string]interface{} `mapstructure:",remain"`
  104. // }
  105. //
  106. // Given the input below, Other would be populated with the other
  107. // values that weren't used (everything but "name"):
  108. //
  109. // map[string]interface{}{
  110. // "name": "bob",
  111. // "address": "123 Maple St.",
  112. // }
  113. //
  114. // Omit Empty Values
  115. //
  116. // When decoding from a struct to any other value, you may use the
  117. // ",omitempty" suffix on your tag to omit that value if it equates to
  118. // the zero value. The zero value of all types is specified in the Go
  119. // specification.
  120. //
  121. // For example, the zero type of a numeric type is zero ("0"). If the struct
  122. // field value is zero and a numeric type, the field is empty, and it won't
  123. // be encoded into the destination type.
  124. //
  125. // type Source struct {
  126. // Age int `mapstructure:",omitempty"`
  127. // }
  128. //
  129. // Unexported fields
  130. //
  131. // Since unexported (private) struct fields cannot be set outside the package
  132. // where they are defined, the decoder will simply skip them.
  133. //
  134. // For this output type definition:
  135. //
  136. // type Exported struct {
  137. // private string // this unexported field will be skipped
  138. // Public string
  139. // }
  140. //
  141. // Using this map as input:
  142. //
  143. // map[string]interface{}{
  144. // "private": "I will be ignored",
  145. // "Public": "I made it through!",
  146. // }
  147. //
  148. // The following struct will be decoded:
  149. //
  150. // type Exported struct {
  151. // private: "" // field is left with an empty string (zero value)
  152. // Public: "I made it through!"
  153. // }
  154. //
  155. // Other Configuration
  156. //
  157. // mapstructure is highly configurable. See the DecoderConfig struct
  158. // for other features and options that are supported.
  159. package mapstructure
  160. import (
  161. "encoding/json"
  162. "errors"
  163. "fmt"
  164. "reflect"
  165. "sort"
  166. "strconv"
  167. "strings"
  168. )
  169. // DecodeHookFunc is the callback function that can be used for
  170. // data transformations. See "DecodeHook" in the DecoderConfig
  171. // struct.
  172. //
  173. // The type must be one of DecodeHookFuncType, DecodeHookFuncKind, or
  174. // DecodeHookFuncValue.
  175. // Values are a superset of Types (Values can return types), and Types are a
  176. // superset of Kinds (Types can return Kinds) and are generally a richer thing
  177. // to use, but Kinds are simpler if you only need those.
  178. //
  179. // The reason DecodeHookFunc is multi-typed is for backwards compatibility:
  180. // we started with Kinds and then realized Types were the better solution,
  181. // but have a promise to not break backwards compat so we now support
  182. // both.
  183. type DecodeHookFunc interface{}
  184. // DecodeHookFuncType is a DecodeHookFunc which has complete information about
  185. // the source and target types.
  186. type DecodeHookFuncType func(reflect.Type, reflect.Type, interface{}) (interface{}, error)
  187. // DecodeHookFuncKind is a DecodeHookFunc which knows only the Kinds of the
  188. // source and target types.
  189. type DecodeHookFuncKind func(reflect.Kind, reflect.Kind, interface{}) (interface{}, error)
  190. // DecodeHookFuncValue is a DecodeHookFunc which has complete access to both the source and target
  191. // values.
  192. type DecodeHookFuncValue func(from reflect.Value, to reflect.Value) (interface{}, error)
  193. // DecoderConfig is the configuration that is used to create a new decoder
  194. // and allows customization of various aspects of decoding.
  195. type DecoderConfig struct {
  196. // DecodeHook, if set, will be called before any decoding and any
  197. // type conversion (if WeaklyTypedInput is on). This lets you modify
  198. // the values before they're set down onto the resulting struct. The
  199. // DecodeHook is called for every map and value in the input. This means
  200. // that if a struct has embedded fields with squash tags the decode hook
  201. // is called only once with all of the input data, not once for each
  202. // embedded struct.
  203. //
  204. // If an error is returned, the entire decode will fail with that error.
  205. DecodeHook DecodeHookFunc
  206. // If ErrorUnused is true, then it is an error for there to exist
  207. // keys in the original map that were unused in the decoding process
  208. // (extra keys).
  209. ErrorUnused bool
  210. // If ErrorUnset is true, then it is an error for there to exist
  211. // fields in the result that were not set in the decoding process
  212. // (extra fields). This only applies to decoding to a struct. This
  213. // will affect all nested structs as well.
  214. ErrorUnset bool
  215. // ZeroFields, if set to true, will zero fields before writing them.
  216. // For example, a map will be emptied before decoded values are put in
  217. // it. If this is false, a map will be merged.
  218. ZeroFields bool
  219. // If WeaklyTypedInput is true, the decoder will make the following
  220. // "weak" conversions:
  221. //
  222. // - bools to string (true = "1", false = "0")
  223. // - numbers to string (base 10)
  224. // - bools to int/uint (true = 1, false = 0)
  225. // - strings to int/uint (base implied by prefix)
  226. // - int to bool (true if value != 0)
  227. // - string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F,
  228. // FALSE, false, False. Anything else is an error)
  229. // - empty array = empty map and vice versa
  230. // - negative numbers to overflowed uint values (base 10)
  231. // - slice of maps to a merged map
  232. // - single values are converted to slices if required. Each
  233. // element is weakly decoded. For example: "4" can become []int{4}
  234. // if the target type is an int slice.
  235. //
  236. WeaklyTypedInput bool
  237. // Squash will squash embedded structs. A squash tag may also be
  238. // added to an individual struct field using a tag. For example:
  239. //
  240. // type Parent struct {
  241. // Child `mapstructure:",squash"`
  242. // }
  243. Squash bool
  244. // Metadata is the struct that will contain extra metadata about
  245. // the decoding. If this is nil, then no metadata will be tracked.
  246. Metadata *Metadata
  247. // Result is a pointer to the struct that will contain the decoded
  248. // value.
  249. Result interface{}
  250. // The tag name that mapstructure reads for field names. This
  251. // defaults to "mapstructure"
  252. TagName string
  253. // IgnoreUntaggedFields ignores all struct fields without explicit
  254. // TagName, comparable to `mapstructure:"-"` as default behaviour.
  255. IgnoreUntaggedFields bool
  256. // MatchName is the function used to match the map key to the struct
  257. // field name or tag. Defaults to `strings.EqualFold`. This can be used
  258. // to implement case-sensitive tag values, support snake casing, etc.
  259. MatchName func(mapKey, fieldName string) bool
  260. }
  261. // A Decoder takes a raw interface value and turns it into structured
  262. // data, keeping track of rich error information along the way in case
  263. // anything goes wrong. Unlike the basic top-level Decode method, you can
  264. // more finely control how the Decoder behaves using the DecoderConfig
  265. // structure. The top-level Decode method is just a convenience that sets
  266. // up the most basic Decoder.
  267. type Decoder struct {
  268. config *DecoderConfig
  269. }
  270. // Metadata contains information about decoding a structure that
  271. // is tedious or difficult to get otherwise.
  272. type Metadata struct {
  273. // Keys are the keys of the structure which were successfully decoded
  274. Keys []string
  275. // Unused is a slice of keys that were found in the raw value but
  276. // weren't decoded since there was no matching field in the result interface
  277. Unused []string
  278. // Unset is a slice of field names that were found in the result interface
  279. // but weren't set in the decoding process since there was no matching value
  280. // in the input
  281. Unset []string
  282. }
  283. // Decode takes an input structure and uses reflection to translate it to
  284. // the output structure. output must be a pointer to a map or struct.
  285. func Decode(input interface{}, output interface{}) error {
  286. config := &DecoderConfig{
  287. Metadata: nil,
  288. Result: output,
  289. }
  290. decoder, err := NewDecoder(config)
  291. if err != nil {
  292. return err
  293. }
  294. return decoder.Decode(input)
  295. }
  296. // WeakDecode is the same as Decode but is shorthand to enable
  297. // WeaklyTypedInput. See DecoderConfig for more info.
  298. func WeakDecode(input, output interface{}) error {
  299. config := &DecoderConfig{
  300. Metadata: nil,
  301. Result: output,
  302. WeaklyTypedInput: true,
  303. }
  304. decoder, err := NewDecoder(config)
  305. if err != nil {
  306. return err
  307. }
  308. return decoder.Decode(input)
  309. }
  310. // DecodeMetadata is the same as Decode, but is shorthand to
  311. // enable metadata collection. See DecoderConfig for more info.
  312. func DecodeMetadata(input interface{}, output interface{}, metadata *Metadata) error {
  313. config := &DecoderConfig{
  314. Metadata: metadata,
  315. Result: output,
  316. }
  317. decoder, err := NewDecoder(config)
  318. if err != nil {
  319. return err
  320. }
  321. return decoder.Decode(input)
  322. }
  323. // WeakDecodeMetadata is the same as Decode, but is shorthand to
  324. // enable both WeaklyTypedInput and metadata collection. See
  325. // DecoderConfig for more info.
  326. func WeakDecodeMetadata(input interface{}, output interface{}, metadata *Metadata) error {
  327. config := &DecoderConfig{
  328. Metadata: metadata,
  329. Result: output,
  330. WeaklyTypedInput: true,
  331. }
  332. decoder, err := NewDecoder(config)
  333. if err != nil {
  334. return err
  335. }
  336. return decoder.Decode(input)
  337. }
  338. // NewDecoder returns a new decoder for the given configuration. Once
  339. // a decoder has been returned, the same configuration must not be used
  340. // again.
  341. func NewDecoder(config *DecoderConfig) (*Decoder, error) {
  342. val := reflect.ValueOf(config.Result)
  343. if val.Kind() != reflect.Ptr {
  344. return nil, errors.New("result must be a pointer")
  345. }
  346. val = val.Elem()
  347. if !val.CanAddr() {
  348. return nil, errors.New("result must be addressable (a pointer)")
  349. }
  350. if config.Metadata != nil {
  351. if config.Metadata.Keys == nil {
  352. config.Metadata.Keys = make([]string, 0)
  353. }
  354. if config.Metadata.Unused == nil {
  355. config.Metadata.Unused = make([]string, 0)
  356. }
  357. if config.Metadata.Unset == nil {
  358. config.Metadata.Unset = make([]string, 0)
  359. }
  360. }
  361. if config.TagName == "" {
  362. config.TagName = "mapstructure"
  363. }
  364. if config.MatchName == nil {
  365. config.MatchName = strings.EqualFold
  366. }
  367. result := &Decoder{
  368. config: config,
  369. }
  370. return result, nil
  371. }
  372. // Decode decodes the given raw interface to the target pointer specified
  373. // by the configuration.
  374. func (d *Decoder) Decode(input interface{}) error {
  375. return d.decode("", input, reflect.ValueOf(d.config.Result).Elem())
  376. }
  377. // Decodes an unknown data type into a specific reflection value.
  378. func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) error {
  379. var inputVal reflect.Value
  380. if input != nil {
  381. inputVal = reflect.ValueOf(input)
  382. // We need to check here if input is a typed nil. Typed nils won't
  383. // match the "input == nil" below so we check that here.
  384. if inputVal.Kind() == reflect.Ptr && inputVal.IsNil() {
  385. input = nil
  386. }
  387. }
  388. if input == nil {
  389. // If the data is nil, then we don't set anything, unless ZeroFields is set
  390. // to true.
  391. if d.config.ZeroFields {
  392. outVal.Set(reflect.Zero(outVal.Type()))
  393. if d.config.Metadata != nil && name != "" {
  394. d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
  395. }
  396. }
  397. return nil
  398. }
  399. if !inputVal.IsValid() {
  400. // If the input value is invalid, then we just set the value
  401. // to be the zero value.
  402. outVal.Set(reflect.Zero(outVal.Type()))
  403. if d.config.Metadata != nil && name != "" {
  404. d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
  405. }
  406. return nil
  407. }
  408. if d.config.DecodeHook != nil {
  409. // We have a DecodeHook, so let's pre-process the input.
  410. var err error
  411. input, err = DecodeHookExec(d.config.DecodeHook, inputVal, outVal)
  412. if err != nil {
  413. return fmt.Errorf("error decoding '%s': %s", name, err)
  414. }
  415. }
  416. var err error
  417. outputKind := getKind(outVal)
  418. addMetaKey := true
  419. switch outputKind {
  420. case reflect.Bool:
  421. err = d.decodeBool(name, input, outVal)
  422. case reflect.Interface:
  423. err = d.decodeBasic(name, input, outVal)
  424. case reflect.String:
  425. err = d.decodeString(name, input, outVal)
  426. case reflect.Int:
  427. err = d.decodeInt(name, input, outVal)
  428. case reflect.Uint:
  429. err = d.decodeUint(name, input, outVal)
  430. case reflect.Float32:
  431. err = d.decodeFloat(name, input, outVal)
  432. case reflect.Struct:
  433. err = d.decodeStruct(name, input, outVal)
  434. case reflect.Map:
  435. err = d.decodeMap(name, input, outVal)
  436. case reflect.Ptr:
  437. addMetaKey, err = d.decodePtr(name, input, outVal)
  438. case reflect.Slice:
  439. err = d.decodeSlice(name, input, outVal)
  440. case reflect.Array:
  441. err = d.decodeArray(name, input, outVal)
  442. case reflect.Func:
  443. err = d.decodeFunc(name, input, outVal)
  444. default:
  445. // If we reached this point then we weren't able to decode it
  446. return fmt.Errorf("%s: unsupported type: %s", name, outputKind)
  447. }
  448. // If we reached here, then we successfully decoded SOMETHING, so
  449. // mark the key as used if we're tracking metainput.
  450. if addMetaKey && d.config.Metadata != nil && name != "" {
  451. d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
  452. }
  453. return err
  454. }
  455. // This decodes a basic type (bool, int, string, etc.) and sets the
  456. // value to "data" of that type.
  457. func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value) error {
  458. if val.IsValid() && val.Elem().IsValid() {
  459. elem := val.Elem()
  460. // If we can't address this element, then its not writable. Instead,
  461. // we make a copy of the value (which is a pointer and therefore
  462. // writable), decode into that, and replace the whole value.
  463. copied := false
  464. if !elem.CanAddr() {
  465. copied = true
  466. // Make *T
  467. copy := reflect.New(elem.Type())
  468. // *T = elem
  469. copy.Elem().Set(elem)
  470. // Set elem so we decode into it
  471. elem = copy
  472. }
  473. // Decode. If we have an error then return. We also return right
  474. // away if we're not a copy because that means we decoded directly.
  475. if err := d.decode(name, data, elem); err != nil || !copied {
  476. return err
  477. }
  478. // If we're a copy, we need to set te final result
  479. val.Set(elem.Elem())
  480. return nil
  481. }
  482. dataVal := reflect.ValueOf(data)
  483. // If the input data is a pointer, and the assigned type is the dereference
  484. // of that exact pointer, then indirect it so that we can assign it.
  485. // Example: *string to string
  486. if dataVal.Kind() == reflect.Ptr && dataVal.Type().Elem() == val.Type() {
  487. dataVal = reflect.Indirect(dataVal)
  488. }
  489. if !dataVal.IsValid() {
  490. dataVal = reflect.Zero(val.Type())
  491. }
  492. dataValType := dataVal.Type()
  493. if !dataValType.AssignableTo(val.Type()) {
  494. return fmt.Errorf(
  495. "'%s' expected type '%s', got '%s'",
  496. name, val.Type(), dataValType)
  497. }
  498. val.Set(dataVal)
  499. return nil
  500. }
  501. func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value) error {
  502. dataVal := reflect.Indirect(reflect.ValueOf(data))
  503. dataKind := getKind(dataVal)
  504. converted := true
  505. switch {
  506. case dataKind == reflect.String:
  507. val.SetString(dataVal.String())
  508. case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
  509. if dataVal.Bool() {
  510. val.SetString("1")
  511. } else {
  512. val.SetString("0")
  513. }
  514. case dataKind == reflect.Int && d.config.WeaklyTypedInput:
  515. val.SetString(strconv.FormatInt(dataVal.Int(), 10))
  516. case dataKind == reflect.Uint && d.config.WeaklyTypedInput:
  517. val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
  518. case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
  519. val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64))
  520. case dataKind == reflect.Slice && d.config.WeaklyTypedInput,
  521. dataKind == reflect.Array && d.config.WeaklyTypedInput:
  522. dataType := dataVal.Type()
  523. elemKind := dataType.Elem().Kind()
  524. switch elemKind {
  525. case reflect.Uint8:
  526. var uints []uint8
  527. if dataKind == reflect.Array {
  528. uints = make([]uint8, dataVal.Len(), dataVal.Len())
  529. for i := range uints {
  530. uints[i] = dataVal.Index(i).Interface().(uint8)
  531. }
  532. } else {
  533. uints = dataVal.Interface().([]uint8)
  534. }
  535. val.SetString(string(uints))
  536. default:
  537. converted = false
  538. }
  539. default:
  540. converted = false
  541. }
  542. if !converted {
  543. return fmt.Errorf(
  544. "'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
  545. name, val.Type(), dataVal.Type(), data)
  546. }
  547. return nil
  548. }
  549. func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error {
  550. dataVal := reflect.Indirect(reflect.ValueOf(data))
  551. dataKind := getKind(dataVal)
  552. dataType := dataVal.Type()
  553. switch {
  554. case dataKind == reflect.Int:
  555. val.SetInt(dataVal.Int())
  556. case dataKind == reflect.Uint:
  557. val.SetInt(int64(dataVal.Uint()))
  558. case dataKind == reflect.Float32:
  559. val.SetInt(int64(dataVal.Float()))
  560. case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
  561. if dataVal.Bool() {
  562. val.SetInt(1)
  563. } else {
  564. val.SetInt(0)
  565. }
  566. case dataKind == reflect.String && d.config.WeaklyTypedInput:
  567. str := dataVal.String()
  568. if str == "" {
  569. str = "0"
  570. }
  571. i, err := strconv.ParseInt(str, 0, val.Type().Bits())
  572. if err == nil {
  573. val.SetInt(i)
  574. } else {
  575. return fmt.Errorf("cannot parse '%s' as int: %s", name, err)
  576. }
  577. case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number":
  578. jn := data.(json.Number)
  579. i, err := jn.Int64()
  580. if err != nil {
  581. return fmt.Errorf(
  582. "error decoding json.Number into %s: %s", name, err)
  583. }
  584. val.SetInt(i)
  585. default:
  586. return fmt.Errorf(
  587. "'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
  588. name, val.Type(), dataVal.Type(), data)
  589. }
  590. return nil
  591. }
  592. func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error {
  593. dataVal := reflect.Indirect(reflect.ValueOf(data))
  594. dataKind := getKind(dataVal)
  595. dataType := dataVal.Type()
  596. switch {
  597. case dataKind == reflect.Int:
  598. i := dataVal.Int()
  599. if i < 0 && !d.config.WeaklyTypedInput {
  600. return fmt.Errorf("cannot parse '%s', %d overflows uint",
  601. name, i)
  602. }
  603. val.SetUint(uint64(i))
  604. case dataKind == reflect.Uint:
  605. val.SetUint(dataVal.Uint())
  606. case dataKind == reflect.Float32:
  607. f := dataVal.Float()
  608. if f < 0 && !d.config.WeaklyTypedInput {
  609. return fmt.Errorf("cannot parse '%s', %f overflows uint",
  610. name, f)
  611. }
  612. val.SetUint(uint64(f))
  613. case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
  614. if dataVal.Bool() {
  615. val.SetUint(1)
  616. } else {
  617. val.SetUint(0)
  618. }
  619. case dataKind == reflect.String && d.config.WeaklyTypedInput:
  620. str := dataVal.String()
  621. if str == "" {
  622. str = "0"
  623. }
  624. i, err := strconv.ParseUint(str, 0, val.Type().Bits())
  625. if err == nil {
  626. val.SetUint(i)
  627. } else {
  628. return fmt.Errorf("cannot parse '%s' as uint: %s", name, err)
  629. }
  630. case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number":
  631. jn := data.(json.Number)
  632. i, err := strconv.ParseUint(string(jn), 0, 64)
  633. if err != nil {
  634. return fmt.Errorf(
  635. "error decoding json.Number into %s: %s", name, err)
  636. }
  637. val.SetUint(i)
  638. default:
  639. return fmt.Errorf(
  640. "'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
  641. name, val.Type(), dataVal.Type(), data)
  642. }
  643. return nil
  644. }
  645. func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error {
  646. dataVal := reflect.Indirect(reflect.ValueOf(data))
  647. dataKind := getKind(dataVal)
  648. switch {
  649. case dataKind == reflect.Bool:
  650. val.SetBool(dataVal.Bool())
  651. case dataKind == reflect.Int && d.config.WeaklyTypedInput:
  652. val.SetBool(dataVal.Int() != 0)
  653. case dataKind == reflect.Uint && d.config.WeaklyTypedInput:
  654. val.SetBool(dataVal.Uint() != 0)
  655. case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
  656. val.SetBool(dataVal.Float() != 0)
  657. case dataKind == reflect.String && d.config.WeaklyTypedInput:
  658. b, err := strconv.ParseBool(dataVal.String())
  659. if err == nil {
  660. val.SetBool(b)
  661. } else if dataVal.String() == "" {
  662. val.SetBool(false)
  663. } else {
  664. return fmt.Errorf("cannot parse '%s' as bool: %s", name, err)
  665. }
  666. default:
  667. return fmt.Errorf(
  668. "'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
  669. name, val.Type(), dataVal.Type(), data)
  670. }
  671. return nil
  672. }
  673. func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error {
  674. dataVal := reflect.Indirect(reflect.ValueOf(data))
  675. dataKind := getKind(dataVal)
  676. dataType := dataVal.Type()
  677. switch {
  678. case dataKind == reflect.Int:
  679. val.SetFloat(float64(dataVal.Int()))
  680. case dataKind == reflect.Uint:
  681. val.SetFloat(float64(dataVal.Uint()))
  682. case dataKind == reflect.Float32:
  683. val.SetFloat(dataVal.Float())
  684. case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
  685. if dataVal.Bool() {
  686. val.SetFloat(1)
  687. } else {
  688. val.SetFloat(0)
  689. }
  690. case dataKind == reflect.String && d.config.WeaklyTypedInput:
  691. str := dataVal.String()
  692. if str == "" {
  693. str = "0"
  694. }
  695. f, err := strconv.ParseFloat(str, val.Type().Bits())
  696. if err == nil {
  697. val.SetFloat(f)
  698. } else {
  699. return fmt.Errorf("cannot parse '%s' as float: %s", name, err)
  700. }
  701. case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number":
  702. jn := data.(json.Number)
  703. i, err := jn.Float64()
  704. if err != nil {
  705. return fmt.Errorf(
  706. "error decoding json.Number into %s: %s", name, err)
  707. }
  708. val.SetFloat(i)
  709. default:
  710. return fmt.Errorf(
  711. "'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
  712. name, val.Type(), dataVal.Type(), data)
  713. }
  714. return nil
  715. }
  716. func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) error {
  717. valType := val.Type()
  718. valKeyType := valType.Key()
  719. valElemType := valType.Elem()
  720. // By default we overwrite keys in the current map
  721. valMap := val
  722. // If the map is nil or we're purposely zeroing fields, make a new map
  723. if valMap.IsNil() || d.config.ZeroFields {
  724. // Make a new map to hold our result
  725. mapType := reflect.MapOf(valKeyType, valElemType)
  726. valMap = reflect.MakeMap(mapType)
  727. }
  728. // Check input type and based on the input type jump to the proper func
  729. dataVal := reflect.Indirect(reflect.ValueOf(data))
  730. switch dataVal.Kind() {
  731. case reflect.Map:
  732. return d.decodeMapFromMap(name, dataVal, val, valMap)
  733. case reflect.Struct:
  734. return d.decodeMapFromStruct(name, dataVal, val, valMap)
  735. case reflect.Array, reflect.Slice:
  736. if d.config.WeaklyTypedInput {
  737. return d.decodeMapFromSlice(name, dataVal, val, valMap)
  738. }
  739. fallthrough
  740. default:
  741. return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind())
  742. }
  743. }
  744. func (d *Decoder) decodeMapFromSlice(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
  745. // Special case for BC reasons (covered by tests)
  746. if dataVal.Len() == 0 {
  747. val.Set(valMap)
  748. return nil
  749. }
  750. for i := 0; i < dataVal.Len(); i++ {
  751. err := d.decode(
  752. name+"["+strconv.Itoa(i)+"]",
  753. dataVal.Index(i).Interface(), val)
  754. if err != nil {
  755. return err
  756. }
  757. }
  758. return nil
  759. }
  760. func (d *Decoder) decodeMapFromMap(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
  761. valType := val.Type()
  762. valKeyType := valType.Key()
  763. valElemType := valType.Elem()
  764. // Accumulate errors
  765. errors := make([]string, 0)
  766. // If the input data is empty, then we just match what the input data is.
  767. if dataVal.Len() == 0 {
  768. if dataVal.IsNil() {
  769. if !val.IsNil() {
  770. val.Set(dataVal)
  771. }
  772. } else {
  773. // Set to empty allocated value
  774. val.Set(valMap)
  775. }
  776. return nil
  777. }
  778. for _, k := range dataVal.MapKeys() {
  779. fieldName := name + "[" + k.String() + "]"
  780. // First decode the key into the proper type
  781. currentKey := reflect.Indirect(reflect.New(valKeyType))
  782. if err := d.decode(fieldName, k.Interface(), currentKey); err != nil {
  783. errors = appendErrors(errors, err)
  784. continue
  785. }
  786. // Next decode the data into the proper type
  787. v := dataVal.MapIndex(k).Interface()
  788. currentVal := reflect.Indirect(reflect.New(valElemType))
  789. if err := d.decode(fieldName, v, currentVal); err != nil {
  790. errors = appendErrors(errors, err)
  791. continue
  792. }
  793. valMap.SetMapIndex(currentKey, currentVal)
  794. }
  795. // Set the built up map to the value
  796. val.Set(valMap)
  797. // If we had errors, return those
  798. if len(errors) > 0 {
  799. return &Error{errors}
  800. }
  801. return nil
  802. }
  803. func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
  804. typ := dataVal.Type()
  805. for i := 0; i < typ.NumField(); i++ {
  806. // Get the StructField first since this is a cheap operation. If the
  807. // field is unexported, then ignore it.
  808. f := typ.Field(i)
  809. if f.PkgPath != "" {
  810. continue
  811. }
  812. // Next get the actual value of this field and verify it is assignable
  813. // to the map value.
  814. v := dataVal.Field(i)
  815. if !v.Type().AssignableTo(valMap.Type().Elem()) {
  816. return fmt.Errorf("cannot assign type '%s' to map value field of type '%s'", v.Type(), valMap.Type().Elem())
  817. }
  818. tagValue := f.Tag.Get(d.config.TagName)
  819. keyName := f.Name
  820. if tagValue == "" && d.config.IgnoreUntaggedFields {
  821. continue
  822. }
  823. // If Squash is set in the config, we squash the field down.
  824. squash := d.config.Squash && v.Kind() == reflect.Struct && f.Anonymous
  825. v = dereferencePtrToStructIfNeeded(v, d.config.TagName)
  826. // Determine the name of the key in the map
  827. if index := strings.Index(tagValue, ","); index != -1 {
  828. if tagValue[:index] == "-" {
  829. continue
  830. }
  831. // If "omitempty" is specified in the tag, it ignores empty values.
  832. if strings.Index(tagValue[index+1:], "omitempty") != -1 && isEmptyValue(v) {
  833. continue
  834. }
  835. // If "squash" is specified in the tag, we squash the field down.
  836. squash = squash || strings.Index(tagValue[index+1:], "squash") != -1
  837. if squash {
  838. // When squashing, the embedded type can be a pointer to a struct.
  839. if v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct {
  840. v = v.Elem()
  841. }
  842. // The final type must be a struct
  843. if v.Kind() != reflect.Struct {
  844. return fmt.Errorf("cannot squash non-struct type '%s'", v.Type())
  845. }
  846. }
  847. if keyNameTagValue := tagValue[:index]; keyNameTagValue != "" {
  848. keyName = keyNameTagValue
  849. }
  850. } else if len(tagValue) > 0 {
  851. if tagValue == "-" {
  852. continue
  853. }
  854. keyName = tagValue
  855. }
  856. switch v.Kind() {
  857. // this is an embedded struct, so handle it differently
  858. case reflect.Struct:
  859. x := reflect.New(v.Type())
  860. x.Elem().Set(v)
  861. vType := valMap.Type()
  862. vKeyType := vType.Key()
  863. vElemType := vType.Elem()
  864. mType := reflect.MapOf(vKeyType, vElemType)
  865. vMap := reflect.MakeMap(mType)
  866. // Creating a pointer to a map so that other methods can completely
  867. // overwrite the map if need be (looking at you decodeMapFromMap). The
  868. // indirection allows the underlying map to be settable (CanSet() == true)
  869. // where as reflect.MakeMap returns an unsettable map.
  870. addrVal := reflect.New(vMap.Type())
  871. reflect.Indirect(addrVal).Set(vMap)
  872. err := d.decode(keyName, x.Interface(), reflect.Indirect(addrVal))
  873. if err != nil {
  874. return err
  875. }
  876. // the underlying map may have been completely overwritten so pull
  877. // it indirectly out of the enclosing value.
  878. vMap = reflect.Indirect(addrVal)
  879. if squash {
  880. for _, k := range vMap.MapKeys() {
  881. valMap.SetMapIndex(k, vMap.MapIndex(k))
  882. }
  883. } else {
  884. valMap.SetMapIndex(reflect.ValueOf(keyName), vMap)
  885. }
  886. default:
  887. valMap.SetMapIndex(reflect.ValueOf(keyName), v)
  888. }
  889. }
  890. if val.CanAddr() {
  891. val.Set(valMap)
  892. }
  893. return nil
  894. }
  895. func (d *Decoder) decodePtr(name string, data interface{}, val reflect.Value) (bool, error) {
  896. // If the input data is nil, then we want to just set the output
  897. // pointer to be nil as well.
  898. isNil := data == nil
  899. if !isNil {
  900. switch v := reflect.Indirect(reflect.ValueOf(data)); v.Kind() {
  901. case reflect.Chan,
  902. reflect.Func,
  903. reflect.Interface,
  904. reflect.Map,
  905. reflect.Ptr,
  906. reflect.Slice:
  907. isNil = v.IsNil()
  908. }
  909. }
  910. if isNil {
  911. if !val.IsNil() && val.CanSet() {
  912. nilValue := reflect.New(val.Type()).Elem()
  913. val.Set(nilValue)
  914. }
  915. return true, nil
  916. }
  917. // Create an element of the concrete (non pointer) type and decode
  918. // into that. Then set the value of the pointer to this type.
  919. valType := val.Type()
  920. valElemType := valType.Elem()
  921. if val.CanSet() {
  922. realVal := val
  923. if realVal.IsNil() || d.config.ZeroFields {
  924. realVal = reflect.New(valElemType)
  925. }
  926. if err := d.decode(name, data, reflect.Indirect(realVal)); err != nil {
  927. return false, err
  928. }
  929. val.Set(realVal)
  930. } else {
  931. if err := d.decode(name, data, reflect.Indirect(val)); err != nil {
  932. return false, err
  933. }
  934. }
  935. return false, nil
  936. }
  937. func (d *Decoder) decodeFunc(name string, data interface{}, val reflect.Value) error {
  938. // Create an element of the concrete (non pointer) type and decode
  939. // into that. Then set the value of the pointer to this type.
  940. dataVal := reflect.Indirect(reflect.ValueOf(data))
  941. if val.Type() != dataVal.Type() {
  942. return fmt.Errorf(
  943. "'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
  944. name, val.Type(), dataVal.Type(), data)
  945. }
  946. val.Set(dataVal)
  947. return nil
  948. }
  949. func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value) error {
  950. dataVal := reflect.Indirect(reflect.ValueOf(data))
  951. dataValKind := dataVal.Kind()
  952. valType := val.Type()
  953. valElemType := valType.Elem()
  954. sliceType := reflect.SliceOf(valElemType)
  955. // If we have a non array/slice type then we first attempt to convert.
  956. if dataValKind != reflect.Array && dataValKind != reflect.Slice {
  957. if d.config.WeaklyTypedInput {
  958. switch {
  959. // Slice and array we use the normal logic
  960. case dataValKind == reflect.Slice, dataValKind == reflect.Array:
  961. break
  962. // Empty maps turn into empty slices
  963. case dataValKind == reflect.Map:
  964. if dataVal.Len() == 0 {
  965. val.Set(reflect.MakeSlice(sliceType, 0, 0))
  966. return nil
  967. }
  968. // Create slice of maps of other sizes
  969. return d.decodeSlice(name, []interface{}{data}, val)
  970. case dataValKind == reflect.String && valElemType.Kind() == reflect.Uint8:
  971. return d.decodeSlice(name, []byte(dataVal.String()), val)
  972. // All other types we try to convert to the slice type
  973. // and "lift" it into it. i.e. a string becomes a string slice.
  974. default:
  975. // Just re-try this function with data as a slice.
  976. return d.decodeSlice(name, []interface{}{data}, val)
  977. }
  978. }
  979. return fmt.Errorf(
  980. "'%s': source data must be an array or slice, got %s", name, dataValKind)
  981. }
  982. // If the input value is nil, then don't allocate since empty != nil
  983. if dataValKind != reflect.Array && dataVal.IsNil() {
  984. return nil
  985. }
  986. valSlice := val
  987. if valSlice.IsNil() || d.config.ZeroFields {
  988. // Make a new slice to hold our result, same size as the original data.
  989. valSlice = reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len())
  990. }
  991. // Accumulate any errors
  992. errors := make([]string, 0)
  993. for i := 0; i < dataVal.Len(); i++ {
  994. currentData := dataVal.Index(i).Interface()
  995. for valSlice.Len() <= i {
  996. valSlice = reflect.Append(valSlice, reflect.Zero(valElemType))
  997. }
  998. currentField := valSlice.Index(i)
  999. fieldName := name + "[" + strconv.Itoa(i) + "]"
  1000. if err := d.decode(fieldName, currentData, currentField); err != nil {
  1001. errors = appendErrors(errors, err)
  1002. }
  1003. }
  1004. // Finally, set the value to the slice we built up
  1005. val.Set(valSlice)
  1006. // If there were errors, we return those
  1007. if len(errors) > 0 {
  1008. return &Error{errors}
  1009. }
  1010. return nil
  1011. }
  1012. func (d *Decoder) decodeArray(name string, data interface{}, val reflect.Value) error {
  1013. dataVal := reflect.Indirect(reflect.ValueOf(data))
  1014. dataValKind := dataVal.Kind()
  1015. valType := val.Type()
  1016. valElemType := valType.Elem()
  1017. arrayType := reflect.ArrayOf(valType.Len(), valElemType)
  1018. valArray := val
  1019. if valArray.Interface() == reflect.Zero(valArray.Type()).Interface() || d.config.ZeroFields {
  1020. // Check input type
  1021. if dataValKind != reflect.Array && dataValKind != reflect.Slice {
  1022. if d.config.WeaklyTypedInput {
  1023. switch {
  1024. // Empty maps turn into empty arrays
  1025. case dataValKind == reflect.Map:
  1026. if dataVal.Len() == 0 {
  1027. val.Set(reflect.Zero(arrayType))
  1028. return nil
  1029. }
  1030. // All other types we try to convert to the array type
  1031. // and "lift" it into it. i.e. a string becomes a string array.
  1032. default:
  1033. // Just re-try this function with data as a slice.
  1034. return d.decodeArray(name, []interface{}{data}, val)
  1035. }
  1036. }
  1037. return fmt.Errorf(
  1038. "'%s': source data must be an array or slice, got %s", name, dataValKind)
  1039. }
  1040. if dataVal.Len() > arrayType.Len() {
  1041. return fmt.Errorf(
  1042. "'%s': expected source data to have length less or equal to %d, got %d", name, arrayType.Len(), dataVal.Len())
  1043. }
  1044. // Make a new array to hold our result, same size as the original data.
  1045. valArray = reflect.New(arrayType).Elem()
  1046. }
  1047. // Accumulate any errors
  1048. errors := make([]string, 0)
  1049. for i := 0; i < dataVal.Len(); i++ {
  1050. currentData := dataVal.Index(i).Interface()
  1051. currentField := valArray.Index(i)
  1052. fieldName := name + "[" + strconv.Itoa(i) + "]"
  1053. if err := d.decode(fieldName, currentData, currentField); err != nil {
  1054. errors = appendErrors(errors, err)
  1055. }
  1056. }
  1057. // Finally, set the value to the array we built up
  1058. val.Set(valArray)
  1059. // If there were errors, we return those
  1060. if len(errors) > 0 {
  1061. return &Error{errors}
  1062. }
  1063. return nil
  1064. }
  1065. func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value) error {
  1066. dataVal := reflect.Indirect(reflect.ValueOf(data))
  1067. // If the type of the value to write to and the data match directly,
  1068. // then we just set it directly instead of recursing into the structure.
  1069. if dataVal.Type() == val.Type() {
  1070. val.Set(dataVal)
  1071. return nil
  1072. }
  1073. dataValKind := dataVal.Kind()
  1074. switch dataValKind {
  1075. case reflect.Map:
  1076. return d.decodeStructFromMap(name, dataVal, val)
  1077. case reflect.Struct:
  1078. // Not the most efficient way to do this but we can optimize later if
  1079. // we want to. To convert from struct to struct we go to map first
  1080. // as an intermediary.
  1081. // Make a new map to hold our result
  1082. mapType := reflect.TypeOf((map[string]interface{})(nil))
  1083. mval := reflect.MakeMap(mapType)
  1084. // Creating a pointer to a map so that other methods can completely
  1085. // overwrite the map if need be (looking at you decodeMapFromMap). The
  1086. // indirection allows the underlying map to be settable (CanSet() == true)
  1087. // where as reflect.MakeMap returns an unsettable map.
  1088. addrVal := reflect.New(mval.Type())
  1089. reflect.Indirect(addrVal).Set(mval)
  1090. if err := d.decodeMapFromStruct(name, dataVal, reflect.Indirect(addrVal), mval); err != nil {
  1091. return err
  1092. }
  1093. result := d.decodeStructFromMap(name, reflect.Indirect(addrVal), val)
  1094. return result
  1095. default:
  1096. return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind())
  1097. }
  1098. }
  1099. func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) error {
  1100. dataValType := dataVal.Type()
  1101. if kind := dataValType.Key().Kind(); kind != reflect.String && kind != reflect.Interface {
  1102. return fmt.Errorf(
  1103. "'%s' needs a map with string keys, has '%s' keys",
  1104. name, dataValType.Key().Kind())
  1105. }
  1106. dataValKeys := make(map[reflect.Value]struct{})
  1107. dataValKeysUnused := make(map[interface{}]struct{})
  1108. for _, dataValKey := range dataVal.MapKeys() {
  1109. dataValKeys[dataValKey] = struct{}{}
  1110. dataValKeysUnused[dataValKey.Interface()] = struct{}{}
  1111. }
  1112. targetValKeysUnused := make(map[interface{}]struct{})
  1113. errors := make([]string, 0)
  1114. // This slice will keep track of all the structs we'll be decoding.
  1115. // There can be more than one struct if there are embedded structs
  1116. // that are squashed.
  1117. structs := make([]reflect.Value, 1, 5)
  1118. structs[0] = val
  1119. // Compile the list of all the fields that we're going to be decoding
  1120. // from all the structs.
  1121. type field struct {
  1122. field reflect.StructField
  1123. val reflect.Value
  1124. }
  1125. // remainField is set to a valid field set with the "remain" tag if
  1126. // we are keeping track of remaining values.
  1127. var remainField *field
  1128. fields := []field{}
  1129. for len(structs) > 0 {
  1130. structVal := structs[0]
  1131. structs = structs[1:]
  1132. structType := structVal.Type()
  1133. for i := 0; i < structType.NumField(); i++ {
  1134. fieldType := structType.Field(i)
  1135. fieldVal := structVal.Field(i)
  1136. if fieldVal.Kind() == reflect.Ptr && fieldVal.Elem().Kind() == reflect.Struct {
  1137. // Handle embedded struct pointers as embedded structs.
  1138. fieldVal = fieldVal.Elem()
  1139. }
  1140. // If "squash" is specified in the tag, we squash the field down.
  1141. squash := d.config.Squash && fieldVal.Kind() == reflect.Struct && fieldType.Anonymous
  1142. remain := false
  1143. // We always parse the tags cause we're looking for other tags too
  1144. tagParts := strings.Split(fieldType.Tag.Get(d.config.TagName), ",")
  1145. for _, tag := range tagParts[1:] {
  1146. if tag == "squash" {
  1147. squash = true
  1148. break
  1149. }
  1150. if tag == "remain" {
  1151. remain = true
  1152. break
  1153. }
  1154. }
  1155. if squash {
  1156. if fieldVal.Kind() != reflect.Struct {
  1157. errors = appendErrors(errors,
  1158. fmt.Errorf("%s: unsupported type for squash: %s", fieldType.Name, fieldVal.Kind()))
  1159. } else {
  1160. structs = append(structs, fieldVal)
  1161. }
  1162. continue
  1163. }
  1164. // Build our field
  1165. if remain {
  1166. remainField = &field{fieldType, fieldVal}
  1167. } else {
  1168. // Normal struct field, store it away
  1169. fields = append(fields, field{fieldType, fieldVal})
  1170. }
  1171. }
  1172. }
  1173. // for fieldType, field := range fields {
  1174. for _, f := range fields {
  1175. field, fieldValue := f.field, f.val
  1176. fieldName := field.Name
  1177. tagValue := field.Tag.Get(d.config.TagName)
  1178. tagValue = strings.SplitN(tagValue, ",", 2)[0]
  1179. if tagValue != "" {
  1180. fieldName = tagValue
  1181. }
  1182. rawMapKey := reflect.ValueOf(fieldName)
  1183. rawMapVal := dataVal.MapIndex(rawMapKey)
  1184. if !rawMapVal.IsValid() {
  1185. // Do a slower search by iterating over each key and
  1186. // doing case-insensitive search.
  1187. for dataValKey := range dataValKeys {
  1188. mK, ok := dataValKey.Interface().(string)
  1189. if !ok {
  1190. // Not a string key
  1191. continue
  1192. }
  1193. if d.config.MatchName(mK, fieldName) {
  1194. rawMapKey = dataValKey
  1195. rawMapVal = dataVal.MapIndex(dataValKey)
  1196. break
  1197. }
  1198. }
  1199. if !rawMapVal.IsValid() {
  1200. // There was no matching key in the map for the value in
  1201. // the struct. Remember it for potential errors and metadata.
  1202. targetValKeysUnused[fieldName] = struct{}{}
  1203. continue
  1204. }
  1205. }
  1206. if !fieldValue.IsValid() {
  1207. // This should never happen
  1208. panic("field is not valid")
  1209. }
  1210. // If we can't set the field, then it is unexported or something,
  1211. // and we just continue onwards.
  1212. if !fieldValue.CanSet() {
  1213. continue
  1214. }
  1215. // Delete the key we're using from the unused map so we stop tracking
  1216. delete(dataValKeysUnused, rawMapKey.Interface())
  1217. // If the name is empty string, then we're at the root, and we
  1218. // don't dot-join the fields.
  1219. if name != "" {
  1220. fieldName = name + "." + fieldName
  1221. }
  1222. if err := d.decode(fieldName, rawMapVal.Interface(), fieldValue); err != nil {
  1223. errors = appendErrors(errors, err)
  1224. }
  1225. }
  1226. // If we have a "remain"-tagged field and we have unused keys then
  1227. // we put the unused keys directly into the remain field.
  1228. if remainField != nil && len(dataValKeysUnused) > 0 {
  1229. // Build a map of only the unused values
  1230. remain := map[interface{}]interface{}{}
  1231. for key := range dataValKeysUnused {
  1232. remain[key] = dataVal.MapIndex(reflect.ValueOf(key)).Interface()
  1233. }
  1234. // Decode it as-if we were just decoding this map onto our map.
  1235. if err := d.decodeMap(name, remain, remainField.val); err != nil {
  1236. errors = appendErrors(errors, err)
  1237. }
  1238. // Set the map to nil so we have none so that the next check will
  1239. // not error (ErrorUnused)
  1240. dataValKeysUnused = nil
  1241. }
  1242. if d.config.ErrorUnused && len(dataValKeysUnused) > 0 {
  1243. keys := make([]string, 0, len(dataValKeysUnused))
  1244. for rawKey := range dataValKeysUnused {
  1245. keys = append(keys, rawKey.(string))
  1246. }
  1247. sort.Strings(keys)
  1248. err := fmt.Errorf("'%s' has invalid keys: %s", name, strings.Join(keys, ", "))
  1249. errors = appendErrors(errors, err)
  1250. }
  1251. if d.config.ErrorUnset && len(targetValKeysUnused) > 0 {
  1252. keys := make([]string, 0, len(targetValKeysUnused))
  1253. for rawKey := range targetValKeysUnused {
  1254. keys = append(keys, rawKey.(string))
  1255. }
  1256. sort.Strings(keys)
  1257. err := fmt.Errorf("'%s' has unset fields: %s", name, strings.Join(keys, ", "))
  1258. errors = appendErrors(errors, err)
  1259. }
  1260. if len(errors) > 0 {
  1261. return &Error{errors}
  1262. }
  1263. // Add the unused keys to the list of unused keys if we're tracking metadata
  1264. if d.config.Metadata != nil {
  1265. for rawKey := range dataValKeysUnused {
  1266. key := rawKey.(string)
  1267. if name != "" {
  1268. key = name + "." + key
  1269. }
  1270. d.config.Metadata.Unused = append(d.config.Metadata.Unused, key)
  1271. }
  1272. for rawKey := range targetValKeysUnused {
  1273. key := rawKey.(string)
  1274. if name != "" {
  1275. key = name + "." + key
  1276. }
  1277. d.config.Metadata.Unset = append(d.config.Metadata.Unset, key)
  1278. }
  1279. }
  1280. return nil
  1281. }
  1282. func isEmptyValue(v reflect.Value) bool {
  1283. switch getKind(v) {
  1284. case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
  1285. return v.Len() == 0
  1286. case reflect.Bool:
  1287. return !v.Bool()
  1288. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1289. return v.Int() == 0
  1290. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1291. return v.Uint() == 0
  1292. case reflect.Float32, reflect.Float64:
  1293. return v.Float() == 0
  1294. case reflect.Interface, reflect.Ptr:
  1295. return v.IsNil()
  1296. }
  1297. return false
  1298. }
  1299. func getKind(val reflect.Value) reflect.Kind {
  1300. kind := val.Kind()
  1301. switch {
  1302. case kind >= reflect.Int && kind <= reflect.Int64:
  1303. return reflect.Int
  1304. case kind >= reflect.Uint && kind <= reflect.Uint64:
  1305. return reflect.Uint
  1306. case kind >= reflect.Float32 && kind <= reflect.Float64:
  1307. return reflect.Float32
  1308. default:
  1309. return kind
  1310. }
  1311. }
  1312. func isStructTypeConvertibleToMap(typ reflect.Type, checkMapstructureTags bool, tagName string) bool {
  1313. for i := 0; i < typ.NumField(); i++ {
  1314. f := typ.Field(i)
  1315. if f.PkgPath == "" && !checkMapstructureTags { // check for unexported fields
  1316. return true
  1317. }
  1318. if checkMapstructureTags && f.Tag.Get(tagName) != "" { // check for mapstructure tags inside
  1319. return true
  1320. }
  1321. }
  1322. return false
  1323. }
  1324. func dereferencePtrToStructIfNeeded(v reflect.Value, tagName string) reflect.Value {
  1325. if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
  1326. return v
  1327. }
  1328. deref := v.Elem()
  1329. derefT := deref.Type()
  1330. if isStructTypeConvertibleToMap(derefT, true, tagName) {
  1331. return deref
  1332. }
  1333. return v
  1334. }