refactor: set output method

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