Skip to content

Commit

Permalink
optimize: trimmer match method go name (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean authored Apr 29, 2024
1 parent 49e7c42 commit b4b1e45
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
5 changes: 5 additions & 0 deletions tool/trimmer/trim/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type YamlArguments struct {
Methods []string `yaml:"methods,omitempty"`
Preserve *bool `yaml:"preserve,omitempty"`
PreservedStructs []string `yaml:"preserved_structs,omitempty"`
MatchGoName *bool `yaml:"match_go_name,omitempty"`
}

func ParseYamlConfig(path string) *YamlArguments {
Expand All @@ -46,5 +47,9 @@ func ParseYamlConfig(path string) *YamlArguments {
t := true
cfg.Preserve = &t
}
if cfg.MatchGoName == nil {
t := false
cfg.MatchGoName = &t
}
return &cfg
}
15 changes: 15 additions & 0 deletions tool/trimmer/trim/mark.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ func (t *Trimmer) markAST(ast *parser.Thrift) {
t.markKeptPart(ast, ast.Filename)
}

func toGoName(input string) string {
words := strings.Split(input, "_")
var result strings.Builder
for _, word := range words {
if word != "" {
upperWord := strings.ToUpper(string(word[0])) + word[1:]
result.WriteString(upperWord)
}
}
return result.String()
}

func (t *Trimmer) markService(svc *parser.Service, ast *parser.Thrift, filename string) {
if t.marks[filename][svc] {
return
Expand All @@ -44,6 +56,9 @@ func (t *Trimmer) markService(svc *parser.Service, ast *parser.Thrift, filename
if len(t.trimMethods) != 0 {
funcName := svc.Name + "." + function.Name
for i, method := range t.trimMethods {
if t.matchGoName {
funcName = svc.Name + "." + toGoName(function.Name)
}
if ok, _ := method.MatchString(funcName); ok {
if funcName == method.String() || !strings.HasPrefix(funcName, method.String()) {
t.marks[filename][svc] = true
Expand Down
20 changes: 15 additions & 5 deletions tool/trimmer/trim/trimmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Trimmer struct {
outDir string
// use -m
trimMethods []*regexp2.Regexp
matchGoName bool
trimMethodValid []bool
preserveRegex *regexp.Regexp
forceTrimming bool
Expand All @@ -47,6 +48,7 @@ type TrimASTArg struct {
Ast *parser.Thrift
TrimMethods []string
Preserve *bool
MatchGoName *bool
}

// TrimAST parse the cfg and trim the single AST
Expand All @@ -62,18 +64,25 @@ func TrimAST(arg *TrimASTArg) (structureTrimmed int, fieldTrimmed int, err error
preserve := false
arg.Preserve = &preserve
}
if arg.MatchGoName == nil && cfg.MatchGoName != nil {
arg.MatchGoName = cfg.MatchGoName
}
preservedStructs = cfg.PreservedStructs
}
}
forceTrim := false
if arg.Preserve != nil {
forceTrim = !*arg.Preserve
}
return doTrimAST(arg.Ast, arg.TrimMethods, forceTrim, preservedStructs)
matchGoName := false
if arg.MatchGoName != nil {
matchGoName = *arg.MatchGoName
}
return doTrimAST(arg.Ast, arg.TrimMethods, forceTrim, matchGoName, preservedStructs)
}

// doTrimAST trim the single AST, pass method names if -m specified
func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming bool, preservedStructs []string) (
func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming bool, matchGoName bool, preservedStructs []string) (
structureTrimmed int, fieldTrimmed int, err error) {
trimmer, err := newTrimmer(nil, "")
if err != nil {
Expand All @@ -83,15 +92,16 @@ func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming bool, pre
trimmer.trimMethods = make([]*regexp2.Regexp, len(trimMethods))
trimmer.trimMethodValid = make([]bool, len(trimMethods))
trimmer.forceTrimming = forceTrimming
trimmer.matchGoName = matchGoName
for i, method := range trimMethods {
parts := strings.Split(method, ".")
if len(parts) < 2 {
if len(ast.Services) == 1 {
trimMethods[i] = ast.Services[0].Name + "." + method
} else {
trimMethods[i] = ast.Services[len(ast.Services)-1].Name + "." + method
// println("please specify service name!\n -m usage: -m [service_name.method_name]")
// os.Exit(2)
trimMethods[i] = ast.Services[len(ast.Services)-1].Name + "." + method
// println("please specify service name!\n -m usage: -m [service_name.method_name]")
// os.Exit(2)

}
}
Expand Down

0 comments on commit b4b1e45

Please sign in to comment.