astgo

package module
v0.0.0-...-cced755 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

go/ast utilities

the golang go/ast utilities.

astgo

proc

proc annotation syntax:

  • #[ident]: headless syntax
  • #[ident(name1=value1,name2=value2)]: no headless syntax

identis a identity, name=value slice in the (). value support the following syntax

  • string: "hello"
  • integer: 123
  • float: 1.0
  • bool: true,false
  • string slice: ["hello","world"]
  • integer slice: [123,12,1]
  • float slice: [1.0,1.1,1.2], NOTE: the first value in slice must be a float type, like [1,1.1,1.2] will parsed as integer slice, then failure.
  • bool slice: [true,false,true]
  • map[string]Value: {k1="v1", k2="v2"}, the value can be the defined.

example:

  • #[ident]
  • #[ident(k1=1,k2="2")]
  • #[ident(k1=[1,2,3],k2=["1","2","3"])]
  • #[ident(k1="hello",k2=["1","2","3"])]
  • #[ident(k1={k2="v2",k3="v3"})]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelCase

func CamelCase(s string) string

CamelCase returns the CamelCased name. If there is an interior underscore followed by a lower case letter, drop the underscore and convert the letter to upper case. There is a remote possibility of this rewrite causing a name collision, but it's so remote we're prepared to pretend it's nonexistent - since the C++ generator lowercases names, it's extremely unlikely to have two fields with different capitalizations. In short, _my_field_name_2 becomes XMyFieldName_2.

func CloneCommentGroup

func CloneCommentGroup(firstLF bool, c *ast.CommentGroup) *ast.CommentGroup

func CloneExpr

func CloneExpr(e ast.Expr) ast.Expr

func CloneIdent

func CloneIdent(src []*ast.Ident) []*ast.Ident

func FromComment

func FromComment(c *ast.CommentGroup) []string

func IdentEmptyString

func IdentEmptyString() *ast.Ident

ast.NewIdent(`""“)

func IdentErr

func IdentErr() *ast.Ident

ast.NewIdent("err")

func IdentError

func IdentError() *ast.Ident

ast.NewIdent("error")

func IdentName

func IdentName(vs []*ast.Ident) string

IdentName get first item's ident name.

func IdentNil

func IdentNil() *ast.Ident

ast.NewIdent("nil")

func ImportName

func ImportName(s *ast.ImportSpec) string

ImportName returns the name of s, or "" if the import is not named.

func ImportPath

func ImportPath(s *ast.ImportSpec) string

ImportPath returns the unquoted import path of s, or "" if the path is not properly quoted.

func JoinCase

func JoinCase(s string, delimiter byte) string

func Kebab

func Kebab(str string) string

Kebab 转换驼峰字符串为用'-'分隔的字符串 example2: delimiter = '-' initialisms = DefaultInitialisms IdCom -> id-com Idcom -> idcom nameIdCom -> name-id-com nameIdcom -> name-idcom

func LowTitle

func LowTitle(s string) string

LowTitle 首字母小写 see strings.Title

func NewBinaryExpr

func NewBinaryExpr(tok token.Token, x, y ast.Expr) *ast.BinaryExpr

func NewBlockStmt

func NewBlockStmt(stmts ...ast.Stmt) *ast.BlockStmt

func NewCallExpr

func NewCallExpr(fun ast.Expr, args ...ast.Expr) *ast.CallExpr

func NewComment

func NewComment(firstLF bool, comments ...string) []*ast.Comment

NewComment creates a new comment list

func NewCommentGroup

func NewCommentGroup(firstLF bool, comments ...string) *ast.CommentGroup

NewCommentGroup creates a new comment group

func NewDeclStmt

func NewDeclStmt(decl ast.Decl) *ast.DeclStmt

NewDeclStmt *GenDecl with CONST, TYPE, or VAR token

func NewEllipsis

func NewEllipsis(elt ast.Expr) *ast.Ellipsis

func NewExprStmt

func NewExprStmt(x ast.Expr) *ast.ExprStmt

func NewFieldFuncField

func NewFieldFuncField(name string, typ ast.Expr) *ast.Field

shortcuts NewField(typ).Name(name).FuncField()

func NewFieldUnameFuncField

func NewFieldUnameFuncField(typ ast.Expr) *ast.Field

shortcuts NewField(typ).FuncField()

func NewFuncDecl

func NewFuncDecl(name string, typ *ast.FuncType, body *ast.BlockStmt, opts ...Option) *ast.FuncDecl

func NewFuncLit

func NewFuncLit(ft *ast.FuncType, body *ast.BlockStmt) *ast.FuncLit

func NewGenDeclReferenceVariable

func NewGenDeclReferenceVariable(x, sel string, opt ...Option) *ast.GenDecl

var _ = context.TODO

func NewIdent

func NewIdent(name string) *ast.Ident

func NewImportSpec

func NewImportSpec(name, path string, opts ...Option) *ast.ImportSpec

NewImportSpec new the import with the given name and path to the file f, if absent. If name is empty, use PkgName(path).

import fmt "fmt"

func NewIndexExpr

func NewIndexExpr(x, index ast.Expr) *ast.IndexExpr

func NewIndexListExpr

func NewIndexListExpr(x ast.Expr, indices ...ast.Expr) *ast.IndexListExpr

func NewInterfaceType

func NewInterfaceType(fields ...*ast.Field) *ast.InterfaceType

func NewKeyValueExpr

func NewKeyValueExpr(key, value ast.Expr) *ast.KeyValueExpr

func NewLitChar

func NewLitChar(v string) *ast.BasicLit

func NewLitFloat

func NewLitFloat(v string) *ast.BasicLit

func NewLitImag

func NewLitImag(v string) *ast.BasicLit

func NewLitInt

func NewLitInt(v string) *ast.BasicLit

func NewLitString

func NewLitString(v string) *ast.BasicLit

func NewMethodDecl

func NewMethodDecl(name string, recv *ast.Field, typ *ast.FuncType, body *ast.BlockStmt, opts ...Option) *ast.FuncDecl

func NewParenExpr

func NewParenExpr(x ast.Expr) *ast.ParenExpr

func NewReturnStmt

func NewReturnStmt(expr ...ast.Expr) *ast.ReturnStmt

func NewSelectorExpr

func NewSelectorExpr(x ast.Expr, sel string) *ast.SelectorExpr

func NewStarExpr

func NewStarExpr(x ast.Expr) *ast.StarExpr

func NewStructType

func NewStructType(fields ...*ast.Field) *ast.StructType

func NewTypeAssertExpr

func NewTypeAssertExpr(x ast.Expr, tpy ast.Expr) *ast.TypeAssertExpr

tpy: asserted type; nil means type switch X.(type)

func NewTypeSpec

func NewTypeSpec(name string, typ ast.Expr, opts ...Option) *ast.TypeSpec

func NewUnameFuncDecl

func NewUnameFuncDecl(typ *ast.FuncType, body *ast.BlockStmt, opts ...Option) *ast.FuncDecl

func NewUnaryExpr

func NewUnaryExpr(op token.Token, x ast.Expr) *ast.UnaryExpr

func NewValueSpecReferenceVariable

func NewValueSpecReferenceVariable(x, sel string, opts ...Option) *ast.ValueSpec

NewValueSpecReferenceVariable format: `_ = context.TODO`, `_ = errors.New`

func PkgName

func PkgName(path string) string

PkgName returns the package name from a Go path with a package qualifier. github.com/{{owner}}/{{package}} -> {{package}}

func ReplaceInitialism

func ReplaceInitialism(s string) string

ReplaceInitialism replaces the initialism. see `defaultInitialisms` ID -> Id API -> Api APIRequest-> ApiRequest

func SmallCamelCase

func SmallCamelCase(fieldName string) string

SmallCamelCase to small camel case string id_com -> idCom idcom -> idcom name_id_com -> nameIdCom name_idcom -> nameIdcom

func SnakeCase

func SnakeCase(str string) string

SnakeCase 转换驼峰字符串为用'_'分隔的字符串 example2: delimiter = '_' initialisms = DefaultInitialisms IdCom -> id_com Idcom -> idcom nameIdCom -> name_id_com nameIdcom -> name_idcom

func SplitCase

func SplitCase(str string, delimiter byte) string

SplitCase 转换驼峰字符串为用delimiter分隔的字符串 example: delimiter = '_' 空字符 -> 空字符 HelloWorld -> hello_world Hello_World -> hello_world HiHello_World -> hi_hello_world IdCom -> id_com Idcom -> idcom nameIdCom -> name_id_com nameIdcom -> name_idcom

func UnStar

func UnStar(e ast.Expr) ast.Expr

*time.Time -> time.Time **time.Time -> time.Time

Types

type Applier

type Applier interface {
	// contains filtered or unexported methods
}

type AssignStmtBuilder

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

func NewAssignStmt

func NewAssignStmt() *AssignStmtBuilder

func NewShortVarDeclStmt

func NewShortVarDeclStmt() *AssignStmtBuilder

func (*AssignStmtBuilder) Build

func (b *AssignStmtBuilder) Build() *ast.AssignStmt

func (*AssignStmtBuilder) Lhs

func (b *AssignStmtBuilder) Lhs(lhs ...ast.Expr) *AssignStmtBuilder

func (*AssignStmtBuilder) Rhs

func (b *AssignStmtBuilder) Rhs(rhs ...ast.Expr) *AssignStmtBuilder

type BlockStmtBuilder

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

func NewBlockStmtBuilder

func NewBlockStmtBuilder(stmts ...ast.Stmt) *BlockStmtBuilder

func (*BlockStmtBuilder) Build

func (b *BlockStmtBuilder) Build() *ast.BlockStmt

func (*BlockStmtBuilder) Stmt

func (b *BlockStmtBuilder) Stmt(stmts ...ast.Stmt) *BlockStmtBuilder

type ChainExprBUilder

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

func NewChainExpr

func NewChainExpr(inner ast.Expr) *ChainExprBUilder

func NewChainExprIdent

func NewChainExprIdent(name string) *ChainExprBUilder

shortcut NewChainExpr(ast.NewIdent(name))

func (*ChainExprBUilder) Binary

func (b *ChainExprBUilder) Binary(tok token.Token, y ast.Expr) *ChainExprBUilder

func (*ChainExprBUilder) Build

func (b *ChainExprBUilder) Build() ast.Expr

func (*ChainExprBUilder) Call

func (b *ChainExprBUilder) Call(args ...ast.Expr) *ChainExprBUilder

func (*ChainExprBUilder) Ellipsis

func (b *ChainExprBUilder) Ellipsis() *ChainExprBUilder

func (*ChainExprBUilder) Index

func (b *ChainExprBUilder) Index(index ast.Expr) *ChainExprBUilder

func (*ChainExprBUilder) IndexList

func (b *ChainExprBUilder) IndexList(indices ...ast.Expr) *ChainExprBUilder

func (*ChainExprBUilder) KeyValue

func (b *ChainExprBUilder) KeyValue(value ast.Expr) *ChainExprBUilder

func (*ChainExprBUilder) Paren

func (b *ChainExprBUilder) Paren() *ChainExprBUilder

func (*ChainExprBUilder) Selector

func (b *ChainExprBUilder) Selector(sel string) *ChainExprBUilder

func (*ChainExprBUilder) Star

func (*ChainExprBUilder) TypeAssert

func (b *ChainExprBUilder) TypeAssert(typ ast.Expr) *ChainExprBUilder

tpy: asserted type; nil means type switch X.(type)

func (*ChainExprBUilder) Unary

type FieldBuilder

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

func NewField

func NewField(typ ast.Expr) *FieldBuilder

NewField function/method filed syntax: `a time.Time`, `a,b time.Time`, `time.Time` struct field syntax: `a time.Time`, `a,b time.Time`, `time.Time`

func (*FieldBuilder) Comment

func (b *FieldBuilder) Comment(comment *ast.CommentGroup) *FieldBuilder

func (*FieldBuilder) Doc

func (b *FieldBuilder) Doc(comment *ast.CommentGroup) *FieldBuilder

func (*FieldBuilder) FuncField

func (b *FieldBuilder) FuncField() *ast.Field

func (*FieldBuilder) Name

func (b *FieldBuilder) Name(name string) *FieldBuilder

func (*FieldBuilder) StructField

func (b *FieldBuilder) StructField() *ast.Field

func (*FieldBuilder) Tag

func (b *FieldBuilder) Tag(v string) *FieldBuilder

type File

type File struct {
	Filename string            // filename of the file
	Doc      *ast.CommentGroup // package comment
	Name     string            // package name
	Imports  *ImportMgr        // package import manager
	GenDecl  []ast.Decl        // top-level generic declarations(token.CONST, token.TYPE, token.VAR); or nil
	Decls    []ast.Decl        // top-level declarations
}

func (*File) Bytes

func (f *File) Bytes() ([]byte, error)

func (*File) WriteFile

func (f *File) WriteFile() error

type FuncTypeBuilder

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

func NewFuncType

func NewFuncType() *FuncTypeBuilder

func (*FuncTypeBuilder) Build

func (b *FuncTypeBuilder) Build() *ast.FuncType

func (*FuncTypeBuilder) Param

func (b *FuncTypeBuilder) Param(f ...*ast.Field) *FuncTypeBuilder

func (*FuncTypeBuilder) Result

func (b *FuncTypeBuilder) Result(f ...*ast.Field) *FuncTypeBuilder

type GenDeclBuilder

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

func NewGenDecl

func NewGenDecl(tok token.Token, spec ...ast.Spec) *GenDeclBuilder

NewGenDecl * tok: IMPORT, CONST, TYPE, or VAR

func NewGenDeclConst

func NewGenDeclConst(spec ...ast.Spec) *GenDeclBuilder

func NewGenDeclImport

func NewGenDeclImport(spec ...ast.Spec) *GenDeclBuilder

func NewGenDeclType

func NewGenDeclType(spec ...ast.Spec) *GenDeclBuilder

func NewGenDeclVar

func NewGenDeclVar(spec ...ast.Spec) *GenDeclBuilder

func (*GenDeclBuilder) Build

func (b *GenDeclBuilder) Build() *ast.GenDecl

func (*GenDeclBuilder) Doc

func (*GenDeclBuilder) Spec

func (b *GenDeclBuilder) Spec(spec ...ast.Spec) *GenDeclBuilder

type IfStmtBuilder

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

func NewIfStmt

func NewIfStmt() *IfStmtBuilder

func (*IfStmtBuilder) Body

func (*IfStmtBuilder) Build

func (b *IfStmtBuilder) Build() *ast.IfStmt

func (*IfStmtBuilder) Cond

func (b *IfStmtBuilder) Cond(v ast.Expr) *IfStmtBuilder

func (*IfStmtBuilder) Else

func (b *IfStmtBuilder) Else(v ast.Stmt) *IfStmtBuilder

func (*IfStmtBuilder) Init

func (b *IfStmtBuilder) Init(v ast.Stmt) *IfStmtBuilder

type ImportMgr

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

func NewImportMgr

func NewImportMgr() *ImportMgr

func (*ImportMgr) AddImport

func (g *ImportMgr) AddImport(path string, opts ...Option) (added bool)

func (*ImportMgr) AddNamedImport

func (g *ImportMgr) AddNamedImport(name, path string, opts ...Option) (added bool)

func (*ImportMgr) GetNamedImport

func (g *ImportMgr) GetNamedImport(name string) (path string, existed bool)

func (*ImportMgr) Imports

func (g *ImportMgr) Imports() []*ast.ImportSpec

func (*ImportMgr) Iter

func (g *ImportMgr) Iter(f func(name, path string) bool)

func (*ImportMgr) IterNamedImport

func (g *ImportMgr) IterNamedImport(f func(name, path string) bool)

func (*ImportMgr) Len

func (g *ImportMgr) Len() int

type Option

type Option func(Applier)

func WithComment

func WithComment(c *ast.CommentGroup) Option

func WithDocComment

func WithDocComment(c *ast.CommentGroup) Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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