mapstructure.go 42 KB

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