2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-09-28 13:33:41 +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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-09-28 13:33:41 +08:00
|
|
|
|
|
|
|
|
package gconv
|
|
|
|
|
|
2018-11-30 09:48:57 +08:00
|
|
|
import (
|
2020-06-17 21:16:25 +08:00
|
|
|
"github.com/gogf/gf/internal/json"
|
2018-11-30 09:48:57 +08:00
|
|
|
)
|
2018-09-28 13:33:41 +08:00
|
|
|
|
2019-07-09 09:43:53 +08:00
|
|
|
// SliceMap is alias of Maps.
|
|
|
|
|
func SliceMap(i interface{}) []map[string]interface{} {
|
|
|
|
|
return Maps(i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SliceMapDeep is alias of MapsDeep.
|
|
|
|
|
func SliceMapDeep(i interface{}) []map[string]interface{} {
|
|
|
|
|
return MapsDeep(i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SliceStruct is alias of Structs.
|
|
|
|
|
func SliceStruct(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
|
|
|
|
|
return Structs(params, pointer, mapping...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SliceStructDeep is alias of StructsDeep.
|
|
|
|
|
func SliceStructDeep(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
|
|
|
|
|
return StructsDeep(params, pointer, mapping...)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 21:03:04 +08:00
|
|
|
// Maps converts <i> to []map[string]interface{}.
|
2019-07-28 11:50:12 +08:00
|
|
|
func Maps(value interface{}, tags ...string) []map[string]interface{} {
|
|
|
|
|
if value == nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2020-06-15 18:59:18 +08:00
|
|
|
switch r := value.(type) {
|
|
|
|
|
case string:
|
|
|
|
|
list := make([]map[string]interface{}, 0)
|
|
|
|
|
if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
|
|
|
|
|
if err := json.Unmarshal([]byte(r), &list); err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return list
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case []byte:
|
|
|
|
|
list := make([]map[string]interface{}, 0)
|
|
|
|
|
if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
|
|
|
|
|
if err := json.Unmarshal(r, &list); err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return list
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case []map[string]interface{}:
|
2019-06-19 09:06:52 +08:00
|
|
|
return r
|
2020-06-15 18:59:18 +08:00
|
|
|
|
|
|
|
|
default:
|
2019-07-28 11:50:12 +08:00
|
|
|
array := Interfaces(value)
|
2019-06-19 09:06:52 +08:00
|
|
|
if len(array) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
list := make([]map[string]interface{}, len(array))
|
|
|
|
|
for k, v := range array {
|
2019-07-28 11:50:12 +08:00
|
|
|
list[k] = Map(v, tags...)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
|
return list
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-06 11:10:32 +08:00
|
|
|
|
2019-07-09 09:43:53 +08:00
|
|
|
// MapsDeep converts <i> to []map[string]interface{} recursively.
|
2020-06-15 18:59:18 +08:00
|
|
|
//
|
|
|
|
|
// TODO completely implement the recursive converting for all types.
|
2019-07-28 11:50:12 +08:00
|
|
|
func MapsDeep(value interface{}, tags ...string) []map[string]interface{} {
|
|
|
|
|
if value == nil {
|
2019-07-09 09:43:53 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2020-06-15 18:59:18 +08:00
|
|
|
switch r := value.(type) {
|
|
|
|
|
case string:
|
|
|
|
|
list := make([]map[string]interface{}, 0)
|
|
|
|
|
if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
|
|
|
|
|
if err := json.Unmarshal([]byte(r), &list); err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return list
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case []byte:
|
|
|
|
|
list := make([]map[string]interface{}, 0)
|
|
|
|
|
if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
|
|
|
|
|
if err := json.Unmarshal(r, &list); err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return list
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case []map[string]interface{}:
|
2020-06-15 23:19:38 +08:00
|
|
|
list := make([]map[string]interface{}, len(r))
|
|
|
|
|
for k, v := range r {
|
|
|
|
|
list[k] = MapDeep(v, tags...)
|
|
|
|
|
}
|
|
|
|
|
return list
|
2020-06-15 18:59:18 +08:00
|
|
|
|
|
|
|
|
default:
|
2019-07-28 11:50:12 +08:00
|
|
|
array := Interfaces(value)
|
2019-07-09 09:43:53 +08:00
|
|
|
if len(array) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
list := make([]map[string]interface{}, len(array))
|
|
|
|
|
for k, v := range array {
|
2019-07-28 11:50:12 +08:00
|
|
|
list[k] = MapDeep(v, tags...)
|
2019-07-09 09:43:53 +08:00
|
|
|
}
|
|
|
|
|
return list
|
|
|
|
|
}
|
|
|
|
|
}
|