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

feat: support trimmer compose for trimming multiple IDLs #204

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DMwangnima
Copy link
Contributor

Description

Motivation and Context

Related Issue

@DMwangnima DMwangnima requested review from a team as code owners May 27, 2024 05:44
@DMwangnima DMwangnima changed the title [WIP] feat: support trimmer compose for trimming multiple IDLs feat: support trimmer compose for trimming multiple IDLs Jun 3, 2024

func parseAndCheckAST(path string, includeDirs []string, recursive bool) *parser.Thrift {
ast, err := parser.ParseFile(path, includeDirs, recursive)
check(err)
Copy link
Member

Choose a reason for hiding this comment

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

check 里面是 os.Exit 吗,统一抛出 error,在 main.go 相关的地方再处理 error 然后 os.Exit 吧,不然 sdk 调用的时候容易把进程搞炸

@@ -77,6 +76,12 @@ func (t *TrimResultInfo) FieldTrimmedPercentage() float64 {
return float64(t.FieldsTrimmed) / float64(t.FieldsTotal) * 100
}

type TrimASTWithComposeArg struct {
Copy link
Member

Choose a reason for hiding this comment

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

为啥不能直接在 TrimASTArgs 上加?要单独包一层

@@ -19,20 +19,27 @@ import (
)

// traverse and remove the unmarked part of ast
func (t *Trimmer) traversal(ast *parser.Thrift, filename string) {
func (t *Trimmer) traversal(ast *parser.Thrift) {
Copy link
Member

Choose a reason for hiding this comment

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

没太懂为啥要封一个doTraversal
另外没看到改动之后 travversal 在哪里被调用了?直接把 t.countStructs 放在 traversal 调用前执行一次就好了吧?

trimmer, err := newTrimmer(nil, "")
if err != nil {
return nil, err
func TrimASTWithCompose(arg *TrimASTWithComposeArg) (trimResultInfo *TrimResultInfo, 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.

这个为啥要改名

}
cfg := arg.Cfg
// When ReadCfgFromLocal is set, local cfg has higher priority and the passed cfg would be ignored
if arg.ReadCfgFromLocal {
Copy link
Member

Choose a reason for hiding this comment

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

读文件这些感觉可以提到上面TrimAST里,doTrimAST里就只做裁切相关的事

trimmer.trimMethodValid = make([]bool, len(trimMethods))
trimmer.forceTrimming = forceTrimming
trimmer.matchGoName = matchGoName
for i, method := range trimMethods {
Copy link
Member

Choose a reason for hiding this comment

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

这段怎么也删了呢

t.marks[ast.Filename] = make(map[interface{}]bool)
t.preProcess(ast, ast.Filename)
func (t *Trimmer) markAST(ast *parser.Thrift, arg *YamlArguments) {
t.refresh(ast, arg)
Copy link
Member

Choose a reason for hiding this comment

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

arg放到trimmer的 field 里传递可能会好些?

t.markInclude(ast.Includes[theType.Reference.Index], filename)
}

if theType.IsTypedef != nil {
t.markTypeDef(theType, baseAST, filename)
t.markTypeDef(theType, baseAST)
Copy link
Member

Choose a reason for hiding this comment

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

我看你把所有mark的 filename 都去掉了,改为从ast拿,这里我不能完全确定对不对,需要保证 filename 一定和 ast 一致。但似乎看这一行,和原来的代码行为可能就会有出入了(baseAST可能变成别的,但filename是不变的,到你的实现里,传入的filename就变了)

return
}
t.marks[filename][str] = true
t.marks[ast.Filename][str.Name] = true
Copy link
Member

Choose a reason for hiding this comment

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

原本这之前是用指针,现在改成了用string为key,是有啥必要吗?(虽然直觉看着也是唯一的)

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

Successfully merging this pull request may close these issues.

2 participants