Skip to content

Commit

Permalink
Merge branch 'master' into feat/gconv-group-convert
Browse files Browse the repository at this point in the history
  • Loading branch information
wln32 authored Feb 6, 2025
2 parents 2709a74 + 20b1987 commit a54e58a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 23 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
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]
}
22 changes: 22 additions & 0 deletions net/ghttp/ghttp_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,3 +656,25 @@ func Test_Issue4108(t *testing.T) {
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
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 a54e58a

Please sign in to comment.