Files
gf/util/gconv/gconv_map.go

70 lines
2.2 KiB
Go
Raw Permalink Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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
// Map converts any variable `value` to map[string]any. If the parameter `value` is not a
2019-12-10 21:14:15 +08:00
// map/struct/*struct type, then the conversion will fail and returns nil.
//
// If `value` is a struct/*struct object, the second parameter `priorityTagAndFieldName` specifies the most priority
// priorityTagAndFieldName that will be detected, otherwise it detects the priorityTagAndFieldName in order of:
// gconv, json, field name.
func Map(value any, option ...MapOption) map[string]any {
result, _ := defaultConverter.Map(value, getUsedMapOption(option...))
return result
}
// MapDeep does Map function recursively, which means if the attribute of `value`
// is also a struct/*struct, calls Map function on this attribute converting it to
// a map[string]any type variable.
2025-11-10 21:40:35 +08:00
//
// Deprecated: used Map instead.
func MapDeep(value any, tags ...string) map[string]any {
result, _ := defaultConverter.Map(value, MapOption{
Deep: true,
OmitEmpty: false,
Tags: tags,
ContinueOnError: true,
})
return result
}
// MapStrStr converts `value` to map[string]string.
2019-12-14 17:01:27 +08:00
// Note that there might be data copy for this map type converting.
func MapStrStr(value any, option ...MapOption) map[string]string {
result, _ := defaultConverter.MapStrStr(value, getUsedMapOption(option...))
return result
2019-12-14 17:01:27 +08:00
}
// MapStrStrDeep converts `value` to map[string]string recursively.
2019-12-14 17:01:27 +08:00
// Note that there might be data copy for this map type converting.
2025-11-10 21:40:35 +08:00
//
// Deprecated: used MapStrStr instead.
func MapStrStrDeep(value any, tags ...string) map[string]string {
2019-12-14 17:01:27 +08:00
if r, ok := value.(map[string]string); ok {
return r
}
m := MapDeep(value, tags...)
if len(m) > 0 {
vMap := make(map[string]string, len(m))
2019-12-14 17:01:27 +08:00
for k, v := range m {
vMap[k] = String(v)
}
return vMap
}
return nil
}
func getUsedMapOption(option ...MapOption) MapOption {
var usedOption = MapOption{
ContinueOnError: true,
}
if len(option) > 0 {
usedOption = option[0]
}
return usedOption
}