astinspector

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2021 License: MIT Imports: 6 Imported by: 0

README

go-astinspector

Personal project.

Doc coming soon with more features.

➡ install

go get -u github.com/kovacou/go-astinspector

➡ usage

f := astinspector.ParseFile("../go-astinspector/testdata/struct.go")
f.Structs().Names() // []string{"User", "Test"}
f.Structs("User", "Toto").Names() // []string{"User"}
f.PackageName() // testdata 

if u := f.StructByName("User"); u != nil {
    u.AddField("CustomField", "string")
    u.Name() // User

    for _, field := range u.Fields() {
        fmt.Printf("%s %s\n", field.Name(), field.Type())
    }

    // Print: 
    //
    // ID uint64
    // Name string
    // Birthday *time.Time
    // CustomField string
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field interface {
	Name() string
	Type() string
	Tags() *structtag.Tags
}

Field abstract an ast.Field.

type File

type File interface {
	StructByName(name string) Struct
	PackageName() string
	Structs(...string) StructList
}

File abstract an ast.File.

func ParseFile

func ParseFile(filename string) File

ParseFile parse the given filename.

type Struct

type Struct interface {
	Name() string
	Fields() []Field
	AddField(name, typ string) Field
	AstTypeSpec() *ast.TypeSpec
	IsValid() bool
}

Struct abstract an ast.TypeSpec

func StructByName

func StructByName(node ast.Node, name string) Struct

StructByName return a struct composed of *ast.TypeSpec.

type StructList

type StructList []Struct

StructList is a list of struct.

func Structs

func Structs(node ast.Node, names ...string) (out StructList)

Structs return a list of structs defined in the given node.

func (StructList) FindByName

func (sl StructList) FindByName(name string) Struct

FindByName return the struct matching with the given name.

func (StructList) First

func (sl StructList) First() Struct

First return the first element of the slice.

func (StructList) Len

func (sl StructList) Len() int

Len says the size of the slice.

func (StructList) Names

func (sl StructList) Names() (out []string)

Names return the names of the structs.

Jump to

Keyboard shortcuts

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