Skip to content

Commit

Permalink
feat: support preserve-structs for yaml-config of trimmer tool
Browse files Browse the repository at this point in the history
  • Loading branch information
tksky1 committed Oct 24, 2023
1 parent 494cae6 commit 7afa4e3
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 30 deletions.
11 changes: 1 addition & 10 deletions generator/golang/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package golang
import (
"fmt"
"go/format"
"os"
"path/filepath"
"strings"
"text/template"
Expand Down Expand Up @@ -87,15 +86,7 @@ func (g *GoBackend) Generate(req *plugin.Request, log backend.LogFunc) *plugin.R
g.log = log
g.prepareUtilities()
if g.utils.Features().TrimIDL {
wd, _ := os.Getwd()
cfg := trim.ParseYamlConfig(wd)
var err error
if cfg == nil {
err = trim.TrimAST(req.AST, nil, false, nil)
} else {
err = trim.TrimAST(req.AST, cfg.Methods, !*cfg.Preserve, cfg.PreservedStructs)
}

err := trim.TrimAST(&trim.TrimASTArg{Ast: req.AST, TrimMethods: nil, Preserve: nil})
if err != nil {
g.log.Warn("trim error:", err.Error())
}
Expand Down
24 changes: 6 additions & 18 deletions tool/trimmer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ func main() {
os.Exit(0)
}

preserve := true
var preserveInput *bool
if a.Preserve != "" {
preserve, err = strconv.ParseBool(a.Preserve)
preserve, err := strconv.ParseBool(a.Preserve)
if err != nil {
help()
os.Exit(2)
}
preserveInput = &preserve
}

// parse file to ast
Expand All @@ -73,23 +74,10 @@ func main() {
check(err)
check(semantic.ResolveSymbols(ast))

// try parse yaml config
var preservedStructs []string
if wd, err := os.Getwd(); err == nil {
cfg := trim.ParseYamlConfig(wd)
if cfg != nil {
if len(a.Methods) == 0 && len(cfg.Methods) > 0 {
a.Methods = cfg.Methods
}
if a.Preserve == "" && !(*cfg.Preserve) {
preserve = false
}
preservedStructs = cfg.PreservedStructs
}
}

// trim ast
check(trim.TrimAST(ast, a.Methods, !preserve, preservedStructs))
check(trim.TrimAST(&trim.TrimASTArg{
Ast: ast, TrimMethods: a.Methods, Preserve: preserveInput,
}))

// dump the trimmed ast to idl
idl, err := dump.DumpIDL(ast)
Expand Down
33 changes: 31 additions & 2 deletions tool/trimmer/trim/trimmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,37 @@ type Trimmer struct {
preservedStructs []string
}

// TrimAST trim the single AST, pass method names if -m specified
func TrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming bool, preservedStructs []string) error {
type TrimASTArg struct {
Ast *parser.Thrift
TrimMethods []string
Preserve *bool
}

// TrimAST parse the cfg and trim the single AST
func TrimAST(arg *TrimASTArg) error {
var preservedStructs []string
if wd, err := os.Getwd(); err == nil {
cfg := ParseYamlConfig(wd)
if cfg != nil {
if len(arg.TrimMethods) == 0 && len(cfg.Methods) > 0 {
arg.TrimMethods = cfg.Methods
}
if arg.Preserve == nil && !(*cfg.Preserve) {
preserve := false
arg.Preserve = &preserve
}
preservedStructs = cfg.PreservedStructs
}
}
forceTrim := false
if arg.Preserve != nil {
forceTrim = !*arg.Preserve
}
return doTrimAST(arg.Ast, arg.TrimMethods, forceTrim, preservedStructs)
}

// doTrimAST trim the single AST, pass method names if -m specified
func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming bool, preservedStructs []string) error {
trimmer, err := newTrimmer(nil, "")
if err != nil {
return err
Expand Down
47 changes: 47 additions & 0 deletions tool/trimmer/trim/trimmer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,50 @@ func TestInclude(t *testing.T) {
test.Assert(t, len(ast.Includes) == 1)
test.Assert(t, ast.Includes[0].Used == nil)
}

func TestTrimMethod(t *testing.T) {
filename := filepath.Join("..", "test_cases", "tests", "dir", "dir2", "test.thrift")
ast, err := parser.ParseFile(filename, nil, true)
check(err)
if path := parser.CircleDetect(ast); len(path) > 0 {
check(fmt.Errorf("found include circle:\n\t%s", path))
}
checker := semantic.NewChecker(semantic.Options{FixWarnings: true})
_, err = checker.CheckAll(ast)
check(err)
check(semantic.ResolveSymbols(ast))

methods := make([]string, 1)
methods[0] = "func1"

err = TrimAST(&TrimASTArg{
Ast: ast,
TrimMethods: methods,
Preserve: nil,
})
check(err)
test.Assert(t, len(ast.Services[0].Functions) == 1)
}

func TestPreserve(t *testing.T) {
filename := filepath.Join("..", "test_cases", "tests", "dir", "dir2", "test.thrift")
ast, err := parser.ParseFile(filename, nil, true)
check(err)
if path := parser.CircleDetect(ast); len(path) > 0 {
check(fmt.Errorf("found include circle:\n\t%s", path))
}
checker := semantic.NewChecker(semantic.Options{FixWarnings: true})
_, err = checker.CheckAll(ast)
check(err)
check(semantic.ResolveSymbols(ast))

preserve := false

err = TrimAST(&TrimASTArg{
Ast: ast,
TrimMethods: nil,
Preserve: &preserve,
})
check(err)
test.Assert(t, len(ast.Structs) == 0)
}

0 comments on commit 7afa4e3

Please sign in to comment.