gvar_map.go 2.9 KB

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