gvar_map.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 gvar
  7. import "github.com/gogf/gf/v2/util/gconv"
  8. // MapOption specifies the option for map converting.
  9. type MapOption = gconv.MapOption
  10. // Map converts and returns `v` as map[string]interface{}.
  11. func (v *Var) Map(option ...MapOption) map[string]interface{} {
  12. return gconv.Map(v.Val(), option...)
  13. }
  14. // MapStrAny is like function Map, but implements the interface of MapStrAny.
  15. func (v *Var) MapStrAny(option ...MapOption) map[string]interface{} {
  16. return v.Map(option...)
  17. }
  18. // MapStrStr converts and returns `v` as map[string]string.
  19. func (v *Var) MapStrStr(option ...MapOption) map[string]string {
  20. return gconv.MapStrStr(v.Val(), option...)
  21. }
  22. // MapStrVar converts and returns `v` as map[string]Var.
  23. func (v *Var) MapStrVar(option ...MapOption) map[string]*Var {
  24. m := v.Map(option...)
  25. if len(m) > 0 {
  26. vMap := make(map[string]*Var, len(m))
  27. for k, v := range m {
  28. vMap[k] = New(v)
  29. }
  30. return vMap
  31. }
  32. return nil
  33. }
  34. // MapDeep converts and returns `v` as map[string]interface{} recursively.
  35. // Deprecated: used Map instead.
  36. func (v *Var) MapDeep(tags ...string) map[string]interface{} {
  37. return gconv.MapDeep(v.Val(), tags...)
  38. }
  39. // MapStrStrDeep converts and returns `v` as map[string]string recursively.
  40. // Deprecated: used MapStrStr instead.
  41. func (v *Var) MapStrStrDeep(tags ...string) map[string]string {
  42. return gconv.MapStrStrDeep(v.Val(), tags...)
  43. }
  44. // MapStrVarDeep converts and returns `v` as map[string]*Var recursively.
  45. // Deprecated: used MapStrVar instead.
  46. func (v *Var) MapStrVarDeep(tags ...string) map[string]*Var {
  47. m := v.MapDeep(tags...)
  48. if len(m) > 0 {
  49. vMap := make(map[string]*Var, len(m))
  50. for k, v := range m {
  51. vMap[k] = New(v)
  52. }
  53. return vMap
  54. }
  55. return nil
  56. }
  57. // Maps converts and returns `v` as map[string]string.
  58. // See gconv.Maps.
  59. func (v *Var) Maps(option ...MapOption) []map[string]interface{} {
  60. return gconv.Maps(v.Val(), option...)
  61. }
  62. // MapsDeep converts `value` to []map[string]interface{} recursively.
  63. // Deprecated: used Maps instead.
  64. func (v *Var) MapsDeep(tags ...string) []map[string]interface{} {
  65. return gconv.MapsDeep(v.Val(), tags...)
  66. }
  67. // MapToMap converts any map type variable `params` to another map type variable `pointer`.
  68. // See gconv.MapToMap.
  69. func (v *Var) MapToMap(pointer interface{}, mapping ...map[string]string) (err error) {
  70. return gconv.MapToMap(v.Val(), pointer, mapping...)
  71. }
  72. // MapToMaps converts any map type variable `params` to another map type variable `pointer`.
  73. // See gconv.MapToMaps.
  74. func (v *Var) MapToMaps(pointer interface{}, mapping ...map[string]string) (err error) {
  75. return gconv.MapToMaps(v.Val(), pointer, mapping...)
  76. }
  77. // MapToMapsDeep converts any map type variable `params` to another map type variable
  78. // `pointer` recursively.
  79. // See gconv.MapToMapsDeep.
  80. func (v *Var) MapToMapsDeep(pointer interface{}, mapping ...map[string]string) (err error) {
  81. return gconv.MapToMaps(v.Val(), pointer, mapping...)
  82. }