feat(net/ghttp): add Request.GetMetaTag to retrieve specific meta tag value (#4185)

This commit is contained in:
PandaPy
2025-03-09 11:17:41 +08:00
committed by GitHub
parent bcda48bf82
commit f8331bad6e
6 changed files with 96 additions and 15 deletions

View File

@ -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