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

optimize: trimmer config idl file preserve #244

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tool/trimmer/trim/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type YamlArguments struct {
Preserve *bool `yaml:"preserve,omitempty"`
PreservedStructs []string `yaml:"preserved_structs,omitempty"`
MatchGoName *bool `yaml:"match_go_name,omitempty"`
PreservedFiles []string `yaml:"preserved_files,omitempty"`
}

func ParseYamlConfig(path string) *YamlArguments {
Expand Down
24 changes: 23 additions & 1 deletion tool/trimmer/trim/mark.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,5 +312,27 @@ func (t *Trimmer) checkPreserve(theStruct *parser.StructLike) bool {
return true
}
}
return t.preserveRegex.MatchString(strings.ToLower(theStruct.ReservedComments))
if t.preserveRegex.MatchString(strings.ToLower(theStruct.ReservedComments)) {
return true
}
// 如果整个文件也是要保留的,那么里面的结构体也不删除
if t.preserveFileStructs[theStruct] {
return true
}
return false
}

func (t *Trimmer) loadPreserveFiles(ast *parser.Thrift, preserveFiles []string) {
preserveFilesMap := map[string]bool{}
for _, fn := range preserveFiles {
preserveFilesMap[fn] = true
}
t.preserveFileStructs = map[*parser.StructLike]bool{}
for th := range ast.DepthFirstSearch() {
if preserveFilesMap[th.Filename] {
for _, st := range ast.Structs {
t.preserveFileStructs[st] = true
}
}
}
}
32 changes: 20 additions & 12 deletions tool/trimmer/trim/trimmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ type Trimmer struct {
marks map[string]map[interface{}]bool
outDir string
// use -m
trimMethods []*regexp2.Regexp
matchGoName bool
trimMethodValid []bool
preserveRegex *regexp.Regexp
forceTrimming bool
preservedStructs []string
structsTrimmed int
fieldsTrimmed int
extServices []*parser.Service
trimMethods []*regexp2.Regexp
matchGoName bool
trimMethodValid []bool
preserveRegex *regexp.Regexp
forceTrimming bool
preservedStructs []string
structsTrimmed int
fieldsTrimmed int
extServices []*parser.Service
PreservedFiles []string
preserveFileStructs map[*parser.StructLike]bool
}

type TrimASTArg struct {
Expand All @@ -53,6 +55,7 @@ type TrimASTArg struct {
Preserve *bool
MatchGoName *bool
PreserveStructs []string
PreservedFiles []string
}

type TrimResultInfo struct {
Expand Down Expand Up @@ -80,8 +83,9 @@ func (t *TrimResultInfo) FieldTrimmedPercentage() float64 {

// TrimAST parse the cfg and trim the single AST
func TrimAST(arg *TrimASTArg) (trimResultInfo *TrimResultInfo, err error) {
var preservedStructs []string
var preservedStructs, preservedFiles []string
preservedStructs = arg.PreserveStructs
preservedFiles = arg.PreservedFiles
if wd, err := dir_utils.Getwd(); err == nil {
cfg := ParseYamlConfig(wd)
if cfg != nil {
Expand All @@ -98,6 +102,9 @@ func TrimAST(arg *TrimASTArg) (trimResultInfo *TrimResultInfo, err error) {
if len(preservedStructs) == 0 {
preservedStructs = cfg.PreservedStructs
}
if len(preservedFiles) == 0 {
preservedFiles = cfg.PreservedFiles
}
}
}
forceTrim := false
Expand All @@ -108,11 +115,11 @@ func TrimAST(arg *TrimASTArg) (trimResultInfo *TrimResultInfo, err error) {
if arg.MatchGoName != nil {
matchGoName = *arg.MatchGoName
}
return doTrimAST(arg.Ast, arg.TrimMethods, forceTrim, matchGoName, preservedStructs)
return doTrimAST(arg.Ast, arg.TrimMethods, forceTrim, matchGoName, preservedStructs, preservedFiles)
}

// doTrimAST trim the single AST, pass method names if -m specified
func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming, matchGoName bool, preservedStructs []string) (
func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming, matchGoName bool, preservedStructs, preserveFiles []string) (
trimResultInfo *TrimResultInfo, err error) {
trimmer, err := newTrimmer(nil, "")
if err != nil {
Expand Down Expand Up @@ -143,6 +150,7 @@ func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming, matchGoN
trimmer.countStructs(ast)
originStructsNum := trimmer.structsTrimmed
originFieldNum := trimmer.fieldsTrimmed
trimmer.loadPreserveFiles(ast, preserveFiles)
trimmer.markAST(ast)
trimmer.traversal(ast, ast.Filename)
if path := parser.CircleDetect(ast); len(path) > 0 {
Expand Down
Loading