Documentation
¶
Index ¶
- func CreateImports(imports []string) string
- func GetPackageNameFromFile(astFile *ast.File) (packageName string)
- func GetPackageNameFromPath(path string) string
- func InjectImports(source []byte, packages []string) []byte
- type AstBundle
- func NewAstBundle(fileSet *token.FileSet, astFile *ast.File) *AstBundle
- func NewAstBundleV1(data []byte) (*AstBundle, error)
- func NewAstBundleV2(fset *token.FileSet, data []byte) (*AstBundle, error)
- func NewAstBundleV3(fset *token.FileSet, path string) (*AstBundle, error)
- func NewAstBundleV4(path string) (*AstBundle, error)
- func NewAstBundleV5(fset *token.FileSet, path string, mode parser.Mode) (*AstBundle, error)
- func NewAstBundleV6(path string, mode parser.Mode) (*AstBundle, error)
- func (ab *AstBundle) AddImport(path string) bool
- func (ab *AstBundle) AddNamedImport(name string, path string) bool
- func (ab *AstBundle) DeleteImport(path string) bool
- func (ab *AstBundle) DeleteNamedImport(name string, path string) bool
- func (ab *AstBundle) FormatSource() ([]byte, error)
- func (ab *AstBundle) GetBundle() (*ast.File, *token.FileSet)
- func (ab *AstBundle) GetPackageName() string
- func (ab *AstBundle) Print() error
- func (ab *AstBundle) SerializeAst() ([]byte, error)
- type PackageImportOptions
- func (param *PackageImportOptions) CreateImports() string
- func (param *PackageImportOptions) GetPkgPaths() []string
- func (param *PackageImportOptions) InjectImports(source []byte) []byte
- func (param *PackageImportOptions) SetInferredObject(object any) *PackageImportOptions
- func (param *PackageImportOptions) SetPkgPath(pkgPath string) *PackageImportOptions
- func (param *PackageImportOptions) SetPkgPaths(pkgPaths []string) *PackageImportOptions
- func (param *PackageImportOptions) SetReferencedType(reflectType reflect.Type) *PackageImportOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateImports ¶
CreateImports generates the import statements as a string from the provided package paths. CreateImports 从提供的包路径生成导入语句的字符串。
func GetPackageNameFromFile ¶
GetPackageNameFromFile extracts package name from an AST file node Accesses the Name field of the AST file to obtain the package name Returns the package name as a string from the parsed AST structure
GetPackageNameFromFile 从 AST 文件节点中提取包名 访问 AST 文件的 Name 字段以获取包名称 从已解析的 AST 结构返回包名字符串
func GetPackageNameFromPath ¶
GetPackageNameFromPath extracts package name from a Go source file at the given path Uses optimized parsing with PackageClauseOnly mode to read just the package statement Returns the package name as a string without reading the entire file content
GetPackageNameFromPath 从给定路径的 Go 源文件中提取包名 使用优化的 PackageClauseOnly 模式解析,仅读取包声明语句 返回包名字符串,无需读取整个文件内容
func InjectImports ¶
InjectImports inserts the missing import paths into the provided Go source code. InjectImports 将缺失的导入路径插入到提供的 Go 源代码中。
Types ¶
type AstBundle ¶
type AstBundle struct {
// contains filtered or unexported fields
}
AstBundle is a wrapper for an AST file and its corresponding FileSet. AstBundle 是一个封装 AST 文件及其对应 FileSet 的结构体。
func NewAstBundle ¶
NewAstBundle creates a new AstBundle. NewAstBundle 创建一个新的 AstBundle 实例。
func NewAstBundleV1 ¶
NewAstBundleV1 creates an AstBundle from a byte slice. NewAstBundleV1 根据字节切片创建 AstBundle 实例。
func NewAstBundleV2 ¶
NewAstBundleV2 creates an AstBundle from a byte slice with a provided FileSet. NewAstBundleV2 根据给定的 FileSet 和字节切片创建 AstBundle 实例。
func NewAstBundleV3 ¶
NewAstBundleV3 creates an AstBundle by parsing a file from the given path. NewAstBundleV3 根据文件路径解析并创建 AstBundle 实例。
func NewAstBundleV4 ¶
NewAstBundleV4 creates an AstBundle from a file path using a new FileSet. NewAstBundleV4 使用新的 FileSet 从文件路径创建 AstBundle 实例。
func NewAstBundleV5 ¶
NewAstBundleV5 creates an AstBundle by parsing a file with a specific parser mode. NewAstBundleV5 根据文件路径和特定的解析模式创建 AstBundle 实例。
func NewAstBundleV6 ¶
NewAstBundleV6 creates an AstBundle from a file path using a new FileSet and specific parser mode. NewAstBundleV6 使用新的 FileSet 和指定的解析模式从文件路径创建 AstBundle 实例。
func (*AstBundle) AddNamedImport ¶
AddNamedImport adds a new named import to the AST. AddNamedImport 向 AST 添加带名称的导入路径。
func (*AstBundle) DeleteImport ¶
DeleteImport removes an import from the AST. DeleteImport 从 AST 中删除指定的导入路径。
func (*AstBundle) DeleteNamedImport ¶
DeleteNamedImport removes a named import from the AST. DeleteNamedImport 从 AST 中删除指定名称的导入路径。
func (*AstBundle) FormatSource ¶
FormatSource formats the AST back into Go source code. FormatSource 将 AST 格式化为 Go 源代码。
func (*AstBundle) GetBundle ¶
GetBundle retrieves the AST file and FileSet from the AstBundle. GetBundle 从 AstBundle 中获取 AST 文件和 FileSet。
func (*AstBundle) GetPackageName ¶
GetPackageName retrieves the package name from the AST. GetPackageName 从 AST 中获取包名。
func (*AstBundle) SerializeAst ¶
SerializeAst serializes the AST into a textual representation. SerializeAst 将 AST 序列化为文本表示形式。
type PackageImportOptions ¶
type PackageImportOptions struct {
// contains filtered or unexported fields
}
PackageImportOptions is used to manage the import paths for Go packages. PackageImportOptions 用于管理 Go 包的导入路径。
func NewPackageImportOptions ¶
func NewPackageImportOptions() *PackageImportOptions
NewPackageImportOptions creates and returns a new PackageImportOptions instance. NewPackageImportOptions 创建并返回一个新的 PackageImportOptions 实例。
func (*PackageImportOptions) CreateImports ¶
func (param *PackageImportOptions) CreateImports() string
CreateImports generates a string containing import statements for the given package paths. CreateImports 根据给定的包路径生成包含导入语句的字符串。
func (*PackageImportOptions) GetPkgPaths ¶
func (param *PackageImportOptions) GetPkgPaths() []string
GetPkgPaths returns a merged list of package paths from packages, referenced types, and inferred objects. GetPkgPaths 返回从包路径、引用类型和推断对象中合并得到的包路径列表。
func (*PackageImportOptions) InjectImports ¶
func (param *PackageImportOptions) InjectImports(source []byte) []byte
InjectImports adds required import paths into the provided Go source code. InjectImports 将必要的导入路径添加到提供的 Go 源代码中。
func (*PackageImportOptions) SetInferredObject ¶
func (param *PackageImportOptions) SetInferredObject(object any) *PackageImportOptions
SetInferredObject adds an inferred object to the list of inferred objects. SetInferredObject 将一个推断对象添加到推断对象列表中。
func (*PackageImportOptions) SetPkgPath ¶
func (param *PackageImportOptions) SetPkgPath(pkgPath string) *PackageImportOptions
SetPkgPath adds a package path to the list of packages to be imported. SetPkgPath 将包路径添加到待导入的包列表中。
func (*PackageImportOptions) SetPkgPaths ¶ added in v0.0.50
func (param *PackageImportOptions) SetPkgPaths(pkgPaths []string) *PackageImportOptions
SetPkgPaths adds some package paths to the list of packages to be imported. SetPkgPaths 将包路径添加到待导入的包列表中。
func (*PackageImportOptions) SetReferencedType ¶
func (param *PackageImportOptions) SetReferencedType(reflectType reflect.Type) *PackageImportOptions
SetReferencedType adds a referenced type to the list of referenced types. SetReferencedType 将一个引用类型添加到引用类型列表中。