github.com/keysonzzz/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgGoSource/FuncDecl.go.bak (about)

     1  package kmgGoSource
     2  
     3  import (
     4  	"go/ast"
     5  	"go/doc"
     6  	"reflect"
     7  )
     8  
     9  //golang missing function decl of parameter name
    10  //it is just too complex to get type info...
    11  type FuncDecl struct {
    12  	ParamNames []string //paramNames
    13  	ResultName []string //resultNames
    14  }
    15  
    16  func (funcDecl *FuncDecl) GetReflectFuncDecl(rfun reflect.Type, isMethod bool) (*ReflectFuncDecl, error) {
    17  	return NewReflectFuncDecl(funcDecl, rfun, isMethod)
    18  }
    19  func NewFuncDeclFromDocFunc(docfunc *doc.Func) *FuncDecl {
    20  	decl := &FuncDecl{}
    21  	funcType := docfunc.Decl.Type
    22  	for _, p := range funcType.Params.List {
    23  		decl.ParamNames = append(decl.ParamNames, getNameFromAstField(p)...)
    24  	}
    25  	if funcType.Results != nil {
    26  		for _, p := range funcType.Results.List {
    27  			decl.ResultName = append(decl.ResultName, getNameFromAstField(p)...)
    28  		}
    29  	}
    30  	return decl
    31  }
    32  
    33  func getNameFromAstField(astField *ast.Field) []string {
    34  	output := []string{}
    35  	//some thing like "func test1(a,b int)"->astField->"a,b"
    36  	for _, name := range astField.Names {
    37  		output = append(output, name.Name)
    38  	}
    39  	return output
    40  }