goast

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2025 License: MIT Imports: 12 Imported by: 0

README

goast

goast is a library for parsing Go code and extracting information, it supports merging two Go files into one.

Example of use

Parse Go code and extract information

package main

import (
	"fmt"
	"github.com/go-dev-frame/sponge/pkg/goast"
)

func main() {
	src := []byte(`package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello, world!")
}
`)

	// Case 1: Parse Go code and extract information
	{
		astInfos, err := goast.ParseGoCode("main.go", src)
	}

	// Case 2: Parse file and extract information
	{
		astInfos, err := goast.ParseFile("main.go")
	}
}

Merge two Go files into one

package main

import (
	"fmt"
	"github.com/go-dev-frame/sponge/pkg/goast"
)

func main() {
	const (
		srcFile = "data/src.go.code"
		genFile = "data/gen.go.code"
	)

	// Case 1: without covering the same function
	{
		codeAst, err := goast.MergeGoFile(srcFile, genFile)
		fmt.Println(codeAst.Code)
	}

	// Case 2: with covering the same function
	{
		codeAst, err := goast.MergeGoFile(srcFile, genFile, goast.WithCoverSameFunc())
		fmt.Println(codeAst.Code)
	}
}

Documentation

Overview

Package goast is a library for parsing Go code and extracting information from it.

Index

Constants

View Source
const (
	PackageType = "package"
	ImportType  = "import"
	ConstType   = "const"
	VarType     = "var"
	FuncType    = "func"
	TypeType    = "type"

	StructType    = "struct"
	InterfaceType = "interface"
	ArrayType     = "array"
	MapType       = "map"
	ChanType      = "chan"
)

Variables

This section is empty.

Functions

func ParseStruct

func ParseStruct(body string) (map[string]*StructInfo, error)

ParseStruct parse struct info from source code

func ParseStructMethods

func ParseStructMethods(astInfos []*AstInfo) map[string][]*MethodInfo

ParseStructMethods parse struct methods from ast infos

Types

type AstInfo

type AstInfo struct {
	// Type is the type of the code block, such as "func", "type", "const", "var", "import", "package".
	Type string

	// Names is the name of the code block, such as "func Name", "type Names", "const Names", "var Names", "import Paths".
	// If Type is "func", a standalone function without a receiver has a single name.
	// If the function is a method belonging to a struct, it has two names: the first
	// represents the function name, and the second represents the struct name.
	Names []string

	Comment string

	Body string
}

AstInfo Go code block information

func ParseFile

func ParseFile(goFilePath string) ([]*AstInfo, error)

ParseFile parses a go file and returns a list of AstInfo

func ParseGoCode

func ParseGoCode(filename string, data []byte) ([]*AstInfo, error)

ParseGoCode parses a go code and returns a list of AstInfo

func (*AstInfo) GetName

func (a *AstInfo) GetName() string

func (*AstInfo) IsConstType

func (a *AstInfo) IsConstType() bool

func (*AstInfo) IsFuncType

func (a *AstInfo) IsFuncType() bool

func (*AstInfo) IsImportType

func (a *AstInfo) IsImportType() bool

func (*AstInfo) IsPackageType

func (a *AstInfo) IsPackageType() bool

func (*AstInfo) IsTypeType

func (a *AstInfo) IsTypeType() bool

func (*AstInfo) IsVarType

func (a *AstInfo) IsVarType() bool

type CodeAst

type CodeAst struct {
	FilePath string
	Code     string

	AstInfos []*AstInfo
	// contains filtered or unexported fields
}

CodeAst is the struct for code

func MergeGoCode

func MergeGoCode(srcCode []byte, genCode []byte, opts ...CodeAstOption) (*CodeAst, error)

MergeGoCode merges two Go code strings into one.

func MergeGoFile

func MergeGoFile(srcFile string, genFile string, opts ...CodeAstOption) (*CodeAst, error)

MergeGoFile merges two Go code files into one.

func NewCodeAst

func NewCodeAst(filePath string, opts ...CodeAstOption) (*CodeAst, error)

NewCodeAst creates a new CodeAst object from file path

func NewCodeAstFromData

func NewCodeAstFromData(data []byte, opts ...CodeAstOption) (*CodeAst, error)

NewCodeAstFromData creates a new CodeAst object from data

type CodeAstOption

type CodeAstOption func(*CodeAst)

func WithCoverSameFunc

func WithCoverSameFunc() CodeAstOption

WithCoverSameFunc sets cover same function in the merged code

func WithIgnoreMergeFunc

func WithIgnoreMergeFunc(funcName ...string) CodeAstOption

WithIgnoreMergeFunc sets ignore to merge the same function name in the two code

type ConstInfo

type ConstInfo struct {
	Name    string
	Value   string
	Comment string
	Body    string
}

func ParseConstGroup

func ParseConstGroup(body string) ([]*ConstInfo, error)

ParseConstGroup parse const group from source code

type FuncInfo

type FuncInfo struct {
	Name    string
	Comment string
}

FuncInfo represents function information

func FilterFuncCode

func FilterFuncCode(filename string, data []byte, customFlag ...string) ([]byte, []FuncInfo, error)

FilterFuncCode filters out the code of functions that contain panic("implement me") or customized flag, e.g. panic("ai to do")

func FilterFuncCodeByFile

func FilterFuncCodeByFile(goFilePath string, customFlag ...string) ([]byte, []FuncInfo, error)

FilterFuncCodeByFile filters out the code of functions that contain panic("implement me") or customized flag, e.g. panic("ai to do")

func (FuncInfo) ExtractComment

func (f FuncInfo) ExtractComment() string

ExtractComment extracts function comments in Go code

type ImportInfo

type ImportInfo struct {
	Path    string
	Alias   string
	Comment string
	Body    string
}

func ParseImportGroup

func ParseImportGroup(body string) ([]*ImportInfo, error)

ParseImportGroup parse import group from source code

type InterfaceInfo

type InterfaceInfo struct {
	Name        string
	Comment     string
	MethodInfos []*MethodInfo
}

func ParseInterface

func ParseInterface(body string) ([]*InterfaceInfo, error)

ParseInterface parse interface group from source code

type MethodInfo

type MethodInfo struct {
	Name         string
	Comment      string
	Body         string
	ReceiverName string
	IsIdent      bool
}

MethodInfo method function info

type StructFieldInfo

type StructFieldInfo struct {
	Name    string
	Type    string
	Comment string
	Body    string
}

type StructInfo

type StructInfo struct {
	Name    string
	Comment string
	Fields  []*StructFieldInfo
}

type TypeInfo

type TypeInfo struct {
	Type    string
	Name    string
	Comment string
	Body    string
	IsIdent bool
}

func ParseTypeGroup

func ParseTypeGroup(body string) ([]*TypeInfo, error)

ParseTypeGroup parse type group from source code

type VarInfo

type VarInfo struct {
	Name    string
	Value   string
	Comment string
	Body    string
}

func ParseVarGroup

func ParseVarGroup(body string) ([]*VarInfo, error)

ParseVarGroup parse var group from source code

Jump to

Keyboard shortcuts

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