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

After upgraded to v2.5.2, defined as "*", but it should be named with "Res" suffix like "XxxRes" #2955

Closed
mason51 opened this issue Sep 11, 2023 · 9 comments
Labels

Comments

@mason51
Copy link
Contributor

mason51 commented Sep 11, 2023

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

1.20.4

2. What version of GoFrame are you using?

v2.5.2

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

Yes

4. What did you do?

type MenuListReq struct {
	g.Meta `path:"/user/menu-list" method:"get" summary:"菜单列表" tags:"用户"`
}
type MenuListRes struct {
	Title    string         `json:"title"  dc:"菜单名称"`
	Path     string         `json:"path"  dc:"菜单地址"`
	Children []MenuListData `json:"children"`
}

func (c *cUser) MenuList(ctx context.Context, req *user.MenuListReq) (res *[]user.MenuListRes, err error) {
	return
}

5. What did you expect to see?

6. What did you see instead?

在 v2.5.1 的时候, res []user.MenuListRes 是可以用的, 但升级到 v2.5.2, 就会报错.
invalid struct naming for response: defined as "
", but it should be named with "Res" suffix like "XxxRes"

同样, 当前最新版的 v2.5.3 也会报错.

如果, 我把 res *[]user.MenuListRes 改成 res *user.MenuListRes, 也就是不用数组, 就不报错.

所以, 请问这是 gf 的 bug? 还是后续不能再用数组?

@spenice
Copy link

spenice commented Sep 12, 2023

目测是支持泛型了,就不能用 [] 了
控制器只允许两种 "func(*ghttp.Request)" or "func(context.Context, *BizReq)(*BizRes, error)" is required
源码上 变量 是通过 反射获取 in 的参数个数 和参数类型来判断的
返回类型 是字符串判断你的请求是否为 “xxxReq” 和 “xxxRes”
支持泛型后,会将泛型部分移除掉,即保留中括号左边的字符串,导致获取不到你的真正的返回类型
你可以试下,res []user.MenuListRes 会抛出 defined as ""
res []*user.MenuListRes 会抛出 defined as ""

下面是部分源码

// trimGeneric removes type definitions string from response type name if generic
func trimGeneric(structName string) string {
	var (
		leftBraceIndex  = strings.LastIndex(structName, "[")
		rightBraceIndex = strings.LastIndex(structName, "]")
	)
	if leftBraceIndex == -1 && rightBraceIndex == -1 {
		// no generic type usage.
		return structName
	}
	return structName[:leftBraceIndex]
}

@mason51
Copy link
Contributor Author

mason51 commented Sep 12, 2023

如果不能用[]了, 后续想升级gf最新版本, 好几十个接口要改

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


If [] cannot be used anymore, I want to upgrade to the latest version of gf later. Dozens of interfaces need to be changed.

@spenice
Copy link

spenice commented Sep 12, 2023 via email

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Hello, I have received your letter

@spenice
Copy link

spenice commented Sep 12, 2023

如果不能用[]了, 后续想升级gf最新版本, 好几十个接口要改

将你的 返回类型 再包装成一个新的类型

你原来的 res []user.MenuListRes 包装成
type UserMenuListRes []user.MenuListRes
然后将新的 UserMenuListRes 做你的返回值类型

或者你所有的 返回 xxxRes 都使用一个,然后将 responseHandler 跟着修改
下面是一个小demo
Snipaste_2023-09-12_13-38-50
image

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


If [] cannot be used anymore, and I want to upgrade to the latest version of gf later, dozens of interfaces need to be changed.

Repackage your return type into a new type

Your original res []user.MenuListRes is wrapped into
type UserMenuListRes []user.MenuListRes
Then make the new UserMenuListRes your return value type

Or use one for all your returned xxxRes, and then modify the responseHandler accordingly
Below is a small demo
Snipaste_2023-09-12_13-38-50
image

@gqcn gqcn added the question label Sep 13, 2023
@gqcn
Copy link
Member

gqcn commented Sep 13, 2023

如果不能用[]了, 后续想升级gf最新版本, 好几十个接口要改

将你的 返回类型 再包装成一个新的类型

你原来的 res []user.MenuListRes 包装成 type UserMenuListRes []user.MenuListRes 然后将新的 UserMenuListRes 做你的返回值类型

或者你所有的 返回 xxxRes 都使用一个,然后将 responseHandler 跟着修改 下面是一个小demo Snipaste_2023-09-12_13-38-50 image

@mason51 对,参考这个来修改吧,其实使用正则来做批量替换也可以实现的。

@gqcn gqcn closed this as completed Sep 13, 2023
@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


If [] cannot be used anymore, and I want to upgrade to the latest version of gf later, dozens of interfaces need to be changed.

Rewrap your return type into a new type

Wrap your original res []user.MenuListRes into type UserMenuListRes []user.MenuListRes and then use the new UserMenuListRes as your return value type

Or you use one for all returned xxxRes, and then modify the responseHandler accordingly. The following is a small demo! [Snipaste_2023-09-12_13-38-50](https://user-images.githubusercontent.com/40162682/267221609- 04247cce-2b4e-4510-b491-346e4f99607a.png) image

@mason51 Yes, refer to this to modify it. In fact, batch replacement can also be achieved using regular expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants