gconv_maps.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright GoFrame Author(https://goframe.org). 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 "github.com/gogf/gf/internal/json"
  8. // SliceMap is alias of Maps.
  9. func SliceMap(any interface{}) []map[string]interface{} {
  10. return Maps(any)
  11. }
  12. // SliceMapDeep is alias of MapsDeep.
  13. func SliceMapDeep(any interface{}) []map[string]interface{} {
  14. return MapsDeep(any)
  15. }
  16. // SliceStruct is alias of Structs.
  17. func SliceStruct(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
  18. return Structs(params, pointer, mapping...)
  19. }
  20. // Maps converts `value` to []map[string]interface{}.
  21. // Note that it automatically checks and converts json string to []map if `value` is string/[]byte.
  22. func Maps(value interface{}, tags ...string) []map[string]interface{} {
  23. if value == nil {
  24. return nil
  25. }
  26. switch r := value.(type) {
  27. case string:
  28. list := make([]map[string]interface{}, 0)
  29. if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
  30. if err := json.UnmarshalUseNumber([]byte(r), &list); err != nil {
  31. return nil
  32. }
  33. return list
  34. } else {
  35. return nil
  36. }
  37. case []byte:
  38. list := make([]map[string]interface{}, 0)
  39. if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
  40. if err := json.UnmarshalUseNumber(r, &list); err != nil {
  41. return nil
  42. }
  43. return list
  44. } else {
  45. return nil
  46. }
  47. case []map[string]interface{}:
  48. return r
  49. default:
  50. array := Interfaces(value)
  51. if len(array) == 0 {
  52. return nil
  53. }
  54. list := make([]map[string]interface{}, len(array))
  55. for k, v := range array {
  56. list[k] = Map(v, tags...)
  57. }
  58. return list
  59. }
  60. }
  61. // MapsDeep converts `value` to []map[string]interface{} recursively.
  62. //
  63. // TODO completely implement the recursive converting for all types.
  64. func MapsDeep(value interface{}, tags ...string) []map[string]interface{} {
  65. if value == nil {
  66. return nil
  67. }
  68. switch r := value.(type) {
  69. case string:
  70. list := make([]map[string]interface{}, 0)
  71. if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
  72. if err := json.UnmarshalUseNumber([]byte(r), &list); err != nil {
  73. return nil
  74. }
  75. return list
  76. } else {
  77. return nil
  78. }
  79. case []byte:
  80. list := make([]map[string]interface{}, 0)
  81. if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
  82. if err := json.UnmarshalUseNumber(r, &list); err != nil {
  83. return nil
  84. }
  85. return list
  86. } else {
  87. return nil
  88. }
  89. case []map[string]interface{}:
  90. list := make([]map[string]interface{}, len(r))
  91. for k, v := range r {
  92. list[k] = MapDeep(v, tags...)
  93. }
  94. return list
  95. default:
  96. array := Interfaces(value)
  97. if len(array) == 0 {
  98. return nil
  99. }
  100. list := make([]map[string]interface{}, len(array))
  101. for k, v := range array {
  102. list[k] = MapDeep(v, tags...)
  103. }
  104. return list
  105. }
  106. }