Skip to content

Commit

Permalink
optimize: trimmer config idl file preserve
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean committed Dec 13, 2024
1 parent 848b104 commit ede37de
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
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] {

Check failure on line 319 in tool/trimmer/trim/mark.go

View workflow job for this annotation

GitHub Actions / build

t.preserveFileStructs undefined (type *Trimmer has no field or method preserveFileStructs)

Check failure on line 319 in tool/trimmer/trim/mark.go

View workflow job for this annotation

GitHub Actions / build

t.preserveFileStructs undefined (type *Trimmer has no field or method preserveFileStructs)

Check failure on line 319 in tool/trimmer/trim/mark.go

View workflow job for this annotation

GitHub Actions / codegen-test

t.preserveFileStructs undefined (type *Trimmer has no field or method preserveFileStructs)
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{}

Check failure on line 330 in tool/trimmer/trim/mark.go

View workflow job for this annotation

GitHub Actions / build

t.preserveFileStructs undefined (type *Trimmer has no field or method preserveFileStructs)

Check failure on line 330 in tool/trimmer/trim/mark.go

View workflow job for this annotation

GitHub Actions / build

t.preserveFileStructs undefined (type *Trimmer has no field or method preserveFileStructs)

Check failure on line 330 in tool/trimmer/trim/mark.go

View workflow job for this annotation

GitHub Actions / codegen-test

t.preserveFileStructs undefined (type *Trimmer has no field or method preserveFileStructs)
for th := range ast.DepthFirstSearch() {
if preserveFilesMap[th.Filename] {
for _, st := range ast.Structs {
t.preserveFileStructs[st] = true

Check failure on line 334 in tool/trimmer/trim/mark.go

View workflow job for this annotation

GitHub Actions / build

t.preserveFileStructs undefined (type *Trimmer has no field or method preserveFileStructs)

Check failure on line 334 in tool/trimmer/trim/mark.go

View workflow job for this annotation

GitHub Actions / build

t.preserveFileStructs undefined (type *Trimmer has no field or method preserveFileStructs)

Check failure on line 334 in tool/trimmer/trim/mark.go

View workflow job for this annotation

GitHub Actions / codegen-test

t.preserveFileStructs undefined (type *Trimmer has no field or method preserveFileStructs)
}
}
}
}
13 changes: 10 additions & 3 deletions tool/trimmer/trim/trimmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Trimmer struct {
structsTrimmed int
fieldsTrimmed int
extServices []*parser.Service
PreservedFiles []string
}

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

type TrimResultInfo struct {
Expand Down Expand Up @@ -80,8 +82,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 +101,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 +114,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 +149,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

0 comments on commit ede37de

Please sign in to comment.