2019-07-03 22:09:35 +08:00
|
|
|
// Copyright 2019 gf Author(https://github.com/gogf/gf). 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.
|
|
|
|
|
|
2019-07-04 11:11:41 +08:00
|
|
|
package structs
|
2019-07-03 22:09:35 +08:00
|
|
|
|
2020-01-01 14:57:57 +08:00
|
|
|
// MapField retrieves struct field as map[name/tag]*Field from <pointer>, and returns the map.
|
|
|
|
|
//
|
2020-04-09 13:37:27 +08:00
|
|
|
// The parameter <pointer> should be type of struct/*struct.
|
|
|
|
|
//
|
2020-01-01 14:57:57 +08:00
|
|
|
// The parameter <priority> specifies the priority tag array for retrieving from high to low.
|
2019-07-04 11:11:41 +08:00
|
|
|
//
|
|
|
|
|
// The parameter <recursive> specifies whether retrieving the struct field recursively.
|
|
|
|
|
//
|
|
|
|
|
// Note that it only retrieves the exported attributes with first letter up-case from struct.
|
2020-11-08 14:25:17 +08:00
|
|
|
func MapField(pointer interface{}, priority []string) (map[string]*Field, error) {
|
|
|
|
|
tagFields, err := getFieldValuesByTagPriority(pointer, priority, map[string]struct{}{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2019-07-03 22:09:35 +08:00
|
|
|
}
|
2020-11-08 14:25:17 +08:00
|
|
|
tagFieldMap := make(map[string]*Field, len(tagFields))
|
|
|
|
|
for _, field := range tagFields {
|
|
|
|
|
tagField := field
|
|
|
|
|
tagFieldMap[field.Name()] = tagField
|
2020-11-08 15:44:04 +08:00
|
|
|
if tagField.TagValue != "" {
|
|
|
|
|
tagFieldMap[tagField.TagValue] = tagField
|
2019-07-03 22:09:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-11-08 14:25:17 +08:00
|
|
|
return tagFieldMap, nil
|
2019-07-03 22:09:35 +08:00
|
|
|
}
|