gstr_parse.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2018 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 gstr
  7. import (
  8. "fmt"
  9. "net/url"
  10. "strings"
  11. )
  12. // Parse parses the string into map[string]interface{}.
  13. //
  14. // v1=m&v2=n -> map[v1:m v2:n]
  15. // v[a]=m&v[b]=n -> map[v:map[a:m b:n]]
  16. // v[a][a]=m&v[a][b]=n -> map[v:map[a:map[a:m b:n]]]
  17. // v[]=m&v[]=n -> map[v:[m n]]
  18. // v[a][]=m&v[a][]=n -> map[v:map[a:[m n]]]
  19. // v[][]=m&v[][]=n -> map[v:[map[]]] // Currently does not support nested slice.
  20. // v=m&v[a]=n -> error
  21. // a .[[b=c -> map[a___[b:c]
  22. //
  23. func Parse(s string) (result map[string]interface{}, err error) {
  24. if s == "" {
  25. return nil, nil
  26. }
  27. result = make(map[string]interface{})
  28. parts := strings.Split(s, "&")
  29. for _, part := range parts {
  30. pos := strings.Index(part, "=")
  31. if pos <= 0 {
  32. continue
  33. }
  34. key, err := url.QueryUnescape(part[:pos])
  35. if err != nil {
  36. return nil, err
  37. }
  38. for key[0] == ' ' {
  39. key = key[1:]
  40. }
  41. if key == "" || key[0] == '[' {
  42. continue
  43. }
  44. value, err := url.QueryUnescape(part[pos+1:])
  45. if err != nil {
  46. return nil, err
  47. }
  48. // split into multiple keys
  49. var keys []string
  50. left := 0
  51. for i, k := range key {
  52. if k == '[' && left == 0 {
  53. left = i
  54. } else if k == ']' {
  55. if left > 0 {
  56. if len(keys) == 0 {
  57. keys = append(keys, key[:left])
  58. }
  59. keys = append(keys, key[left+1:i])
  60. left = 0
  61. if i+1 < len(key) && key[i+1] != '[' {
  62. break
  63. }
  64. }
  65. }
  66. }
  67. if len(keys) == 0 {
  68. keys = append(keys, key)
  69. }
  70. // first key
  71. first := ""
  72. for i, chr := range keys[0] {
  73. if chr == ' ' || chr == '.' || chr == '[' {
  74. first += "_"
  75. } else {
  76. first += string(chr)
  77. }
  78. if chr == '[' {
  79. first += keys[0][i+1:]
  80. break
  81. }
  82. }
  83. keys[0] = first
  84. // build nested map
  85. if err := build(result, keys, value); err != nil {
  86. return nil, err
  87. }
  88. }
  89. return result, nil
  90. }
  91. // build nested map.
  92. func build(result map[string]interface{}, keys []string, value interface{}) error {
  93. length := len(keys)
  94. // trim ',"
  95. key := strings.Trim(keys[0], "'\"")
  96. if length == 1 {
  97. result[key] = value
  98. return nil
  99. }
  100. // The end is slice. like f[], f[a][]
  101. if keys[1] == "" && length == 2 {
  102. // TODO nested slice
  103. if key == "" {
  104. return nil
  105. }
  106. val, ok := result[key]
  107. if !ok {
  108. result[key] = []interface{}{value}
  109. return nil
  110. }
  111. children, ok := val.([]interface{})
  112. if !ok {
  113. return fmt.Errorf("expected type '[]interface{}' for key '%s', but got '%T'", key, val)
  114. }
  115. result[key] = append(children, value)
  116. return nil
  117. }
  118. // The end is slice + map. like v[][a]
  119. if keys[1] == "" && length > 2 && keys[2] != "" {
  120. val, ok := result[key]
  121. if !ok {
  122. result[key] = []interface{}{}
  123. val = result[key]
  124. }
  125. children, ok := val.([]interface{})
  126. if !ok {
  127. return fmt.Errorf("expected type '[]interface{}' for key '%s', but got '%T'", key, val)
  128. }
  129. if l := len(children); l > 0 {
  130. if child, ok := children[l-1].(map[string]interface{}); ok {
  131. if _, ok := child[keys[2]]; !ok {
  132. build(child, keys[2:], value)
  133. return nil
  134. }
  135. }
  136. }
  137. child := map[string]interface{}{}
  138. build(child, keys[2:], value)
  139. result[key] = append(children, child)
  140. return nil
  141. }
  142. // map, like v[a], v[a][b]
  143. val, ok := result[key]
  144. if !ok {
  145. result[key] = map[string]interface{}{}
  146. val = result[key]
  147. }
  148. children, ok := val.(map[string]interface{})
  149. if !ok {
  150. return fmt.Errorf("expected type 'map[string]interface{}' for key '%s', but got '%T'", key, val)
  151. }
  152. if err := build(children, keys[1:], value); err != nil {
  153. return err
  154. }
  155. return nil
  156. }