2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-09-16 10:51:02 +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-16 10:51:02 +08:00
|
|
|
|
|
|
|
|
package gconv
|
|
|
|
|
|
2020-05-13 23:21:11 +08:00
|
|
|
// Struct maps the params key-value pairs to the corresponding struct object's attributes.
|
2021-03-23 17:53:20 +08:00
|
|
|
// The third parameter `mapping` is unnecessary, indicating the mapping rules between the
|
2021-11-05 01:07:06 +08:00
|
|
|
// custom key name and the attribute name(case-sensitive).
|
2019-05-08 21:03:04 +08:00
|
|
|
//
|
|
|
|
|
// Note:
|
2022-11-01 20:12:21 +08:00
|
|
|
// 1. The `params` can be any type of map/struct, usually a map.
|
|
|
|
|
// 2. The `pointer` should be type of *struct/**struct, which is a pointer to struct object
|
|
|
|
|
// or struct pointer.
|
|
|
|
|
// 3. Only the public attributes of struct object can be mapped.
|
|
|
|
|
// 4. If `params` is a map, the key of the map `params` can be lowercase.
|
|
|
|
|
// It will automatically convert the first letter of the key to uppercase
|
|
|
|
|
// in mapping procedure to do the matching.
|
|
|
|
|
// It ignores the map key, if it does not match.
|
2025-03-06 23:04:26 +08:00
|
|
|
func Struct(params any, pointer any, paramKeyToAttrMap ...map[string]string) (err error) {
|
2023-11-22 21:05:39 +08:00
|
|
|
return Scan(params, pointer, paramKeyToAttrMap...)
|
2021-02-09 18:00:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StructTag acts as Struct but also with support for priority tag feature, which retrieves the
|
2024-09-09 16:17:01 +08:00
|
|
|
// specified priorityTagAndFieldName for `params` key-value items to struct attribute names mapping.
|
|
|
|
|
// The parameter `priorityTag` supports multiple priorityTagAndFieldName that can be joined with char ','.
|
2025-03-06 23:04:26 +08:00
|
|
|
func StructTag(params any, pointer any, priorityTag string) (err error) {
|
|
|
|
|
option := StructOption{
|
|
|
|
|
PriorityTag: priorityTag,
|
|
|
|
|
ContinueOnError: true,
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2025-03-06 23:04:26 +08:00
|
|
|
return defaultConverter.Struct(params, pointer, option)
|
2018-10-09 10:05:55 +08:00
|
|
|
}
|