astp

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2024 License: MIT Imports: 15 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

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Const

type Const struct {
	Name        string // 名称
	PackagePath string
	TypeString  string   //类型文本
	Type        *Struct  //类型
	Private     bool     //私有
	Slice       bool     //数组
	Value       any      //值
	Docs        []string //文档
	Comments    string   //注释
	IsIota      bool     //iota自动判定值(枚举)
}

Const 常量

func (*Const) GetName

func (c *Const) GetName() string

func (*Const) GetType

func (c *Const) GetType() *Struct

func (*Const) SetInnerType

func (c *Const) SetInnerType(b bool)

func (*Const) SetIsStruct

func (c *Const) SetIsStruct(b bool)

func (*Const) SetPackagePath

func (c *Const) SetPackagePath(s string)

func (*Const) SetPointer

func (c *Const) SetPointer(b bool)

func (*Const) SetPrivate

func (c *Const) SetPrivate(b bool)

func (*Const) SetSlice

func (c *Const) SetSlice(b bool)

func (*Const) SetType

func (c *Const) SetType(namer *Struct)

func (*Const) SetTypeString

func (c *Const) SetTypeString(s string)

type ConstSection

type ConstSection = []*Const

type File

type File struct {
	Name        string    //文件名
	PackageName string    //包名
	Imports     []*Import //导入
	PackagePath string    //包路径
	FilePath    string    //文件路径
	Structs     []*Struct //结构体
	Docs        []string  //文档
	Comments    []string  //注释
	Methods     []*Method //结构体方法
	Funcs       []*Method //函数
	Consts      []*Const  //常量
	Vars        []*Var    //变量
}

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

type Import

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

Import 导入

type Interface

type Interface struct {
	Name        string
	Methods     []*Method
	Constraints []string
}

Interface 接口

type Method

type Method struct {
	Receiver    *Receiver
	Index       int
	PackagePath string
	Name        string
	Private     bool
	Signature   string
	Docs        []string
	Comments    string
	Params      []*ParamField
	Results     []*ParamField
	IsGeneric   bool
	TypeParams  []*TypeParam
	// contains filtered or unexported fields
}

Method 结构体方法

func (*Method) Clone

func (m *Method) Clone() *Method

func (*Method) GetMethod

func (m *Method) GetMethod() any

func (*Method) GetName

func (m *Method) GetName() string

func (*Method) GetType

func (m *Method) GetType() *Struct

func (*Method) IsGenericType

func (m *Method) IsGenericType(name string) bool

func (*Method) SetActualType

func (m *Method) SetActualType(name string, as *Struct)

func (*Method) SetInnerType

func (m *Method) SetInnerType(b bool)

func (*Method) SetIsStruct

func (m *Method) SetIsStruct(b bool)

func (*Method) SetMethod

func (m *Method) SetMethod(method any)

func (*Method) SetPackagePath

func (m *Method) SetPackagePath(s string)

func (*Method) SetParamType

func (m *Method) SetParamType(field *ParamField, as *Struct)

func (*Method) SetPointer

func (m *Method) SetPointer(b bool)

func (*Method) SetPrivate

func (m *Method) SetPrivate(b bool)

func (*Method) SetSlice

func (m *Method) SetSlice(b bool)

func (*Method) SetType

func (m *Method) SetType(namer *Struct)

func (*Method) SetTypeString

func (m *Method) SetTypeString(s string)

type ParamField

type ParamField struct {
	Index       int     //索引
	Name        string  //字段名
	PackagePath string  //包名
	Type        *Struct //类型

	HasTag     bool              //是否有tag
	Tag        reflect.StructTag `json:"-"` //tag
	TypeString string            //类型文本
	InnerType  bool              //内置类型
	Private    bool              //私有
	Pointer    bool              //指针字段
	Slice      bool              //数组字段
	IsStruct   bool              //是否结构体
	Docs       []string          //文档
	Comment    string            //注释
	IsGeneric  bool
	// contains filtered or unexported fields
}

ParamField 结构体字段

func (*ParamField) Clone

func (p *ParamField) Clone() *ParamField

func (*ParamField) GetName

func (p *ParamField) GetName() string

func (*ParamField) GetRType

func (p *ParamField) GetRType() reflect.Type

func (*ParamField) GetType

func (p *ParamField) GetType() *Struct

func (*ParamField) SetInnerType

func (p *ParamField) SetInnerType(b bool)

func (*ParamField) SetIsStruct

func (p *ParamField) SetIsStruct(b bool)

func (*ParamField) SetPackagePath

func (p *ParamField) SetPackagePath(s string)

func (*ParamField) SetPointer

func (p *ParamField) SetPointer(b bool)

func (*ParamField) SetPrivate

func (p *ParamField) SetPrivate(b bool)

func (*ParamField) SetRType

func (p *ParamField) SetRType(t reflect.Type)

func (*ParamField) SetSlice

func (p *ParamField) SetSlice(b bool)

func (*ParamField) SetType

func (p *ParamField) SetType(namer *Struct)

func (*ParamField) SetTypeString

func (p *ParamField) SetTypeString(s string)

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) VisitAllStructs

func (p *Parser) VisitAllStructs(name string, f func(s *Struct) bool)

func (*Parser) WriteOut

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

type Receiver

type Receiver struct {
	Name       string  //参数名
	Pointer    bool    //是否指针
	TypeString string  //类型名
	Type       *Struct //类型
}

Receiver 接收器

func (*Receiver) Clone

func (r *Receiver) Clone() *Receiver

type Struct

type Struct struct {
	Name        string         //结构体名
	PackagePath string         //所属包名
	KeyHash     string         //所在文件hash值, 重复解析时直接获取
	Fields      []*StructField //结构体字段
	Methods     []*Method      //结构体方法
	HasParent   bool           //是否有继承结构
	IsInterface bool
	Inter       *Interface
	Docs        []string //文档
	Comment     string   //注释
	IsGeneric   bool
	TypeParams  []*TypeParam
}

Struct 结构体

func (*Struct) AppendFields

func (s *Struct) AppendFields(fields []*StructField)

func (*Struct) Clone

func (s *Struct) Clone() *Struct

func (*Struct) GetAllFieldsByTag

func (s *Struct) GetAllFieldsByTag(tag string) []*StructField

func (*Struct) GetName

func (s *Struct) GetName() string

func (*Struct) GetType

func (s *Struct) GetType() *Struct

func (*Struct) HandleCurrentPackageRef

func (s *Struct) HandleCurrentPackageRef(file *File)

func (*Struct) HandleCurrentPackageRefs

func (s *Struct) HandleCurrentPackageRefs(files map[string]*File)

func (*Struct) IsGenericType

func (s *Struct) IsGenericType(name string) bool

func (*Struct) SetActualType

func (s *Struct) SetActualType(name string, as *Struct)

func (*Struct) SetInnerType

func (s *Struct) SetInnerType(b bool)

func (*Struct) SetIsStruct

func (s *Struct) SetIsStruct(b bool)

func (*Struct) SetPackagePath

func (s *Struct) SetPackagePath(str string)

func (*Struct) SetPointer

func (s *Struct) SetPointer(b bool)

func (*Struct) SetPrivate

func (s *Struct) SetPrivate(b bool)

func (*Struct) SetSlice

func (s *Struct) SetSlice(b bool)

func (*Struct) SetType

func (s *Struct) SetType(namer *Struct)

func (*Struct) SetTypeString

func (s *Struct) SetTypeString(str string)

type StructField

type StructField struct {
	Index       int    //索引
	Name        string //字段名
	IsParent    bool
	PackagePath string  //包名
	Type        *Struct //类型

	HasTag     bool              //是否有tag
	Tag        reflect.StructTag `json:"-"` //tag
	TypeString string            //类型文本
	InnerType  bool              //内置类型
	Private    bool              //私有
	Pointer    bool              //指针字段
	Slice      bool              //数组字段
	IsStruct   bool              //是否结构体
	Docs       []string          //文档
	Comment    string            //注释
	IsGeneric  bool
	// contains filtered or unexported fields
}

StructField 结构体字段

func (*StructField) GetName

func (f *StructField) GetName() string

func (*StructField) GetRType

func (f *StructField) GetRType() reflect.Type

func (*StructField) GetTag

func (f *StructField) GetTag(tag string) string

func (*StructField) GetType

func (f *StructField) GetType() *Struct

func (*StructField) SetInnerType

func (f *StructField) SetInnerType(b bool)

func (*StructField) SetIsStruct

func (f *StructField) SetIsStruct(b bool)

func (*StructField) SetPackagePath

func (f *StructField) SetPackagePath(s string)

func (*StructField) SetPointer

func (f *StructField) SetPointer(b bool)

func (*StructField) SetPrivate

func (f *StructField) SetPrivate(b bool)

func (*StructField) SetRType

func (f *StructField) SetRType(t reflect.Type)

func (*StructField) SetSlice

func (f *StructField) SetSlice(b bool)

func (*StructField) SetType

func (f *StructField) SetType(namer *Struct)

func (*StructField) SetTypeString

func (f *StructField) SetTypeString(s string)

type TypeParam

type TypeParam struct {
	Name        string //泛型名
	PackagePath string
	TypeName    string  //泛型约束名
	Type        *Struct //泛型约束
	ActualType  *Struct //实际类型
}

func (*TypeParam) Clone

func (t *TypeParam) Clone() *TypeParam

func (*TypeParam) GetName

func (t *TypeParam) GetName() string

func (*TypeParam) GetType

func (t *TypeParam) GetType() *Struct

func (*TypeParam) SetInnerType

func (t *TypeParam) SetInnerType(b bool)

func (*TypeParam) SetIsStruct

func (t *TypeParam) SetIsStruct(b bool)

func (*TypeParam) SetPackagePath

func (t *TypeParam) SetPackagePath(s string)

func (*TypeParam) SetPointer

func (t *TypeParam) SetPointer(b bool)

func (*TypeParam) SetPrivate

func (t *TypeParam) SetPrivate(b bool)

func (*TypeParam) SetSlice

func (t *TypeParam) SetSlice(b bool)

func (*TypeParam) SetType

func (t *TypeParam) SetType(namer *Struct)

func (*TypeParam) SetTypeString

func (t *TypeParam) SetTypeString(s string)

type Var

type Var struct {
	Name        string //变量名
	PackagePath string
	TypeString  string   //类型文本
	Type        *Struct  //类型
	InnerType   bool     //内置类型
	IsStruct    bool     //是否结构体
	Pointer     bool     //指针
	Private     bool     //私有
	Slice       bool     //数组
	Value       any      //值
	Docs        []string //文档
	Comments    string   //注释
}

Var 变量

func (*Var) GetName

func (v *Var) GetName() string

func (*Var) GetType

func (v *Var) GetType() *Struct

func (*Var) SetInnerType

func (v *Var) SetInnerType(b bool)

func (*Var) SetIsStruct

func (v *Var) SetIsStruct(b bool)

func (*Var) SetPackagePath

func (v *Var) SetPackagePath(s string)

func (*Var) SetPointer

func (v *Var) SetPointer(b bool)

func (*Var) SetPrivate

func (v *Var) SetPrivate(b bool)

func (*Var) SetSlice

func (v *Var) SetSlice(b bool)

func (*Var) SetType

func (v *Var) SetType(namer *Struct)

func (*Var) SetTypeString

func (v *Var) SetTypeString(s 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