Skip to content

Conversation

oldme-git
Copy link
Member

#3063
之前使用正则表达式获取结构体信息,现在改为使用 AST 获取结构体信息,这样更为合理和精确。

@whileW
Copy link

whileW commented Oct 19, 2023

AST 可以用 ast.Inspect 去遍历,是不是更好一点
像这样

ast.Inspect(node, func(n ast.Node) bool {
				if v, ok := n.(*ast.TypeSpec); ok {
					if !gstr.Contains(v.Name.Name, "Req") {
						return false
					}

					if _, ok := v.Type.(*ast.StructType); !ok {
						return false
					}

					if err := printer.Fprint(&buf, fileSet, v.Type); err != nil {
						return nil, err
					}

					structsInfo[v.Name.Name] = buf.String()
					return false
				}
				return true
			})

@Issues-translate-bot
Copy link

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


AST can be traversed using ast.Inspect. Isn’t it better?
like this

ast.Inspect(node, func(n ast.Node) bool {
if v, ok := n.(*ast.TypeSpec); ok {
if !gstr.Contains(v.Name.Name, "Req") {
return false
}

if _, ok := v.Type.(*ast.StructType); !ok {
return false
}

if err := printer.Fprint(&buf, fileSet, v.Type); err != nil {
return nil, err
}

structsInfo[v.Name.Name] = buf.String()
return false
}
return true
})

// ignore struct name that match a request, but has no g.Meta in its body.
if !gstr.Contains(structBody, `g.Meta`) {
continue
}
// remove end "Req"
methodName = gstr.TrimRight(methodName, "Req")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gstr.TrimRightStr

@gqcn
Copy link
Member

gqcn commented Oct 19, 2023

@oldme-git It is awesome!

@Issues-translate-bot
Copy link

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


@oldme-git It is awesome!

@gqcn
Copy link
Member

gqcn commented Oct 19, 2023

@oldme-git There's another important improvement: adding unit testing case for command gen ctrl. Without a unit testing case, it is hard to validate this pr working as expect or not. There's similar reference codes https://github.com/gogf/gf/blob/master/cmd/gf/internal/cmd/cmd_gen_dao_test.go

Copy link
Member

@gqcn gqcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the comments.

@oldme-git
Copy link
Member Author

@whileW 我觉得使用for循环比Inspect更加的清晰灵活,而且GetStructs()是封装的方法,不能把"Req"直接拿进来判断

@Issues-translate-bot
Copy link

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


@whileW I think using a for loop is clearer and more flexible than Inspect, and GetStructs() is an encapsulated method, and "Req" cannot be directly used for judgment.

@whileW
Copy link

whileW commented Oct 19, 2023

@whileW 我觉得使用for循环比Inspect更加的清晰灵活,而且GetStructs()是封装的方法,不能把"Req"直接拿进来判断

好吧

@Issues-translate-bot
Copy link

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


@whileW I think using a for loop is clearer and more flexible than Inspect, and GetStructs() is an encapsulated method, and "Req" cannot be directly used for judgment.

All right

@oldme-git
Copy link
Member Author

@gqcn 强哥,你看一下geo ctrl的单测

@Issues-translate-bot
Copy link

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


@gqcn Brother Qiang, take a look at the single test of geo ctrl

@oldme-git oldme-git requested a review from gqcn October 25, 2023 11:31

import "context"

var ctx = context.Background()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不用单独增加这个文件,可以将这个变量写到cmd_gen_ctrl_test.go文件中。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个变量被cmd_gen_dao_test.go占用了

type IArticleV2 interface {
Create(ctx context.Context, req *v2.CreateReq) (res *v2.CreateRes, err error)
Update(ctx context.Context, req *v2.UpdateReq) (res *v2.UpdateRes, err error)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要把这个文件还有控制器目录中,生成的文件都加入.gitignore里面,不要直接提交。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的文件用作expect

@oldme-git oldme-git closed this Oct 31, 2023
@oldme-git oldme-git deleted the enhance/gf-gen-ctrl-2 branch October 31, 2023 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants