refactor: set output method

上级 51b0c4e7
......@@ -24,7 +24,7 @@ func BuildPropertyField(name string, field *ast.Field) *trial.CodeProperty {
case *ast.SelectorExpr:
typeName = getSelectorName(*typeX)
default:
fmt.Println("BuildPropertyField ArrayType", reflect.TypeOf(x.Elt))
fmt.Fprintf(output, "BuildPropertyField ArrayType %s\n", reflect.TypeOf(x.Elt))
}
case *ast.FuncType:
typeType = "Function"
......@@ -41,7 +41,7 @@ func BuildPropertyField(name string, field *ast.Field) *trial.CodeProperty {
case *ast.SelectorExpr:
typeName = getSelectorName(*x)
default:
fmt.Println("BuildPropertyField", reflect.TypeOf(x))
fmt.Fprintf(output, "BuildPropertyField %s\n", reflect.TypeOf(x))
}
property := &trial.CodeProperty{
......
package cocago
import (
"bytes"
"fmt"
"github.com/phodal/coca/pkg/domain/trial"
"go/ast"
"go/parser"
"go/token"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
)
......@@ -16,16 +19,27 @@ var currentPackage *trial.CodePackage
type CocagoParser struct {
}
var debug = false
var output io.Writer
func NewCocagoParser() *CocagoParser {
currentPackage = &trial.CodePackage{}
output = os.Stdout
return &CocagoParser{}
}
func (n *CocagoParser) SetOutput(isDebug bool) io.Writer {
output = new(bytes.Buffer)
debug = isDebug
return output
}
func (n *CocagoParser) ProcessFile(fileName string) trial.CodeFile {
absPath, _ := filepath.Abs(fileName)
content, _ := ioutil.ReadFile(absPath)
fmt.Println("process file", fileName)
fmt.Fprintf(output, "process file %s\n", fileName)
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, fileName, string(content), 0)
......@@ -75,7 +89,7 @@ func (n *CocagoParser) Visitor(f *ast.File, fset *token.FileSet, fileName string
AddInterface(x, lastIdent, &currentFile)
default:
if reflect.TypeOf(x) != nil {
fmt.Println("Visitor case ", reflect.TypeOf(x))
fmt.Fprintf(output, "Visitor case %s\n", reflect.TypeOf(x))
}
}
return true
......@@ -146,7 +160,7 @@ func BuildReceiver(x *ast.FuncDecl, recv string) string {
case *ast.Ident:
recv = x.Name
default:
fmt.Println("AddFunctionDecl", reflect.TypeOf(x))
fmt.Fprintf(output, "AddFunctionDecl %s\n", reflect.TypeOf(x))
}
}
return recv
......@@ -157,7 +171,7 @@ func BuildMethodCall(codeFunc *trial.CodeFunction, item ast.Stmt) {
case *ast.ExprStmt:
BuildMethodCallExprStmt(it, codeFunc)
default:
fmt.Println("methodCall", reflect.TypeOf(it))
fmt.Fprintf(output, "methodCall %s\n", reflect.TypeOf(it))
}
}
......@@ -206,7 +220,7 @@ func BuildExpr(expr ast.Expr) (string, string) {
}
return x.Name, name
default:
fmt.Println("BuildExpr", reflect.TypeOf(x))
fmt.Fprintf(output, "BuildExpr %s\n", reflect.TypeOf(x))
}
return "", ""
}
......@@ -245,4 +259,3 @@ func AddStructType(currentStruct trial.CodeDataStruct, x *ast.StructType, curren
currentFile.Members = append(currentFile.Members, &member)
currentFile.DataStructures = append(currentFile.DataStructures, currentStruct)
}
......@@ -18,6 +18,7 @@ var testParser *CocagoParser
func setup() {
testParser = NewCocagoParser()
testParser.SetOutput(true)
}
func shutdown() {
......@@ -60,9 +61,8 @@ func TestCocagoParser_ProcessFile(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := &CocagoParser{}
filePath := getFilePath(tt.fileName)
if got := n.ProcessFile(filePath + ".code"); !cocatest.JSONFileBytesEqual(got, filePath+".json") {
if got := testParser.ProcessFile(filePath + ".code"); !cocatest.JSONFileBytesEqual(got, filePath+".json") {
t.Errorf("ProcessFile() = %v, want %v", got, tt.fileName)
}
})
......
......@@ -8,7 +8,7 @@ import (
"sync"
)
func ProcessPackage(path string) []*trial.CodeFile {
func ProcessPackage(path string, debug bool) []*trial.CodeFile {
var wg sync.WaitGroup
var GoFileFilter = func(path string) bool {
......@@ -18,6 +18,9 @@ func ProcessPackage(path string) []*trial.CodeFile {
files := cocafile.GetFilesWithFilter(path ,GoFileFilter)
filesData := make([]*trial.CodeFile, len(files))
parser := cocago.NewCocagoParser()
if debug {
parser.SetOutput(true)
}
for i, file := range files {
wg.Add(1)
go func(i int, file string) {
......
......@@ -9,6 +9,7 @@ func Test_ProcessPackage(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
results := ProcessPackage("../../../../pkg/domain")
debug := true
results := ProcessPackage("../../../../pkg/domain", debug)
g.Expect(len(results)).To(Equal(27))
}
\ No newline at end of file
......@@ -28,6 +28,7 @@ var analysisGoCmd = &cobra.Command{
files := cocafile.GetFilesWithFilter(importPath, cocafile.GoFileFilter)
for _, file := range files {
parser := cocago.NewCocagoParser()
parser.SetOutput(true)
result := parser.ProcessFile(file)
results = append(results, result)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册