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

(超级月经贴) Should use AST code refactoring some gf gen commands. #3067

Closed
oldme-git opened this issue Oct 19, 2023 · 9 comments · Fixed by #3470 or #3488
Closed

(超级月经贴) Should use AST code refactoring some gf gen commands. #3067

oldme-git opened this issue Oct 19, 2023 · 9 comments · Fixed by #3470 or #3488
Labels
CLI about gf cli discuss We need discuss to make decision. inactive

Comments

@oldme-git
Copy link
Member

oldme-git commented Oct 19, 2023

Now, gf-cli uses regex to get file content, but this is not precision.
PR #3066 has finished some.

@gqcn
Copy link
Member

gqcn commented Oct 19, 2023

@oldme-git Is it slower using codes AST than text regular expression? Some auto generating feature, for example gen service, is sensitive to generating time duration.

@gqcn gqcn added the discuss We need discuss to make decision. label Oct 19, 2023
@oldme-git
Copy link
Member Author

@gqcn AST is slower than Regex after testing. Why gen service, sensitive to generating time duration.

@oldme-git
Copy link
Member Author

oldme-git commented Oct 21, 2023

func BenchmarkOfAST(b *testing.B) {
	for i := 0; i < b.N; i++ {
		utils.GetStructs("./file.go")
	}
}

func BenchmarkOfRegex(b *testing.B) {
	PatternApiDefinition := `type\s+(\w+)Req\s+struct\s+{([\s\S]+?)}`
	for i := 0; i < b.N; i++ {
		fileContent := gfile.GetContents("./file.go")
		gregex.MatchAllString(PatternApiDefinition, fileContent)
	}
}

@gqcn
Copy link
Member

gqcn commented Oct 23, 2023

@gqcn AST is slower than Regex after testing. Why gen service, sensitive to generating time duration.

The command gen service can automatically generate go files using file watching in Goland IDE.
image

@gqcn
Copy link
Member

gqcn commented Oct 23, 2023

func BenchmarkOfAST(b *testing.B) {
	for i := 0; i < b.N; i++ {
		utils.GetStructs("./file.go")
	}
}

func BenchmarkOfRegex(b *testing.B) {
	PatternApiDefinition := `type\s+(\w+)Req\s+struct\s+{([\s\S]+?)}`
	for i := 0; i < b.N; i++ {
		fileContent := gfile.GetContents("./file.go")
		gregex.MatchAllString(PatternApiDefinition, fileContent)
	}
}

Please paste the result.

@oldme-git
Copy link
Member Author

@gqcn AST is slower than Regex, but the 'gf gen' command scans all files after changing one file. This slows down the file generation process. If we make improvements and opt for AST, we can achieve greater precision and maintain an acceptable generation time.
AST是比正则要慢一些的,但是现在Goland IDE的监控是改变一个文件,会去正则所有的文件。如果只去匹配这个修改的文件,并且采用AST,这样是不是更好?

@github-actions github-actions bot removed the inactive label Nov 2, 2023
@gqcn
Copy link
Member

gqcn commented Nov 9, 2023

@gqcn AST is slower than Regex, but the 'gf gen' command scans all files after changing one file. This slows down the file generation process. If we make improvements and opt for AST, we can achieve greater precision and maintain an acceptable generation time.
AST是比正则要慢一些的,但是现在Goland IDE的监控是改变一个文件,会去正则所有的文件。如果只去匹配这个修改的文件,并且采用AST,这样是不是更好?

文件的监控虽然能监控到修改了一个文件,但是在代码生成的期间可能用户又改了其他文件,这样会造成生成得代码会有遗漏。所以当前工具的代码自动生成,会每次重新遍历目录,并且判断生成的内容和当前目标文件内容是否一致,判断是否覆盖生成。

@oldme-git
Copy link
Member Author

@gqcn 很多 issue 都提到了 gf gen 匹配不精确的问题,已经是月经贴了,所以我关闭了所有此类的问题,统一提到这里讨论。
正则无法适应五花八门的各类代码,在无法替换成 AST 的情况下,当前我能想到的好办法:在文档中去引导 api 和 sevice 的规范

@Issues-translate-bot
Copy link

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


@gqcn Many issues have mentioned the problem of inaccurate gf gen matching, which is already for menstrual patches, so I have closed all such issues and mentioned them here for discussion.
Regular expressions cannot adapt to all kinds of codes. When it cannot be replaced by AST, the best way I can think of is to guide the specifications of api and service in the document.

@github-actions github-actions bot removed the inactive label Feb 12, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 20, 2024
@oldme-git oldme-git reopened this Mar 21, 2024
@oldme-git oldme-git changed the title Should use AST code refactoring some gf gen commands. (超级月经贴) Should use AST code refactoring some gf gen commands. Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLI about gf cli discuss We need discuss to make decision. inactive
Projects
None yet
4 participants