library for package AST importing.
go get -u github.com/orisano/impast
package main
import (
"fmt"
"log"
"github.com/orisano/impast"
)
func main() {
pkg, err := impast.ImportPackage("io")
if err != nil {
log.Fatal(err)
}
it := impast.FindInterface(pkg, "Writer")
if it == nil {
log.Fatalf("io.Writer not found")
}
methods := impast.GetRequires(it)
for _, method := range methods {
fmt.Println(method.Names[0].Name)
}
// Output:
// Write
}
struct to interface command
go get -u github.com/orisano/impast/cmd/interfacer
$ interfacer -pkg net/http -type "*Client" -out HTTPClient
type HTTPClient interface {
Get(url string) (resp *http.Response, err error)
Do(req *http.Request) (*http.Response, error)
Post(url string, contentType string, body io.Reader) (resp *http.Response, err error)
PostForm(url string, data url.Values) (resp *http.Response, err error)
Head(url string) (resp *http.Response, err error)
}
generate mock command
go get -u github.com/orisano/impast/cmd/mocker
$ mocker -pkg io -type ReadWriter
type ReadWriterMock struct {
ReadMock func(p []byte) (n int, err error)
WriteMock func(p []byte) (n int, err error)
}
func (m *ReadWriterMock) Read(p []byte) (n int, err error) {
return m.ReadMock(p)
}
func (m *ReadWriterMock) Write(p []byte) (n int, err error) {
return m.WriteMock(p)
}
generate stub command
go get -u github.com/orisano/impast/cmd/stuber
$ stuber -pkg net -implement Conn -export -name c -type "*MyConn"
func (c *MyConn) Read(b []byte) (n int, err error) {
panic("implement me")
}
func (c *MyConn) Write(b []byte) (n int, err error) {
panic("implement me")
}
func (c *MyConn) Close() error {
panic("implement me")
}
func (c *MyConn) LocalAddr() net.Addr {
panic("implement me")
}
func (c *MyConn) RemoteAddr() net.Addr {
panic("implement me")
}
func (c *MyConn) SetDeadline(t time.Time) error {
panic("implement me")
}
func (c *MyConn) SetReadDeadline(t time.Time) error {
panic("implement me")
}
func (c *MyConn) SetWriteDeadline(t time.Time) error {
panic("implement me")
}
MIT