mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
feat(net/ghttp): add Request.GetMetaTag to retrieve specific meta tag value (#4185)
This commit is contained in:
@ -8,6 +8,8 @@
|
||||
package gmeta
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/os/gstructs"
|
||||
)
|
||||
@ -15,19 +17,20 @@ import (
|
||||
// Meta is used as an embedded attribute for struct to enabled metadata feature.
|
||||
type Meta struct{}
|
||||
|
||||
const (
|
||||
metaAttributeName = "Meta" // metaAttributeName is the attribute name of metadata in struct.
|
||||
metaTypeName = "gmeta.Meta" // metaTypeName is for type string comparison.
|
||||
)
|
||||
// metaAttributeName is the attribute name of metadata in struct.
|
||||
const metaAttributeName = "Meta"
|
||||
|
||||
// metaType holds the reflection. Type of Meta, used for efficient type comparison.
|
||||
var metaType = reflect.TypeOf(Meta{})
|
||||
|
||||
// Data retrieves and returns all metadata from `object`.
|
||||
func Data(object interface{}) map[string]string {
|
||||
func Data(object any) map[string]string {
|
||||
reflectType, err := gstructs.StructType(object)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if field, ok := reflectType.FieldByName(metaAttributeName); ok {
|
||||
if field.Type.String() == metaTypeName {
|
||||
if field.Type == metaType {
|
||||
return gstructs.ParseTag(string(field.Tag))
|
||||
}
|
||||
}
|
||||
@ -35,7 +38,7 @@ func Data(object interface{}) map[string]string {
|
||||
}
|
||||
|
||||
// Get retrieves and returns specified metadata by `key` from `object`.
|
||||
func Get(object interface{}, key string) *gvar.Var {
|
||||
func Get(object any, key string) *gvar.Var {
|
||||
v, ok := Data(object)[key]
|
||||
if !ok {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user