2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-06-15 16:46:48 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gconv
|
|
|
|
|
|
|
|
import (
|
2020-07-13 23:13:50 +08:00
|
|
|
"github.com/gogf/gf/errors/gerror"
|
2020-06-15 16:46:48 +08:00
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2021-03-23 17:53:20 +08:00
|
|
|
// Scan automatically calls MapToMap, MapToMaps, Struct or Structs function according to
|
|
|
|
// the type of parameter `pointer` to implement the converting.
|
2021-08-16 21:47:45 +08:00
|
|
|
//
|
2021-03-23 17:53:20 +08:00
|
|
|
// It calls function MapToMap if `pointer` is type of *map to do the converting.
|
|
|
|
// It calls function MapToMaps if `pointer` is type of *[]map/*[]*map to do the converting.
|
|
|
|
// It calls function Struct if `pointer` is type of *struct/**struct to do the converting.
|
|
|
|
// It calls function Structs if `pointer` is type of *[]struct/*[]*struct to do the converting.
|
2020-06-15 16:46:48 +08:00
|
|
|
func Scan(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
|
2021-03-23 17:53:20 +08:00
|
|
|
var (
|
|
|
|
pointerType = reflect.TypeOf(pointer)
|
|
|
|
pointerKind = pointerType.Kind()
|
|
|
|
)
|
|
|
|
if pointerKind != reflect.Ptr {
|
2021-07-20 23:02:02 +08:00
|
|
|
return gerror.NewCodef(gerror.CodeInvalidParameter, "params should be type of pointer, but got: %v", pointerKind)
|
2020-06-15 16:46:48 +08:00
|
|
|
}
|
2021-03-23 17:53:20 +08:00
|
|
|
var (
|
|
|
|
pointerElem = pointerType.Elem()
|
|
|
|
pointerElemKind = pointerElem.Kind()
|
|
|
|
)
|
|
|
|
switch pointerElemKind {
|
|
|
|
case reflect.Map:
|
|
|
|
return MapToMap(params, pointer, mapping...)
|
2021-06-26 16:23:54 +08:00
|
|
|
|
2020-06-15 16:46:48 +08:00
|
|
|
case reflect.Array, reflect.Slice:
|
2021-03-23 17:53:20 +08:00
|
|
|
var (
|
|
|
|
sliceElem = pointerElem.Elem()
|
|
|
|
sliceElemKind = sliceElem.Kind()
|
|
|
|
)
|
|
|
|
for sliceElemKind == reflect.Ptr {
|
|
|
|
sliceElem = sliceElem.Elem()
|
|
|
|
sliceElemKind = sliceElem.Kind()
|
|
|
|
}
|
|
|
|
if sliceElemKind == reflect.Map {
|
|
|
|
return MapToMaps(params, pointer, mapping...)
|
|
|
|
}
|
2020-06-15 16:46:48 +08:00
|
|
|
return Structs(params, pointer, mapping...)
|
|
|
|
default:
|
|
|
|
return Struct(params, pointer, mapping...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScanDeep automatically calls StructDeep or StructsDeep function according to the type of
|
2021-08-16 21:47:45 +08:00
|
|
|
// parameter `pointer` to implement the converting.
|
|
|
|
//
|
2021-03-23 17:53:20 +08:00
|
|
|
// It calls function StructDeep if `pointer` is type of *struct/**struct to do the converting.
|
|
|
|
// It calls function StructsDeep if `pointer` is type of *[]struct/*[]*struct to do the converting.
|
2020-11-05 23:02:29 +08:00
|
|
|
// Deprecated, use Scan instead.
|
2020-06-15 16:46:48 +08:00
|
|
|
func ScanDeep(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
|
|
|
|
t := reflect.TypeOf(pointer)
|
|
|
|
k := t.Kind()
|
|
|
|
if k != reflect.Ptr {
|
2021-07-20 23:02:02 +08:00
|
|
|
return gerror.NewCodef(gerror.CodeInvalidParameter, "params should be type of pointer, but got: %v", k)
|
2020-06-15 16:46:48 +08:00
|
|
|
}
|
|
|
|
switch t.Elem().Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
return StructsDeep(params, pointer, mapping...)
|
|
|
|
default:
|
|
|
|
return StructDeep(params, pointer, mapping...)
|
|
|
|
}
|
|
|
|
}
|