astp

package module
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2024 License: MIT Imports: 18 Imported by: 1

README

astp

A golang ast syntax tree parser

parse your source code, and describe them with json or yaml

Features

  • 解析一个项目代码,生成可序列化内容
  • 内置处理枚举写法
  • 内置处理泛型和对象继承写法

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.VisitStruct(func(element *astp.Element) bool {
		return true
	}, func(e *astp.Element) {
		
    })
}
Use it with go generate
//go:generate go run github.com/linxlib/astp/astpg -o gen.json
Examples

check fw and fw_example for details

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

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
}

AstHandler ast树解析类,一个代表一个go文件

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)

Result 返回 File 对象

type Element added in v0.2.0

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

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

	Value      any                        `json:",omitempty" yaml:",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) GetTag added in v0.2.3

func (b *Element) GetTag() reflect.StructTag

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" yaml:",omitempty"` //文件名
	FilePath    string     `json:",omitempty" yaml:",omitempty"` //文件路径
	PackageName string     `json:",omitempty" yaml:",omitempty"` //包名
	PackagePath string     `json:",omitempty" yaml:",omitempty"` //包路径
	Docs        []string   `json:",omitempty" yaml:",omitempty"` //文档
	Comments    []string   `json:",omitempty" yaml:",omitempty"` //注释
	Imports     []*Import  `json:",omitempty" yaml:",omitempty"` //导入
	Consts      []*Element `json:",omitempty" yaml:",omitempty"` //常量
	Vars        []*Element `json:",omitempty" yaml:",omitempty"` //变量
	Structs     []*Element `json:",omitempty" yaml:",omitempty"` //结构体
	Methods     []*Element `json:"-"`                            //结构体方法
	Funcs       []*Element `json:",omitempty" yaml:",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) AddIgnorePkg added in v0.2.3

func (p *Parser) AddIgnorePkg(pkg string) *Parser

func (*Parser) IgnorePkg added in v0.2.3

func (p *Parser) IgnorePkg(pkg string) bool

func (*Parser) Load

func (p *Parser) Load(f string)

func (*Parser) Parse

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

func (*Parser) SetParseFunctions added in v0.2.3

func (p *Parser) SetParseFunctions(b bool) *Parser

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