gconv_maps.go 2.8 KB

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