本站源代码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1150 行
32KB

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