Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
sanrentai authored Jan 24, 2025
2 parents f1e71b4 + 20b1987 commit 5ec4c80
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 29 deletions.
2 changes: 1 addition & 1 deletion contrib/config/polaris/polaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c *Client) doUpdate(ctx context.Context) (err error) {
return gerror.New("config file is empty")
}
var j *gjson.Json
if j, err = gjson.DecodeToJson([]byte(c.client.GetContent())); err != nil {
if j, err = gjson.LoadContent([]byte(c.client.GetContent())); err != nil {
return gerror.Wrap(err, `parse config map item from polaris failed`)
}
c.value.Set(j)
Expand Down
2 changes: 1 addition & 1 deletion net/ghttp/ghttp_middleware_handler_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func MiddlewareHandlerResponse(r *Request) {
r.Middleware.Next()

// There's custom buffer content, it then exits current handler.
if r.Response.BufferLength() > 0 {
if r.Response.BufferLength() > 0 || r.Response.Writer.BytesWritten() > 0 {
return
}

Expand Down
4 changes: 2 additions & 2 deletions net/ghttp/ghttp_request_param_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (r *Request) doGetRequestStruct(pointer interface{}, mapping ...map[string]
return data, nil
}
// `in` Tag Struct values.
if err = r.mergeInTagStructValue(data, pointer); err != nil {
if err = r.mergeInTagStructValue(data); err != nil {
return data, nil
}

Expand Down Expand Up @@ -239,7 +239,7 @@ func (r *Request) mergeDefaultStructValue(data map[string]interface{}, pointer i
}

// mergeInTagStructValue merges the request parameters with header or cookie values from struct `in` tag definition.
func (r *Request) mergeInTagStructValue(data map[string]interface{}, pointer interface{}) error {
func (r *Request) mergeInTagStructValue(data map[string]interface{}) error {
fields := r.serveHandler.Handler.Info.ReqStructFields
if len(fields) > 0 {
var (
Expand Down
17 changes: 0 additions & 17 deletions net/ghttp/ghttp_server_service_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,3 @@ func createRouterFunc(funcInfo handlerFuncInfo) func(r *Request) {
}
}
}

// trimGeneric removes type definitions string from response type name if generic
func trimGeneric(structName string) string {
var (
leftBraceIndex = strings.LastIndex(structName, "[") // for generic, it is faster to start at the end than at the beginning
rightBraceIndex = strings.LastIndex(structName, "]")
)
if leftBraceIndex == -1 || rightBraceIndex == -1 {
// not found '[' or ']'
return structName
} else if leftBraceIndex+1 == rightBraceIndex {
// may be a slice, because generic is '[X]', not '[]'
// to be compatible with bad return parameter type: []XxxRes
return structName
}
return structName[:leftBraceIndex]
}
48 changes: 48 additions & 0 deletions net/ghttp/ghttp_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package ghttp_test
import (
"context"
"fmt"
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -630,3 +631,50 @@ func Test_Issue4047(t *testing.T) {
t.Assert(s.Logger(), nil)
})
}

// https://github.com/gogf/gf/issues/4108
func Test_Issue4108(t *testing.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.GET("/", func(r *ghttp.Request) {
r.Response.Writer.Write([]byte("hello"))
})
})
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()))

rsp, err := client.Get(ctx, "/")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusOK)
t.Assert(rsp.ReadAllString(), "hello")
})
}

// https://github.com/gogf/gf/issues/4115
func Test_Issue4115(t *testing.T) {
s := g.Server(guid.S())
s.Use(func(r *ghttp.Request) {
r.Response.Writer.Write([]byte("hello"))
})
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()))

rsp, err := client.Get(ctx, "/")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusOK)
t.Assert(rsp.ReadAllString(), "hello")
})
}
2 changes: 1 addition & 1 deletion net/ghttp/internal/response/response_buffer_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (w *BufferWriter) Flush() {
w.Writer.WriteHeader(w.Status)
}
// Default status text output.
if w.Status != http.StatusOK && w.buffer.Len() == 0 {
if w.Status != http.StatusOK && w.buffer.Len() == 0 && w.Writer.BytesWritten() == 0 {
w.buffer.WriteString(http.StatusText(w.Status))
}
if w.buffer.Len() > 0 {
Expand Down
11 changes: 6 additions & 5 deletions os/gcmd/gcmd_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ type FuncWithValue func(ctx context.Context, parser *Parser) (out interface{}, e

// Argument is the command value that are used by certain command.
type Argument struct {
Name string // Option name.
Short string // Option short.
Brief string // Brief info about this Option, which is used in help info.
IsArg bool // IsArg marks this argument taking value from command line argument instead of option.
Orphan bool // Whether this Option having or having no value bound to it.
Name string // Option name.
Short string // Option short.
Default string // Option default value.
Brief string // Brief info about this Option, which is used in help info.
IsArg bool // IsArg marks this argument taking value from command line argument instead of option.
Orphan bool // Whether this Option having or having no value bound to it.
}

var (
Expand Down
3 changes: 3 additions & 0 deletions os/gcmd/gcmd_command_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ func (c *Command) PrintTo(writer io.Writer) {
spaceLength = maxSpaceLength - len(nameStr)
wordwrapPrefix = gstr.Repeat(" ", len(prefix+nameStr)+spaceLength+4)
)
if arg.Default != "" {
brief = fmt.Sprintf("%s (default: \"%s\")", brief, arg.Default)
}
c.printLineBrief(printLineBriefInput{
Buffer: buffer,
Name: nameStr,
Expand Down
3 changes: 3 additions & 0 deletions os/gcmd/gcmd_command_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ func newArgumentsFromInput(object interface{}) (args []Argument, err error) {
if arg.Brief == "" {
arg.Brief = field.TagDescription()
}
if arg.Default == "" {
arg.Default = field.TagDefault()
}
if v, ok := metaData[gtag.Arg]; ok {
arg.IsArg = gconv.Bool(v)
}
Expand Down
18 changes: 16 additions & 2 deletions test/gtest/gtest_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ func AssertLE(value, expect interface{}) {
// AssertIN checks `value` is IN `expect`.
// The `expect` should be a slice,
// but the `value` can be a slice or a basic type variable.
// TODO map support.
// TODO: gconv.Strings(0) is not [0]
func AssertIN(value, expect interface{}) {
var (
Expand Down Expand Up @@ -249,6 +248,14 @@ func AssertIN(value, expect interface{}) {
expectStr = gconv.String(expect)
)
passed = gstr.Contains(expectStr, valueStr)
case reflect.Map:
expectMap := gconv.Map(expect)
for _, v1 := range gconv.Strings(value) {
if _, exists := expectMap[v1]; !exists {
passed = false
break
}
}
default:
panic(fmt.Sprintf(`[ASSERT] INVALID EXPECT VALUE TYPE: %v`, expectKind))
}
Expand All @@ -260,7 +267,6 @@ func AssertIN(value, expect interface{}) {
// AssertNI checks `value` is NOT IN `expect`.
// The `expect` should be a slice,
// but the `value` can be a slice or a basic type variable.
// TODO map support.
func AssertNI(value, expect interface{}) {
var (
passed = true
Expand All @@ -287,6 +293,14 @@ func AssertNI(value, expect interface{}) {
expectStr = gconv.String(expect)
)
passed = !gstr.Contains(expectStr, valueStr)
case reflect.Map:
expectMap := gconv.Map(expect)
for _, v1 := range gconv.Strings(value) {
if _, exists := expectMap[v1]; exists {
passed = false
break
}
}
default:
panic(fmt.Sprintf(`[ASSERT] INVALID EXPECT VALUE TYPE: %v`, expectKind))
}
Expand Down
16 changes: 16 additions & 0 deletions test/gtest/gtest_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,22 @@ func TestAssertIN(t *testing.T) {
})
}

func TestAssertIN_Map(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertIN("k1", map[string]string{"k1": "v1", "k2": "v2"})
t.AssertIN(1, map[int64]string{1: "v1", 2: "v2"})
t.AssertIN([]string{"k1", "k2"}, map[string]string{"k1": "v1", "k2": "v2"})
})
}

func TestAssertNI_Map(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertNI("k3", map[string]string{"k1": "v1", "k2": "v2"})
t.AssertNI(3, map[int64]string{1: "v1", 2: "v2"})
t.AssertNI([]string{"k3", "k4"}, map[string]string{"k1": "v1", "k2": "v2"})
})
}

func TestAssertNI(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertNI("d", []string{"a", "b", "c"})
Expand Down

0 comments on commit 5ec4c80

Please sign in to comment.