Skip to content

Commit

Permalink
Merge branch 'main' into optimize/remove_useless_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean authored Jun 25, 2024
2 parents ede0523 + 58c2f2e commit e267f0b
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 6 deletions.
35 changes: 35 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,41 @@ func search(file, dir string, includeDirs []string) (string, error) {
return file, &os.PathError{Op: "search", Path: file, Err: os.ErrNotExist}
}

// ParseBatchString parses a group of string content and returns an AST.
// IDLContent is a map, which's key is IDLPath and value is IDL content.
func ParseBatchString(mainIDLFilePath string, IDLFileContentMap map[string]string, includeDirs []string) (*Thrift, error) {
thriftMap := make(map[string]*Thrift)
return parseBatchStringRecursively(mainIDLFilePath, includeDirs, thriftMap, IDLFileContentMap)
}

// doParseBatchString
func parseBatchStringRecursively(path string, includeDirs []string, thriftMap map[string]*Thrift, IDLFileContentMap map[string]string) (*Thrift, error) {

bs, ok := IDLFileContentMap[path]
if !ok {
return nil, fmt.Errorf("no idl found for: %s\n", path)
}
if t, ok := thriftMap[path]; ok {
return t, nil
}

t, err := parseString(path, bs, includeDirs)
if err != nil {
return nil, fmt.Errorf("parse %s err: %w\n", path, err)
}
thriftMap[path] = t
dir := filepath.Dir(path)
for _, inc := range t.Includes {
incPath := filepath.Join(dir, inc.Path)
t, err := parseBatchStringRecursively(incPath, includeDirs, thriftMap, IDLFileContentMap)
if err != nil {
return nil, err
}
inc.Reference = t
}
return t, nil
}

// ParseFile parses a thrift file and returns an AST.
// If recursive is true, then the include IDLs are parsed recursively as well.
func ParseFile(path string, includeDirs []string, recursive bool) (*Thrift, error) {
Expand Down
18 changes: 12 additions & 6 deletions tool/trimmer/trim/trimmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,30 @@ func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming bool, mat
}
}
trimmer.trimMethods[i], err = regexp2.Compile(trimMethods[i], 0)
check(err)
if err != nil {
return 0, 0, err
}
}
trimmer.preservedStructs = preservedStructs
trimmer.countStructs(ast)
trimmer.markAST(ast)
trimmer.traversal(ast, ast.Filename)
if path := parser.CircleDetect(ast); len(path) > 0 {
check(fmt.Errorf("found include circle:\n\t%s", path))
return 0, 0, 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))
if err != nil {
return 0, 0, err
}
err = semantic.ResolveSymbols(ast)
if err != nil {
return 0, 0, err
}

for i, method := range trimMethods {
if !trimmer.trimMethodValid[i] {
println("err: method", method, "not found!")
os.Exit(2)
return 0, 0, fmt.Errorf("err: method %s not found!\n", method)
}
}

Expand Down
83 changes: 83 additions & 0 deletions tool/trimmer/trim/trimmer_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package trim

import (
"fmt"

"github.com/cloudwego/thriftgo/parser"
"github.com/cloudwego/thriftgo/semantic"
"github.com/cloudwego/thriftgo/tool/trimmer/dump"
)

// TrimBatchContent receives a group of thrift idl as map[path]content and mainIDLPath, and return the result as the same format.
func TrimBatchContent(mainIDLFilePath string, IDLFileContentMap map[string]string) (map[string]string, error) {
return TrimBatchContentWithConfig(mainIDLFilePath, IDLFileContentMap, TrimASTArg{
TrimMethods: nil,
Preserve: nil,
MatchGoName: nil,
})
}

// TrimBatchContentWithConfig does the same work with TrimBatchContent, but can extra receive a trimArgs
func TrimBatchContentWithConfig(mainIDLFilePath string, IDLFileContentMap map[string]string, trimArgs TrimASTArg) (map[string]string, error) {
ast, err := parser.ParseBatchString(mainIDLFilePath, IDLFileContentMap, nil)
if err != nil {
return nil, err
}

if path := parser.CircleDetect(ast); len(path) > 0 {
return nil, fmt.Errorf("found include circle:\n\t%s\n", path)
}
checker := semantic.NewChecker(semantic.Options{FixWarnings: true})
_, err = checker.CheckAll(ast)
if err != nil {
return nil, err
}
err = semantic.ResolveSymbols(ast)
if err != nil {
return nil, err
}

trimArgs.Ast = ast
_, _, err = TrimAST(&trimArgs)
if err != nil {
return nil, err
}

trimmedContent := map[string]string{}

err = recursiveDump(ast, trimmedContent)
if err != nil {
return nil, err
}
return trimmedContent, nil
}

func recursiveDump(ast *parser.Thrift, trimmedContent map[string]string) error {
main, err := dump.DumpIDL(ast)
if err != nil {
return err
}
trimmedContent[ast.Filename] = main

for _, inc := range ast.Includes {
err = recursiveDump(inc.Reference, trimmedContent)
if err != nil {
return err
}
}
return nil
}

0 comments on commit e267f0b

Please sign in to comment.