astp

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2024 License: MIT Imports: 17 Imported by: 1

README

astp

A golang ast syntax tree parser

TODO List

  • store files group by package
  • 解析各种奇奇怪怪的类型
  • generic type support
  • go generate support
  • Struct 继承,合并字段和方法

Usage

Common Use
package main

import (
	"flag"
	"fmt"
	"github.com/linxlib/astp"
)

var (
	outFile string
)

func init() {
	flag.StringVar(&outFile, "o", "default", "-o gen.json")
}
func main() {
	flag.Parse()
	p := astp.NewParser()
	p.Parse()
	if outFile == "" {
		outFile = "gen.json"
	}
	err := p.WriteOut(outFile)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("complete!")
}
Load
package main

import (
	"github.com/linxlib/astp"
)
func main() {
    parser := astp.NewParser()
    parser.Load("gen.json")
    parser.VisitAllStructs("<Struct Name>", func(s *astp.Struct) bool {
		return false
	})
}
Use it with go generate
//go:generate go run github.com/linxlib/astp/astpg -o gen.json
Examples

check fw_example

Documentation

Index

Constants

View Source
const (
	PackageBuiltIn     = "builtin"
	PackageThisPackage = "this"
	PackageOther       = "other"
	PackageThird       = "third"
	PackageIgnore      = "ignore"
)

Variables

This section is empty.

Functions

func CheckPackage added in v0.2.0

func CheckPackage(modPkg string, pkg string) string

CheckPackage 返回某个包是何种类型的包

func PackagePath added in v0.2.0

func PackagePath(pkgName string, typeName string) string

TODO: 需要判断 1. 内置类型 2. 内置包 3. 第三方包

func ParseIt added in v0.2.0

func ParseIt(modPkg string, pkgPath string) bool

ParseIt 返回一个类型是否需要进行解析(第三方包 系统内置包)

Types

type AstHandler added in v0.2.0

type AstHandler struct {
	// contains filtered or unexported fields
}

func NewAstHandler added in v0.2.0

func NewAstHandler(f *File, modPkg string, astFile *ast.File, findHandler FindHandler) *AstHandler

func (*AstHandler) HandleElements added in v0.2.0

func (a *AstHandler) HandleElements() *AstHandler

func (*AstHandler) HandleImports added in v0.2.0

func (a *AstHandler) HandleImports() *AstHandler

func (*AstHandler) HandlePackages added in v0.2.0

func (a *AstHandler) HandlePackages() *AstHandler

func (*AstHandler) Result added in v0.2.0

func (a *AstHandler) Result() (*File, string)

type Element added in v0.2.0

type Element struct {
	Name        string      `json:",omitempty"`
	PackagePath string      `json:",omitempty"` //包路径
	PackageName string      `json:",omitempty"` //包名
	ElementType ElementType `json:",omitempty"` //当前元素类型

	ItemType      ElementType `json:",omitempty"` //Item的元素类型
	Item          *Element    `json:",omitempty"`
	Index         int
	Tag           reflect.StructTag `json:"-"`
	TypeString    string            `json:",omitempty"` //字段类型名
	ElementString string            `json:",omitempty"`
	Docs          []string          `json:",omitempty"` //上方的文档
	Comment       string            `json:",omitempty"` //后方的注释

	Value      any                        `json:",omitempty"` //值 一般为枚举时用到
	FromParent bool                       //表示当前元素 包含从父级继承而来的字段、方法、文档 或者 表示当前元素是继承而来
	Elements   map[ElementType][]*Element // 子成员 比如字段 方法 泛型类型
	// contains filtered or unexported fields
}

func (*Element) Clone added in v0.2.0

func (b *Element) Clone(i ...int) *Element

func (*Element) ElementsAll added in v0.2.0

func (b *Element) ElementsAll(elementType ElementType) []*Element

func (*Element) Generic added in v0.2.0

func (b *Element) Generic() bool

func (*Element) GetRType added in v0.2.0

func (b *Element) GetRType() reflect.Type

func (*Element) GetRValue added in v0.2.0

func (b *Element) GetRValue() reflect.Value

func (*Element) GetValue added in v0.2.0

func (b *Element) GetValue() any

func (*Element) MustGetElement added in v0.2.0

func (b *Element) MustGetElement(elementType ElementType) *Element

func (*Element) Private added in v0.2.0

func (b *Element) Private() bool

func (*Element) SetRType added in v0.2.0

func (b *Element) SetRType(t reflect.Type)

func (*Element) SetRValue added in v0.2.0

func (b *Element) SetRValue(value reflect.Value)

func (*Element) SetValue added in v0.2.0

func (b *Element) SetValue(i any)

func (*Element) Signature added in v0.2.0

func (b *Element) Signature() string

func (*Element) String added in v0.2.0

func (b *Element) String() string

func (*Element) VisitElements added in v0.2.0

func (b *Element) VisitElements(elementType ElementType, check func(element *Element) bool, f func(element *Element))

func (*Element) VisitElementsAll added in v0.2.0

func (b *Element) VisitElementsAll(elementType ElementType, f func(element *Element))

type ElementType added in v0.2.0

type ElementType string
const (
	ElementNone ElementType = "NONE"

	ElementStruct    ElementType = "STRUCT"
	ElementField     ElementType = "FIELD"
	ElementConst     ElementType = "CONST"
	ElementVar       ElementType = "VAR"
	ElementFunc      ElementType = "FUNC"
	ElementInterface ElementType = "INTERFACE"

	ElementMethod    ElementType = "METHOD"
	ElementEnum      ElementType = "ENUM"
	ElementReceiver  ElementType = "RECEIVER"
	ElementGeneric   ElementType = "TYPE_PARAM"
	ElementParam     ElementType = "PARAM"
	ElementResult    ElementType = "RESULT"
	ElementConstrain ElementType = "CONSTRAIN"
)

type File

type File struct {
	Name        string     `json:",omitempty"` //文件名
	FilePath    string     `json:",omitempty"` //文件路径
	PackageName string     `json:",omitempty"` //包名
	PackagePath string     `json:",omitempty"` //包路径
	Docs        []string   `json:"-"`          //文档
	Comments    []string   `json:"-"`          //注释
	Imports     []*Import  `json:"-"`          //导入
	Consts      []*Element `json:",omitempty"` //常量
	Vars        []*Element `json:",omitempty"` //变量
	Structs     []*Element `json:",omitempty"` //结构体
	Methods     []*Element `json:"-"`          //结构体方法
	Funcs       []*Element `json:",omitempty"` //函数

}

File 代表一个已解析的go文件

func (*File) IsMain added in v0.2.0

func (f *File) IsMain() bool

type FindHandler added in v0.2.0

type FindHandler func(pkg string, name string) *Element

type Import

type Import struct {
	Name       string //名称
	Alias      string //别名
	ImportPath string //导入路径
	IsIgnore   bool   //是否 _ 隐式导入
}

Import 导入

type Parser

type Parser struct {
	Files map[string]*File
	// contains filtered or unexported fields
}

func NewParser

func NewParser() (p *Parser)

func (*Parser) Load

func (p *Parser) Load(f string)

func (*Parser) Parse

func (p *Parser) Parse(fa ...string)

func (*Parser) VisitStruct added in v0.2.0

func (p *Parser) VisitStruct(check func(element *Element) bool, f func(element *Element))

func (*Parser) WriteOut

func (p *Parser) WriteOut(filename string) error

type PkgType added in v0.2.0

type PkgType struct {
	IsGeneric bool
	PkgPath   string
	TypeName  string
}

Directories

Path Synopsis
maybe some package description
maybe some package description

Jump to

Keyboard shortcuts

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