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

@ -19,6 +19,7 @@ import (
"github.com/gogf/gf/v2/os/gview"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gmeta"
"github.com/gogf/gf/v2/util/guid"
)
@ -40,7 +41,7 @@ type Request struct {
// =================================================================================================================
handlers []*HandlerItemParsed // All matched handlers containing handler, hook and middleware for this request.
serveHandler *HandlerItemParsed // Real handler serving for this request, not hook or middleware.
serveHandler *HandlerItemParsed // Real business handler serving for this request, not hook or middleware handler.
handlerResponse interface{} // Handler response object for Request/Response handler.
hasHookHandler bool // A bool marking whether there's hook handler in the handlers for performance purpose.
hasServeHandler bool // A bool marking whether there's serving handler in the handlers for performance purpose.
@ -288,3 +289,25 @@ func (r *Request) GetHandlerResponse() interface{} {
func (r *Request) GetServeHandler() *HandlerItemParsed {
return r.serveHandler
}
// GetMetaTag retrieves and returns the metadata value associated with the given key from the request struct.
// The meta value is from struct tags from g.Meta/gmeta.Meta type.
// For example:
//
// type GetMetaTagReq struct {
// g.Meta `path:"/test" method:"post" summary:"meta_tag" tags:"meta"`
// // ...
// }
//
// r.GetMetaTag("summary") // returns "meta_tag"
// r.GetMetaTag("method") // returns "post"
func (r *Request) GetMetaTag(key string) string {
if r.serveHandler == nil || r.serveHandler.Handler == nil {
return ""
}
metaValue := gmeta.Get(r.serveHandler.Handler.Info.Type.In(1), key)
if metaValue != nil {
return metaValue.String()
}
return ""
}

View File

@ -8,6 +8,7 @@ package ghttp_test
import (
"bytes"
"context"
"fmt"
"io"
"testing"
@ -861,3 +862,43 @@ func Test_Params_GetRequestMapStrVar(t *testing.T) {
t.Assert(client.GetContent(ctx, "/GetRequestMapStrVar", "id=1"), 1)
})
}
type GetMetaTagReq struct {
g.Meta `path:"/test" method:"post" summary:"meta_tag" tags:"meta"`
Name string
}
type GetMetaTagRes struct{}
type GetMetaTagSt struct{}
func (r GetMetaTagSt) PostTest(ctx context.Context, req *GetMetaTagReq) (*GetMetaTagRes, error) {
return &GetMetaTagRes{}, nil
}
func TestRequest_GetMetaTag(t *testing.T) {
s := g.Server(guid.S())
s.Use(func(r *ghttp.Request) {
r.Response.Writef(
"summary:%s,method:%s",
r.GetMetaTag("summary"), r.GetMetaTag("method"),
)
})
s.Group("/", func(grp *ghttp.RouterGroup) {
grp.Bind(GetMetaTagSt{})
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(1000 * time.Millisecond)
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
client := g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
t.Assert(client.PostContent(ctx, "/test", "name=john"), "summary:meta_tag,method:post")
})
}