impast

package module
v0.0.0-...-a3e917d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 13 Imported by: 0

README

impast

Build Status Maintainability Test Coverage

A library for package AST importing.

Installation

go get -u github.com/orisano/impast

How to use

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
}

Useful commands

interfacer

struct to interface command

Installation
go get -u github.com/orisano/impast/cmd/interfacer
How to use
$ interfacer -out HTTPClient net/http.Client
type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
	Get(url string) (resp *http.Response, err error)
	Head(url string) (resp *http.Response, err error)
	Post(url, contentType string, body io.Reader) (resp *http.Response, err error)
	PostForm(url string, data url.Values) (resp *http.Response, err error)
}
mocker

generate mock command

Installation
go get -u github.com/orisano/impast/cmd/mocker
How to use
$ 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)
}
stuber

generate stub command

Installation
go get -u github.com/orisano/impast/cmd/stuber
How to use
$ 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")
}

Author

Nao Yonashiro (@orisano)

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	PackageNotFound = errors.New("package not found")
	TypeNotFound    = errors.New("type not found")
)

Functions

func AutoNaming

func AutoNaming(ft *ast.FuncType) *ast.FuncType

func ExportFields

func ExportFields(pkg *ast.Package, fields *ast.FieldList) *ast.FieldList

func ExportFunc

func ExportFunc(pkg *ast.Package, fn *ast.FuncDecl) *ast.FuncDecl

func ExportType

func ExportType(pkg *ast.Package, expr ast.Expr) ast.Expr

func FindInterface

func FindInterface(pkg *ast.Package, name string) *ast.InterfaceType

func FindStruct

func FindStruct(pkg *ast.Package, name string) *ast.StructType

func FindTypeByName

func FindTypeByName(pkg *ast.Package, name string) ast.Expr

func GetMethods

func GetMethods(pkg *ast.Package, name string) []*ast.FuncDecl

func GetMethodsDeep

func GetMethodsDeep(pkg *ast.Package, name string) ([]*ast.FuncDecl, error)

func GetRequires

func GetRequires(it *ast.InterfaceType) []*ast.Field

func ImportPackage

func ImportPackage(importPath string) (*ast.Package, error)
Example
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

func ResolvePackage

func ResolvePackage(f *ast.File, name string) (*ast.Package, error)

func ResolveType

func ResolveType(f *ast.File, expr ast.Expr) (*ast.Package, string, error)

func ScanDecl

func ScanDecl(pkg *ast.Package, f func(ast.Decl) bool)

func TypeName

func TypeName(expr ast.Expr) string

Types

type Importer

type Importer struct {
	EnableCache bool
	// contains filtered or unexported fields
}
var DefaultImporter Importer

func (*Importer) GetMethodsDeep

func (i *Importer) GetMethodsDeep(pkg *ast.Package, name string) ([]*ast.FuncDecl, error)

func (*Importer) ImportPackage

func (i *Importer) ImportPackage(importPath string) (*ast.Package, error)

func (*Importer) Load

func (i *Importer) Load(pkgs map[string]*ast.Package)

func (*Importer) Loaded

func (i *Importer) Loaded() []string

func (*Importer) ResolvePackage

func (i *Importer) ResolvePackage(f *ast.File, name string) (*ast.Package, error)

func (*Importer) ResolveType

func (i *Importer) ResolveType(f *ast.File, expr ast.Expr) (*ast.Package, string, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL