Skip to content

Commit

Permalink
Merge branch 'main' into dev/keyi_string_json
Browse files Browse the repository at this point in the history
  • Loading branch information
xiekeyi98 authored Sep 14, 2023
2 parents b5a7eff + d13e09a commit f9f7ca2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
5 changes: 2 additions & 3 deletions tool/trimmer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ import (
"path/filepath"
"strings"

"github.com/cloudwego/thriftgo/generator"
"github.com/cloudwego/thriftgo/parser"
"github.com/cloudwego/thriftgo/semantic"
"github.com/cloudwego/thriftgo/tool/trimmer/dump"
"github.com/cloudwego/thriftgo/tool/trimmer/trim"
"github.com/cloudwego/thriftgo/version"

"github.com/cloudwego/thriftgo/generator"
)

var (
Expand Down Expand Up @@ -99,7 +98,7 @@ func main() {
os.Exit(2)
}
} else {
println("-o should be set as a valid dir to enable -r", err.Error())
println("-o should be set as a valid dir to enable -r")
os.Exit(2)
}
}
Expand Down
1 change: 1 addition & 0 deletions tool/trimmer/test_cases/sample1.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ enum Gender {
FEMALE (key = "1", key = "2", key2 = "v2")
} (a = "b")

#@PRESERvE
struct Address {
1: required string(key = "v") street
2: required string city
Expand Down
2 changes: 1 addition & 1 deletion tool/trimmer/test_cases/tests/example2.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ struct E{

}

service MyService{
service MyService extends sample1.EmployeeService{
string A(1:required E req,2:required test.TestStruct t)
}
50 changes: 49 additions & 1 deletion tool/trimmer/trim/mark.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ func (t *Trimmer) markService(svc *parser.Service, ast *parser.Thrift, filename
t.marks[filename][svc] = true
t.markFunction(function, ast, filename)
t.trimMethodValid[i] = true
continue
}
}
continue
}
t.markFunction(function, ast, filename)
}

if t.trimMethods != nil && (svc.Extends != "" || svc.Reference != nil) {
t.traceExtendMethod(svc, svc, ast, filename)
}

if svc.Extends != "" && t.marks[filename][svc] {
// handle extension
if svc.Reference != nil {
Expand Down Expand Up @@ -169,3 +172,48 @@ func (t *Trimmer) markTypeDef(theType *parser.Type, ast *parser.Thrift, filename
}
}
}

// for -m, trace the extends and find specified method to base on
func (t *Trimmer) traceExtendMethod(father, svc *parser.Service, ast *parser.Thrift, filename string) (ret bool) {
for _, function := range svc.Functions {
funcName := father.Name + "." + function.Name
for i, method := range t.trimMethods {
if funcName == method {
t.marks[filename][svc] = true
t.markFunction(function, ast, filename)
t.trimMethodValid[i] = true
ret = true
}
}
}
if svc.Extends != "" {
var nextSvc *parser.Service
var nextAst *parser.Thrift
if svc.Reference == nil {
for i, extend := range ast.Services {
if extend.Name == svc.Extends {
nextSvc = ast.Services[i]
nextAst = ast
break
}
}
} else {
for i, extend := range ast.Includes[svc.Reference.Index].Reference.Services {
if extend.Name == svc.Reference.Name {
nextSvc = ast.Includes[svc.Reference.Index].Reference.Services[i]
nextAst = ast.Includes[svc.Reference.Index].Reference
break
}
}
}
back := t.traceExtendMethod(father, nextSvc, nextAst, filename)
ret = back || ret
}
if ret {
t.marks[filename][svc] = true
if svc.Reference != nil {
t.marks[filename][ast.Includes[svc.Reference.Index]] = true
}
}
return ret
}
15 changes: 13 additions & 2 deletions tool/trimmer/trim/traversal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

package trim

import "github.com/cloudwego/thriftgo/parser"
import (
"regexp"
"strings"

"github.com/cloudwego/thriftgo/parser"
)

// traverse and remove the unmarked part of ast
func (t *Trimmer) traversal(ast *parser.Thrift, filename string) {
Expand All @@ -30,7 +35,7 @@ func (t *Trimmer) traversal(ast *parser.Thrift, filename string) {

var listStruct []*parser.StructLike
for i := range ast.Structs {
if t.marks[filename][ast.Structs[i]] {
if t.marks[filename][ast.Structs[i]] || checkPreserve(ast.Structs[i]) {
listStruct = append(listStruct, ast.Structs[i])
}
}
Expand Down Expand Up @@ -69,3 +74,9 @@ func (t *Trimmer) traversal(ast *parser.Thrift, filename string) {
}
ast.Services = listService
}

func checkPreserve(theStruct *parser.StructLike) bool {
pattern := `^[\s]*(\/\/|#)[\s]*@preserve[\s]*$`
regex := regexp.MustCompile(pattern)
return regex.MatchString(strings.ToLower(theStruct.ReservedComments))
}

0 comments on commit f9f7ca2

Please sign in to comment.