gconv_scan.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2017 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 gconv
  7. import (
  8. "github.com/gogf/gf/errors/gerror"
  9. "reflect"
  10. )
  11. // Scan automatically calls Struct or Structs function according to the type of parameter
  12. // <pointer> to implement the converting.
  13. // It calls function Struct if <pointer> is type of *struct/**struct to do the converting.
  14. // It calls function Structs if <pointer> is type of *[]struct/*[]*struct to do the converting.
  15. func Scan(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
  16. t := reflect.TypeOf(pointer)
  17. k := t.Kind()
  18. if k != reflect.Ptr {
  19. return gerror.Newf("params should be type of pointer, but got: %v", k)
  20. }
  21. switch t.Elem().Kind() {
  22. case reflect.Array, reflect.Slice:
  23. return Structs(params, pointer, mapping...)
  24. default:
  25. return Struct(params, pointer, mapping...)
  26. }
  27. }
  28. // ScanDeep automatically calls StructDeep or StructsDeep function according to the type of
  29. // parameter <pointer> to implement the converting..
  30. // It calls function StructDeep if <pointer> is type of *struct/**struct to do the converting.
  31. // It calls function StructsDeep if <pointer> is type of *[]struct/*[]*struct to do the converting.
  32. // Deprecated, use Scan instead.
  33. func ScanDeep(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
  34. t := reflect.TypeOf(pointer)
  35. k := t.Kind()
  36. if k != reflect.Ptr {
  37. return gerror.Newf("params should be type of pointer, but got: %v", k)
  38. }
  39. switch t.Elem().Kind() {
  40. case reflect.Array, reflect.Slice:
  41. return StructsDeep(params, pointer, mapping...)
  42. default:
  43. return StructDeep(params, pointer, mapping...)
  44. }
  45. }