Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve support for generic router registering #2877

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions net/ghttp/ghttp_server_service_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ func (s *Server) checkAndCreateFuncInfo(f interface{}, pkgPath, structName, meth
return
}

// 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
leftBraceIndex = strings.LastIndex(structName, "[")
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 '[]'
if leftBraceIndex == -1 && rightBraceIndex == -1 {
// no generic type usage.
return structName
}
return structName[:leftBraceIndex]
Expand Down
26 changes: 0 additions & 26 deletions net/ghttp/ghttp_z_unit_feature_router_strict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,40 +323,17 @@ func Test_Router_Handler_Strict_WithGeneric(t *testing.T) {
},
}, nil
})
s.BindHandler("/test1_slice", func(ctx context.Context, req *TestReq) (res []Test1Res, err error) {
return []Test1Res{
Test1Res{
Age: TestGeneric[int]{
Test: req.Age,
},
},
}, nil
})
s.BindHandler("/test2", func(ctx context.Context, req *TestReq) (res *Test2Res, err error) {
return &Test2Res{
Test: req.Age,
}, nil
})
s.BindHandler("/test2_slice", func(ctx context.Context, req *TestReq) (res []Test2Res, err error) {
return []Test2Res{
Test2Res{
Test: req.Age,
},
}, nil
})

s.BindHandler("/test3", func(ctx context.Context, req *TestReq) (res *TestGenericRes[int], err error) {
return &TestGenericRes[int]{
Test: req.Age,
}, nil
})
s.BindHandler("/test3_slice", func(ctx context.Context, req *TestReq) (res []TestGenericRes[int], err error) {
return []TestGenericRes[int]{
TestGenericRes[int]{
Test: req.Age,
},
}, nil
})

s.SetDumpRouterMap(false)
s.Start()
Expand All @@ -368,10 +345,7 @@ func Test_Router_Handler_Strict_WithGeneric(t *testing.T) {
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))

t.Assert(client.GetContent(ctx, "/test1?age=1"), `{"code":0,"message":"","data":{"Age":{"Test":1}}}`)
t.Assert(client.GetContent(ctx, "/test1_slice?age=1"), `{"code":0,"message":"","data":[{"Age":{"Test":1}}]}`)
t.Assert(client.GetContent(ctx, "/test2?age=2"), `{"code":0,"message":"","data":{"Test":2}}`)
t.Assert(client.GetContent(ctx, "/test2_slice?age=2"), `{"code":0,"message":"","data":[{"Test":2}]}`)
t.Assert(client.GetContent(ctx, "/test3?age=3"), `{"code":0,"message":"","data":{"Test":3}}`)
t.Assert(client.GetContent(ctx, "/test3_slice?age=3"), `{"code":0,"message":"","data":[{"Test":3}]}`)
})
}