gvar_struct.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 (
  8. "github.com/gogf/gf/util/gconv"
  9. )
  10. // Struct maps value of <v> to <pointer>.
  11. // The parameter <pointer> should be a pointer to a struct instance.
  12. // The parameter <mapping> is used to specify the key-to-attribute mapping rules.
  13. func (v *Var) Struct(pointer interface{}, mapping ...map[string]string) error {
  14. return gconv.Struct(v.Val(), pointer, mapping...)
  15. }
  16. // Struct maps value of <v> to <pointer> recursively.
  17. // The parameter <pointer> should be a pointer to a struct instance.
  18. // The parameter <mapping> is used to specify the key-to-attribute mapping rules.
  19. // Deprecated, use Struct instead.
  20. func (v *Var) StructDeep(pointer interface{}, mapping ...map[string]string) error {
  21. return gconv.StructDeep(v.Val(), pointer, mapping...)
  22. }
  23. // Structs converts and returns <v> as given struct slice.
  24. func (v *Var) Structs(pointer interface{}, mapping ...map[string]string) error {
  25. return gconv.Structs(v.Val(), pointer, mapping...)
  26. }
  27. // StructsDeep converts and returns <v> as given struct slice recursively.
  28. // Deprecated, use Struct instead.
  29. func (v *Var) StructsDeep(pointer interface{}, mapping ...map[string]string) error {
  30. return gconv.StructsDeep(v.Val(), pointer, mapping...)
  31. }
  32. // Scan automatically calls Struct or Structs function according to the type of parameter
  33. // <pointer> to implement the converting.
  34. // It calls function Struct if <pointer> is type of *struct/**struct to do the converting.
  35. // It calls function Structs if <pointer> is type of *[]struct/*[]*struct to do the converting.
  36. func (v *Var) Scan(pointer interface{}, mapping ...map[string]string) error {
  37. return gconv.Scan(v.Val(), pointer, mapping...)
  38. }
  39. // ScanDeep automatically calls StructDeep or StructsDeep function according to the type of
  40. // parameter <pointer> to implement the converting.
  41. // It calls function StructDeep if <pointer> is type of *struct/**struct to do the converting.
  42. // It calls function StructsDeep if <pointer> is type of *[]struct/*[]*struct to do the converting.
  43. func (v *Var) ScanDeep(pointer interface{}, mapping ...map[string]string) error {
  44. return gconv.ScanDeep(v.Val(), pointer, mapping...)
  45. }