gconv_slice.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gconv
  7. import (
  8. "github.com/gogf/gf/internal/json"
  9. )
  10. // SliceMap is alias of Maps.
  11. func SliceMap(i interface{}) []map[string]interface{} {
  12. return Maps(i)
  13. }
  14. // SliceMapDeep is alias of MapsDeep.
  15. func SliceMapDeep(i interface{}) []map[string]interface{} {
  16. return MapsDeep(i)
  17. }
  18. // SliceStruct is alias of Structs.
  19. func SliceStruct(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
  20. return Structs(params, pointer, mapping...)
  21. }
  22. // SliceStructDeep is alias of StructsDeep.
  23. // Deprecated, use SliceStruct instead.
  24. func SliceStructDeep(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
  25. return StructsDeep(params, pointer, mapping...)
  26. }
  27. // Maps converts <i> to []map[string]interface{}.
  28. // Note that it automatically checks and converts json string to []map if <value> is string/[]byte.
  29. func Maps(value interface{}, tags ...string) []map[string]interface{} {
  30. if value == nil {
  31. return nil
  32. }
  33. switch r := value.(type) {
  34. case string:
  35. list := make([]map[string]interface{}, 0)
  36. if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
  37. if err := json.Unmarshal([]byte(r), &list); err != nil {
  38. return nil
  39. }
  40. return list
  41. } else {
  42. return nil
  43. }
  44. case []byte:
  45. list := make([]map[string]interface{}, 0)
  46. if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
  47. if err := json.Unmarshal(r, &list); err != nil {
  48. return nil
  49. }
  50. return list
  51. } else {
  52. return nil
  53. }
  54. case []map[string]interface{}:
  55. return r
  56. default:
  57. array := Interfaces(value)
  58. if len(array) == 0 {
  59. return nil
  60. }
  61. list := make([]map[string]interface{}, len(array))
  62. for k, v := range array {
  63. list[k] = Map(v, tags...)
  64. }
  65. return list
  66. }
  67. }
  68. // MapsDeep converts <i> to []map[string]interface{} recursively.
  69. //
  70. // TODO completely implement the recursive converting for all types.
  71. func MapsDeep(value interface{}, tags ...string) []map[string]interface{} {
  72. if value == nil {
  73. return nil
  74. }
  75. switch r := value.(type) {
  76. case string:
  77. list := make([]map[string]interface{}, 0)
  78. if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
  79. if err := json.Unmarshal([]byte(r), &list); err != nil {
  80. return nil
  81. }
  82. return list
  83. } else {
  84. return nil
  85. }
  86. case []byte:
  87. list := make([]map[string]interface{}, 0)
  88. if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
  89. if err := json.Unmarshal(r, &list); err != nil {
  90. return nil
  91. }
  92. return list
  93. } else {
  94. return nil
  95. }
  96. case []map[string]interface{}:
  97. list := make([]map[string]interface{}, len(r))
  98. for k, v := range r {
  99. list[k] = MapDeep(v, tags...)
  100. }
  101. return list
  102. default:
  103. array := Interfaces(value)
  104. if len(array) == 0 {
  105. return nil
  106. }
  107. list := make([]map[string]interface{}, len(array))
  108. for k, v := range array {
  109. list[k] = MapDeep(v, tags...)
  110. }
  111. return list
  112. }
  113. }