Files
avo/gotypes/gotypes.go

23 lines
462 B
Go
Raw Normal View History

2018-12-06 21:58:51 -08:00
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
}