diff --git a/goast.go b/goast.go index aeb3fe8..0c6049c 100644 --- a/goast.go +++ b/goast.go @@ -10,6 +10,7 @@ import ( type Parser struct { source *os.File + debug bool } type Defines struct { @@ -39,7 +40,9 @@ func (ps *Parser) Parse() (*Defines, error) { return nil, err } - //ast.Print(tokenSet, astFile) + if ps.debug { + _ = ast.Print(tokenSet, astFile) + } var defines = &Defines{ variable: make(map[string]struct{}), @@ -86,6 +89,10 @@ func (ps *Parser) Parse() (*Defines, error) { return defines, nil } +func (ps *Parser) Debug() { + ps.debug = true +} + func (d *Defines) ExistVariable(name string) bool { _, exist := d.variable[name] return exist diff --git a/goast_test.go b/goast_test.go index d45bd11..c4c10aa 100644 --- a/goast_test.go +++ b/goast_test.go @@ -2,14 +2,15 @@ package goast_test import ( "fmt" + "gitter.top/coco/goast" "testing" "github.com/stretchr/testify/assert" - "gitter.top/coco/goast" ) func TestParser_Parse(t *testing.T) { parser, err := goast.NewParser("./testdata/main.go") + parser.Debug() assert.NoError(t, err) defines, err := parser.Parse() assert.NoError(t, err) diff --git a/testdata/main.go b/testdata/main.go index 5907569..3af0d78 100644 --- a/testdata/main.go +++ b/testdata/main.go @@ -1,19 +1,26 @@ package goast -import ( - _ "github.com/stretchr/testify" -) +import "gitter.top/coco/coco/core" -var a = 1 +type ExampleService struct{} -const d = "world" +// IDE: ExampleService implemented main.ExampleServiceImpl interface +var _ main.ExampleServiceImpl = (*ExampleService)(nil) -func hello() { +// ExampleCall1 test rpc +func (receiver *ExampleService) ExampleCall1(ctx *core.Context, req *main.ExampleMessage1) (resp *main.ReturnType, err error) { + resp = new(main.ReturnType) + // TODO impl... + + return resp, nil } -type c int +// ExampleCall2 +func (receiver *ExampleService) ExampleCall2(ctx *core.Context, req *main.ExampleMessage2) (resp *main.ReturnType, err error) { + resp = new(main.ReturnType) -func (cc c) name() { + // TODO impl... + return resp, nil } diff --git a/util.go b/util.go index 2760d10..dac6413 100644 --- a/util.go +++ b/util.go @@ -4,7 +4,10 @@ import "go/ast" // getKVFromImportSpec get import name and package path from *ast.ImportSpec func getKVFromImportSpec(impt *ast.ImportSpec) (string, string) { - name := impt.Name.Name + var name string + if impt.Name != nil { + name = impt.Name.Name + } pkg := impt.Path.Value return name, pkg } @@ -12,20 +15,39 @@ func getKVFromImportSpec(impt *ast.ImportSpec) (string, string) { // getKVFromValueSpec get const/variable name and value from *ast.ValueSpec func getKVFromValueSpec(spec *ast.ValueSpec) (string, string) { name := spec.Names[0].Name - value := spec.Values[0].(*ast.BasicLit).Value + var value string + switch spec.Values[0].(type) { + case *ast.BasicLit: + value = spec.Values[0].(*ast.BasicLit).Value + case *ast.CallExpr: + value = "@call" + } return name, value } // getKVFromTypeSpec get type name and base type from *ast.TypeSpec func getKVFromTypeSpec(spec *ast.TypeSpec) (string, string) { name := spec.Name.Name - value := spec.Type.(*ast.Ident).Name + var value string + switch spec.Type.(type) { + case *ast.Ident: + value = spec.Type.(*ast.Ident).Name + case *ast.StructType: + value = "@raw" + } return name, value } // getMethodNameFromReceiveField get method type and receiver name from Recv field func getMethodNameFromReceiveField(recv *ast.FieldList) (string, string) { - methodType := recv.List[0].Type.(*ast.Ident).Name - receiver := recv.List[0].Names[0].Name + var methodType, receiver string + switch recv.List[0].Type.(type) { + case *ast.Ident: + methodType = recv.List[0].Type.(*ast.Ident).Name + case *ast.StarExpr: + methodType = recv.List[0].Type.(*ast.StarExpr).X.(*ast.Ident).Name + } + + receiver = recv.List[0].Names[0].Name return methodType, receiver }