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

need support for generic type in router function #2457

Closed
agzou opened this issue Feb 16, 2023 · 6 comments
Closed

need support for generic type in router function #2457

agzou opened this issue Feb 16, 2023 · 6 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. done This issue is done, which may be release in next version. enhancement help wanted

Comments

@agzou
Copy link

agzou commented Feb 16, 2023

1. What version of Go and system type/arch are you using?

go 1.19

2. What version of GoFrame are you using?

v2.3.2

3. Can this issue be re-produced with the latest release?

4. What did you do?

type ListMessageReq struct {
	g.Meta    `path:"/list" method:"get"`
	StartTime int64
	EndTime   int64
}
type ListMessageRes struct {
	g.Meta
	Title   string
	Content string
}
type BaseRes[T any] struct {
	g.Meta
	Code int
	Data T
	Msg  string
}
func (c *cMessage) List(ctx context.Context, req *v1.ListMessageReq) (res *v1.BaseRes[*v1.ListMessageRes], err error) {
	return res, err
}

报错日志:
[FATA] invalid struct naming for response: defined as "*v1.BaseRes[*xxx/api/v1.ListMessageRes]", but it should be named with "Res" suffix like "XxxRes"

是不是要考虑泛型情况下,返回值名称校验?

@github-actions
Copy link

Hello @agzou. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it!
你好 @agzou。我们喜欢您的提案/反馈,并希望您或其他社区成员通过拉取请求做出贡献。我们提前感谢您的贡献,并期待对其进行审查。

@gqcn
Copy link
Member

gqcn commented Feb 21, 2023

@agzou 我看了一下,可以调整下路由注册时的名称校验,欢迎提供PR改进。

@gqcn gqcn added the bug It is confirmed a bug, but don't worry, we'll handle it. label Feb 21, 2023
@agzou
Copy link
Author

agzou commented Feb 21, 2023

@agzou 我看了一下,可以调整下路由注册时的名称校验,欢迎提供PR改进。

@gqcn 好的,我研究下,看看能不能提交PR

@gqcn
Copy link
Member

gqcn commented Feb 21, 2023

@agzou 另外,我想了解一下,你为什么不在BaseRes的属性上使用泛型,而不是对这个结构体使用泛型呢?如果你只是想返回数组的话,可以参考这个 https:/gogf/gf/blob/master/example/httpserver/response_with_json_array/controller.go

按照我的理解,在业务层应该用不上泛型,并且应该尽量避免使用泛型,使得API的输入/输出是确定地,不可变的。

@agzou
Copy link
Author

agzou commented Feb 22, 2023

@gqcn BaseRes 定义了公共返回的code,msg等字段,其实效果和通过插件统一封装返回结果一样,不过如果定义在接口看起来会方便些,第一眼就知道返回结构,不用再找是不是中间件改了我的返回,这个是从java带过来的习惯,基本照搬java的接口返回定义

@gqcn gqcn changed the title 使用规范路由时,带泛型的返回值会报错 need support for generic type in router function Mar 7, 2023
bobzh39 pushed a commit to bobzh39/gf that referenced this issue Apr 28, 2023
veryyoung added a commit to veryyoung/gf that referenced this issue Jun 15, 2023
gqcn added a commit that referenced this issue Feb 1, 2024
@gqcn
Copy link
Member

gqcn commented Feb 1, 2024

#3297

@gqcn gqcn closed this as completed Feb 1, 2024
@gqcn gqcn added the done This issue is done, which may be release in next version. label Feb 1, 2024
gqcn added a commit that referenced this issue Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. done This issue is done, which may be release in next version. enhancement help wanted
Projects
None yet
Development

No branches or pull requests

2 participants