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 match method go name #199

Merged
merged 1 commit into from
Apr 29, 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
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
Loading