This commit is contained in:
Michael McLoughlin
2018-12-06 21:58:51 -08:00
parent 676ec39c51
commit c86ef5ecae
4 changed files with 124 additions and 6 deletions

22
gotypes/gotypes.go Normal file
View File

@@ -0,0 +1,22 @@
package gotypes
import (
"errors"
"go/token"
"go/types"
)
func ParseSignature(expr string) (*types.Signature, error) {
tv, err := types.Eval(token.NewFileSet(), nil, token.NoPos, expr)
if err != nil {
return nil, err
}
if tv.Value != nil {
return nil, errors.New("signature expression should have nil value")
}
s, ok := tv.Type.(*types.Signature)
if !ok {
return nil, errors.New("provided type is not a function signature")
}
return s, nil
}