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
- func ParseRootGetPackages(fset *token.FileSet, root string) (map[string]*ast.Package, error)deprecated
- type AstBundle
- func MergeOnePackageFiles(root string, packageName string) (*AstBundle, error)deprecated
- func MergeSubPackageFiles(root string) (*AstBundle, error)deprecated
- 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) 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 ¶
func GetPackageNameFromPath ¶
func InjectImports ¶
InjectImports inserts the missing import paths into the provided Go source code. InjectImports 将缺失的导入路径插入到提供的 Go 源代码中。
func ParseRootGetPackages
deprecated
Deprecated: This function uses the deprecated `ast.Package` type and should be updated in the future. ParseRootGetPackages parses the entire directory and retrieves a map of package names to package information. ParseRootGetPackages 解析指定目录下的所有 Go 包,返回包名到包信息的映射。 Note: The function name could be more descriptive of its purpose.
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 MergeOnePackageFiles
deprecated
Deprecated: This function uses the deprecated `ast.Package` and `ast.MergePackageFiles` methods. MergeOnePackageFiles merges all Go source files of a specific package within a given directory. MergeOnePackageFiles 合并指定目录下某个包的所有 Go 源文件,生成一个语法树。 Note: This has limited usefulness and may need refactoring in the future.
func MergeSubPackageFiles
deprecated
Deprecated: This function uses the deprecated `ast.Package` type and `ast.MergePackageFiles` method. MergeSubPackageFiles merges all Go source files of the only package in the given directory. If there is more than one package, it returns an error. MergeSubPackageFiles 合并指定目录下唯一包的所有 Go 源文件,若目录中存在多个包则返回错误。
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 { Packages []string // List of package paths. // 直接设置包路径列表 ReferencedTypes []reflect.Type // List of referenced types to find package paths. // 设置反射类型,通过类型能找到包路径 InferredObjects []any // List of inferred objects to find package paths. // 设置要引用的对象列表(非指针对象),通过对象也能找到对象的包路径 }
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 necessary 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) SetReferencedType ¶
func (param *PackageImportOptions) SetReferencedType(reflectType reflect.Type) *PackageImportOptions
SetReferencedType adds a referenced type to the list of referenced types. SetReferencedType 将一个引用类型添加到引用类型列表中。