fix: nil pointer

This commit is contained in:
Young Xu 2023-03-18 22:59:58 +08:00
parent 1f614fadb9
commit 875a73a463
Signed by: xuthus5
GPG Key ID: A23CF9620CBB55F9
4 changed files with 52 additions and 15 deletions

View File

@ -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

View File

@ -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)

23
testdata/main.go vendored
View File

@ -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
}

32
util.go
View File

@ -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
}