diff --git a/trial/cocago/cocago_builder.go b/trial/cocago/cocago_builder.go index 570d25eb2f897254c47f9bdc16c72c0f26f7482f..054bcc42a2d4e2f3789fdb0e1976611d3b0b7579 100644 --- a/trial/cocago/cocago_builder.go +++ b/trial/cocago/cocago_builder.go @@ -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{ diff --git a/trial/cocago/cocago_parser.go b/trial/cocago/cocago_parser.go index b57fcbec65fc42bd067e36ab263863e6717d94db..a97c8c25f4aafad85473f0cb2106d6e07eb4ddcf 100644 --- a/trial/cocago/cocago_parser.go +++ b/trial/cocago/cocago_parser.go @@ -1,12 +1,15 @@ 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, ¤tFile) 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) } - diff --git a/trial/cocago/cocago_parser_test.go b/trial/cocago/cocago_parser_test.go index f4f02c63bfc3160e39db8781b03a3150a3fc9346..5a2d44288079776d896c06dc35c6a7ef73fe4130 100644 --- a/trial/cocago/cocago_parser_test.go +++ b/trial/cocago/cocago_parser_test.go @@ -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) } }) diff --git a/trial/pkg/application/processor/process.go b/trial/pkg/application/processor/process.go index be17f51b57b6e6040a74bc90e4c10aff7bf1beab..b30b6e2d7c0c11298d95240b4d68a439b07039f7 100644 --- a/trial/pkg/application/processor/process.go +++ b/trial/pkg/application/processor/process.go @@ -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) { diff --git a/trial/pkg/application/processor/process_test.go b/trial/pkg/application/processor/process_test.go index 46b18eb12454c5278ad104f0e6f88f4e47e96cc7..f254ef616bd7c7338167303d1d11108139da3db6 100644 --- a/trial/pkg/application/processor/process_test.go +++ b/trial/pkg/application/processor/process_test.go @@ -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 diff --git a/trial/tcmd/analysis_go.go b/trial/tcmd/analysis_go.go index 9226a4caea0e8ec7cc50f2fdcf1dfec772de218a..7c8e7c9abc1ab677c427bada352f594e00ef483a 100644 --- a/trial/tcmd/analysis_go.go +++ b/trial/tcmd/analysis_go.go @@ -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)